/*
 * Copyright 2017 anand.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sshd.shell.springboot.autoconfiguration;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.util.CollectionUtils;
import sshd.shell.springboot.ShellException;

/**
 *
 * @author anand
 */
public class CommandExecutableDetails {

    private final Set<String> roles;
    @lombok.Getter
    private final String description;
    @lombok.Getter
    private final CommandExecutor commandExecutor;

    CommandExecutableDetails(SshdShellCommand command, CommandExecutor commandExecutor) {
        this.roles = Collections.unmodifiableSet(new HashSet<>(Arrays.<String>asList(command.roles())));
        this.description = command.description();
        this.commandExecutor = commandExecutor;
    }

    public boolean matchesRole(Collection<String> userRoles) {
        if (roles.contains("*") || userRoles.contains("*")) {
            return true;
        }
        return CollectionUtils.containsAny(roles, userRoles);
    }

    public String executeWithArg(String arg) throws InterruptedException, ShellException {
        return commandExecutor.get(arg);
    }
}

@@ -638,33 +638,8 @@
 n;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -663,16 +663,16 @@
 ection;%0A
+
 import j
@@ -697,34 +697,8 @@
 ns;%0A
-import java.util.HashSet;%0A
 impo
@@ -838,16 +838,43 @@
 and%0A */%0A
+@lombok.extern.slf4j.Slf4j%0A
 public c
@@ -943,16 +943,50 @@
  roles;%0A
+    private final String command;%0A
     @lom
@@ -1159,16 +1159,35 @@
 command,
+ Set%3CString%3E roles,
  Command
@@ -1267,61 +1267,53 @@
 Set(
-new HashSet%3C%3E(Arrays.%3CString%3EasList(command.roles()))
+roles);%0A        this.command = command.value(
 );%0A 
@@ -1736,16 +1736,16 @@
 ption %7B%0A
-
         
@@ -1782,11 +1782,234 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A        return new StringBuilder(%22Command: %22).append(command).append(%22, description: %22).append(description)%0A                .append(%22, roles: %22).append(roles).toString();%0A    %7D
 %0A%7D%0A

Support for custom roles in System commands