Repository: apache/kafka
File: tools/src/main/java/org/apache/kafka/tools/AclCommand.java (Java)

Code before:
            System.out.println(Utils.stackTrace(e));
            Exit.exit(1);
        }
    }

    private static Properties adminConfigs(AclCommandOptions opts) throws IOException {
        Properties props = new Properties();
        if (opts.options.has(opts.commandConfigOpt)) {
            props = Utils.loadProps(opts.options.valueOf(opts.commandConfigOpt));
        }
        if (opts.options.has(opts.bootstrapServerOpt)) {
            props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, opts.options.valueOf(opts.bootstrapServerOpt));
        } else {
            props.put(AdminClientConfig.BOOTSTRAP_CONTROLLERS_CONFIG, opts.options.valueOf(opts.bootstrapControllerOpt));
        }
        return props;
    }

    private static void addAcls(Admin admin, AclCommandOptions opts) throws ExecutionException, InterruptedException {
        Map<ResourcePattern, Set<AccessControlEntry>> resourceToAcl = getResourceToAcls(opts);
        for (Map.Entry<ResourcePattern, Set<AccessControlEntry>> entry : resourceToAcl.entrySet()) {
            ResourcePattern resource = entry.getKey();
            Set<AccessControlEntry> acls = entry.getValue();

            AclBindingFilter filter = new AclBindingFilter(resource.toFilter(), AccessControlEntryFilter.ANY);
            Collection<AclBinding> existingBindings = admin.describeAcls(filter).values().get();
            Set<AclBinding> existingBindingsSet = new HashSet<>(existingBindings);

            List<AclBinding> aclBindings = new ArrayList<>();
            List<AccessControlEntry> aclsToAdd = new ArrayList<>();
            for (AccessControlEntry acl : acls) {
                AclBinding binding = new AclBinding(resource, acl);
                if (existingBindingsSet.contains(binding)) {
                    System.out.println("Acl " + binding + " already exists.");
                } else {
                    aclBindings.add(binding);
                    aclsToAdd.add(acl);
                }
            }

            if (!aclBindings.isEmpty()) {
                System.out.println("Adding ACLs for resource `" + resource + "`: " + NL + " " + aclsToAdd.stream().map(a -> "\t" + a).collect(Collectors.joining(NL)) + NL);
                admin.createAcls(aclBindings).all().get();
            }
        }
    }

    private static void removeAcls(Admin admin, AclCommandOptions opts) throws ExecutionException, InterruptedException {
        Map<ResourcePatternFilter, Set<AccessControlEntry>> filterToAcl = getResourceFilterToAcls(opts);
        for (Map.Entry<ResourcePatternFilter, Set<AccessControlEntry>> entry : filterToAcl.entrySet()) {
            ResourcePatternFilter filter = entry.getKey();

Review comment (suggestion):
```java
var existingBindingsSet = Set.copyOf(admin.describeAcls(filter).values().get());
```

Code after:
            System.out.println(Utils.stackTrace(e));
            Exit.exit(1);
        }
    }

    private static Properties adminConfigs(AclCommandOptions opts) throws IOException {
        Properties props = new Properties();
        if (opts.options.has(opts.commandConfigOpt)) {
            props = Utils.loadProps(opts.options.valueOf(opts.commandConfigOpt));
        }
        if (opts.options.has(opts.bootstrapServerOpt)) {
            props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, opts.options.valueOf(opts.bootstrapServerOpt));
        } else {
            props.put(AdminClientConfig.BOOTSTRAP_CONTROLLERS_CONFIG, opts.options.valueOf(opts.bootstrapControllerOpt));
        }
        return props;
    }

    private static void addAcls(Admin admin, AclCommandOptions opts) throws ExecutionException, InterruptedException {
        Map<ResourcePattern, Set<AccessControlEntry>> resourceToAcl = getResourceToAcls(opts);
        for (Map.Entry<ResourcePattern, Set<AccessControlEntry>> entry : resourceToAcl.entrySet()) {
            ResourcePattern resource = entry.getKey();
            Set<AccessControlEntry> acls = entry.getValue();

            AclBindingFilter filter = new AclBindingFilter(resource.toFilter(), AccessControlEntryFilter.ANY);
            Set<AclBinding> existingBindingsSet = Set.copyOf(admin.describeAcls(filter).values().get());

            List<AclBinding> aclBindings = new ArrayList<>();
            for (AccessControlEntry acl : acls) {
                AclBinding binding = new AclBinding(resource, acl);
                if (existingBindingsSet.contains(binding)) {
                    System.out.println("Acl " + binding + " already exists.");
                } else {
                    aclBindings.add(binding);
                }
            }

            if (!aclBindings.isEmpty()) {
                System.out.println("Adding ACLs for resource `" + resource + "`: " + NL + " " + aclBindings.stream().map(AclBinding::entry).map(a -> "\t" + a).collect(Collectors.joining(NL)) + NL);
                admin.createAcls(aclBindings).all().get();
            }
        }
    }

    private static void removeAcls(Admin admin, AclCommandOptions opts) throws ExecutionException, InterruptedException {
        Map<ResourcePatternFilter, Set<AccessControlEntry>> filterToAcl = getResourceFilterToAcls(opts);
        for (Map.Entry<ResourcePatternFilter, Set<AccessControlEntry>> entry : filterToAcl.entrySet()) {
            ResourcePatternFilter filter = entry.getKey();
            Set<AccessControlEntry> acls = entry.getValue();
            if (acls.isEmpty()) {
                if (confirmAction(opts, "Are you sure you want to delete all ACLs for resource filter `" + filter + "`? (y/n)")) {