Search in sources :

Example 21 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class CommandsHelpProvider method getCommands.

private Set<Command> getCommands(Session session, String path) {
    // TODO: this is not really clean
    List<Command> commands = session.getRegistry().getCommands();
    String subshell = (String) session.get(Session.SUBSHELL);
    String completionMode = (String) session.get(Session.COMPLETION_MODE);
    Set<Command> matchingCommands = new HashSet<>();
    for (Command command : commands) {
        String name = command.getScope() + ":" + command.getName();
        if (command != null && !name.startsWith(path)) {
            continue;
        }
        if (completionMode != null && completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL)) {
            // filter the help only for "global" commands
            if (subshell == null || subshell.trim().isEmpty()) {
                if (!name.startsWith(Session.SCOPE_GLOBAL)) {
                    continue;
                }
            }
        }
        if (completionMode != null && (completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL) || completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_FIRST))) {
            // filter the help only for commands local to the subshell
            if (!name.startsWith(subshell)) {
                continue;
            }
        }
        matchingCommands.add(command);
    }
    return matchingCommands;
}
Also used : Command(org.apache.karaf.shell.api.console.Command) HashSet(java.util.HashSet)

Example 22 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class CommandWrapper method execute.

@Override
public Object execute(final CommandSession commandSession, List<Object> arguments) throws Exception {
    // TODO: remove the hack for .session
    Session session = (Session) commandSession.get(".session");
    // When need to translate closures to a compatible type for the command
    for (int i = 0; i < arguments.size(); i++) {
        Object v = arguments.get(i);
        if (v instanceof Closure) {
            final Closure closure = (Closure) v;
            arguments.set(i, (org.apache.karaf.shell.api.console.Function) (s, a) -> closure.execute(commandSession, a));
        }
    }
    return command.execute(session, arguments);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) List(java.util.List) Closure(org.apache.felix.gogo.runtime.Closure) Function(org.apache.felix.service.command.Function) CommandSession(org.apache.felix.service.command.CommandSession) Command(org.apache.karaf.shell.api.console.Command) Closure(org.apache.felix.gogo.runtime.Closure) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Aggregations

Command (org.apache.karaf.shell.api.console.Command)22 HashSet (java.util.HashSet)5 CommandLine (org.apache.karaf.shell.api.console.CommandLine)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Parser (org.apache.karaf.shell.api.console.Parser)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Session (org.apache.karaf.shell.api.console.Session)3 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)3 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 Callable (java.util.concurrent.Callable)2 ParsedLineImpl (org.apache.felix.gogo.jline.ParsedLineImpl)2 Program (org.apache.felix.gogo.runtime.Parser.Program)2 Statement (org.apache.felix.gogo.runtime.Parser.Statement)2 Completer (org.apache.karaf.shell.api.console.Completer)2