Search in sources :

Example 6 with Command

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

the class Execute method listCommands.

private static void listCommands() {
    System.out.println("Available commands:");
    for (Map.Entry<String, Class<?>> entry : COMMANDS.entrySet()) {
        Command ann = entry.getValue().getAnnotation(Command.class);
        System.out.printf("  %s - %s\n", entry.getKey(), ann.description());
    }
    System.out.println("Type 'command --help' for more help on the specified command.");
}
Also used : ChangeOptsCommand(org.apache.karaf.instance.command.ChangeOptsCommand) CloneCommand(org.apache.karaf.instance.command.CloneCommand) ChangeSshPortCommand(org.apache.karaf.instance.command.ChangeSshPortCommand) StartCommand(org.apache.karaf.instance.command.StartCommand) Command(org.apache.karaf.shell.api.action.Command) RestartCommand(org.apache.karaf.instance.command.RestartCommand) StopCommand(org.apache.karaf.instance.command.StopCommand) RenameCommand(org.apache.karaf.instance.command.RenameCommand) StatusCommand(org.apache.karaf.instance.command.StatusCommand) DestroyCommand(org.apache.karaf.instance.command.DestroyCommand) ListCommand(org.apache.karaf.instance.command.ListCommand) ChangeRmiServerPortCommand(org.apache.karaf.instance.command.ChangeRmiServerPortCommand) CreateCommand(org.apache.karaf.instance.command.CreateCommand) ChangeRmiRegistryPortCommand(org.apache.karaf.instance.command.ChangeRmiRegistryPortCommand) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 7 with Command

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

the class GenerateHelpMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) && !FORMAT_ASCIIDOC.equals(format)) {
            throw new MojoFailureException("Unsupported format: " + format + ". Supported formats are: asciidoc, docbx, or conf.");
        }
        if (!targetFolder.exists()) {
            targetFolder.mkdirs();
        }
        ClassFinder finder = createFinder(classLoader);
        List<Class<?>> classes = finder.findAnnotatedClasses(Command.class);
        if (classes.isEmpty()) {
            throw new MojoFailureException("No command found");
        }
        CommandHelpPrinter helpPrinter = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            helpPrinter = new AsciiDoctorCommandHelpPrinter();
        }
        if (FORMAT_CONF.equals(format)) {
            helpPrinter = new UserConfCommandHelpPrinter();
        }
        if (FORMAT_DOCBX.equals(format)) {
            helpPrinter = new DocBookCommandHelpPrinter();
        }
        Map<String, Set<String>> commands = new TreeMap<>();
        String commandSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            commandSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            commandSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            commandSuffix = "xml";
        }
        for (Class<?> clazz : classes) {
            try {
                Action action = (Action) clazz.newInstance();
                Command cmd = clazz.getAnnotation(Command.class);
                // skip the *-help command
                if (cmd.scope().equals("*"))
                    continue;
                File output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + "." + commandSuffix);
                FileOutputStream outStream = new FileOutputStream(output);
                PrintStream out = new PrintStream(outStream);
                helpPrinter.printHelp(action, out, includeHelpOption);
                out.close();
                outStream.close();
                commands.computeIfAbsent(cmd.scope(), k -> new TreeSet<>()).add(cmd.name());
                getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
            } catch (Exception e) {
                getLog().warn("Unable to write help for " + clazz.getName(), e);
            }
        }
        String overViewSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            overViewSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            overViewSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            overViewSuffix = "xml";
        }
        PrintStream writer = new PrintStream(new FileOutputStream(new File(targetFolder, "commands." + overViewSuffix)));
        helpPrinter.printOverview(commands, writer);
        writer.close();
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : PrintStream(java.io.PrintStream) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Action(org.apache.karaf.shell.api.action.Action) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) List(java.util.List) TreeMap(java.util.TreeMap) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PrintStream(java.io.PrintStream) Action(org.apache.karaf.shell.api.action.Action) Set(java.util.Set) TreeSet(java.util.TreeSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TreeMap(java.util.TreeMap) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Command(org.apache.karaf.shell.api.action.Command) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File)

Example 8 with Command

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

the class UserConfCommandHelpPrinter method printHelp.

@Override
public void printHelp(Action action, PrintStream out, boolean includeHelpOption) {
    Command command = action.getClass().getAnnotation(Command.class);
    Set<Option> options = new HashSet<>();
    List<Argument> arguments = new ArrayList<>();
    Map<Argument, Field> argFields = new HashMap<>();
    Map<Option, Field> optFields = new HashMap<>();
    for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
        for (Field field : type.getDeclaredFields()) {
            Option option = field.getAnnotation(Option.class);
            if (option != null) {
                options.add(option);
            }
            Argument argument = field.getAnnotation(Argument.class);
            if (argument != null) {
                argument = replaceDefaultArgument(field, argument);
                argFields.put(argument, field);
                int index = argument.index();
                while (arguments.size() <= index) {
                    arguments.add(null);
                }
                if (arguments.get(index) != null) {
                    throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
                }
                arguments.set(index, argument);
            }
        }
    }
    if (includeHelpOption)
        options.add(HelpOption.HELP);
    out.println("h1. " + command.scope() + ":" + command.name());
    out.println();
    out.println("h2. Description");
    out.println(command.description());
    out.println();
    StringBuffer syntax = new StringBuffer();
    syntax.append(String.format("%s:%s", command.scope(), command.name()));
    if (options.size() > 0) {
        syntax.append(" \\[options\\]");
    }
    if (arguments.size() > 0) {
        syntax.append(' ');
        for (Argument argument : arguments) {
            syntax.append(String.format(argument.required() ? "%s " : "\\[%s\\] ", argument.name()));
        }
    }
    out.println("h2. Syntax");
    out.println(syntax.toString());
    out.println();
    if (arguments.size() > 0) {
        out.println("h2. Arguments");
        out.println("|| Name || Description ||");
        for (Argument argument : arguments) {
            String description = argument.description();
            if (!argument.required()) {
                Object o = getDefaultValue(action, argFields.get(argument));
                String defaultValue = getDefaultValueString(o);
                if (defaultValue != null) {
                    description += " (defaults to " + o.toString() + ")";
                }
            }
            out.println("| " + argument.name() + " | " + description + " |");
        }
        out.println();
    }
    if (options.size() > 0) {
        out.println("h2. Options");
        out.println("|| Name || Description ||");
        for (Option option : options) {
            String opt = option.name();
            String desc = option.description();
            for (String alias : option.aliases()) {
                opt += ", " + alias;
            }
            Object o = getDefaultValue(action, optFields.get(option));
            String defaultValue = getDefaultValueString(o);
            if (defaultValue != null) {
                desc += " (defaults to " + defaultValue + ")";
            }
            out.println("| " + opt + " | " + desc + " |");
        }
        out.println();
    }
    if (command.detailedDescription().length() > 0) {
        out.println("h2. Details");
        out.println(command.detailedDescription());
    }
    out.println();
}
Also used : Argument(org.apache.karaf.shell.api.action.Argument) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) Command(org.apache.karaf.shell.api.action.Command) HelpOption(org.apache.karaf.shell.impl.action.command.HelpOption) Option(org.apache.karaf.shell.api.action.Option) HashSet(java.util.HashSet)

Example 9 with Command

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

the class DefaultActionPreparator method printUsage.

public void printUsage(Action action, Map<Option, Field> options, Map<Argument, Field> arguments, PrintStream out, boolean globalScope, int termWidth) {
    Command command = action.getClass().getAnnotation(Command.class);
    if (command != null) {
        List<Argument> argumentsSet = new ArrayList<Argument>(arguments.keySet());
        argumentsSet.sort(Comparator.comparing(Argument::index));
        Set<Option> optionsSet = new HashSet<Option>(options.keySet());
        optionsSet.add(HelpOption.HELP);
        if (command != null && (command.description() != null || command.name() != null)) {
            out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL);
            out.print("        ");
            if (command.name() != null) {
                if (globalScope) {
                    out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
                } else {
                    out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
                }
                out.println();
            }
            out.print("\t");
            out.println(command.description());
            out.println();
        }
        StringBuffer syntax = new StringBuffer();
        if (command != null) {
            if (globalScope) {
                syntax.append(command.name());
            } else {
                syntax.append(String.format("%s:%s", command.scope(), command.name()));
            }
        }
        if (options.size() > 0) {
            syntax.append(" [options]");
        }
        if (arguments.size() > 0) {
            syntax.append(' ');
            for (Argument argument : argumentsSet) {
                if (!argument.required()) {
                    syntax.append(String.format("[%s] ", argument.name()));
                } else {
                    syntax.append(String.format("%s ", argument.name()));
                }
            }
        }
        out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL);
        out.print("        ");
        out.println(syntax.toString());
        out.println();
        if (arguments.size() > 0) {
            out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL);
            for (Argument argument : argumentsSet) {
                out.print("        ");
                out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL);
                printFormatted("                ", argument.description(), termWidth, out, true);
                if (!argument.required()) {
                    if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
                        if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
                            Object o = getDefaultValue(action, arguments.get(argument));
                            String defaultValue = getDefaultValueString(o);
                            if (defaultValue != null) {
                                printDefaultsTo(out, defaultValue);
                            }
                        } else {
                            printDefaultsTo(out, argument.valueToShowInHelp());
                        }
                    }
                }
            }
            out.println();
        }
        if (options.size() > 0) {
            out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL);
            for (Option option : optionsSet) {
                String opt = option.name();
                for (String alias : option.aliases()) {
                    opt += ", " + alias;
                }
                out.print("        ");
                out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL);
                printFormatted("                ", option.description(), termWidth, out, true);
                if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
                    if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
                        Object o = getDefaultValue(action, options.get(option));
                        String defaultValue = getDefaultValueString(o);
                        if (defaultValue != null) {
                            printDefaultsTo(out, defaultValue);
                        }
                    } else {
                        printDefaultsTo(out, option.valueToShowInHelp());
                    }
                }
            }
            out.println();
        }
        if (command.detailedDescription().length() > 0) {
            out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL);
            String desc = loadDescription(action.getClass(), command.detailedDescription());
            printFormatted("        ", desc, termWidth, out, true);
        }
    }
}
Also used : Argument(org.apache.karaf.shell.api.action.Argument) Command(org.apache.karaf.shell.api.action.Command) ArrayList(java.util.ArrayList) Option(org.apache.karaf.shell.api.action.Option) HashSet(java.util.HashSet)

Aggregations

Command (org.apache.karaf.shell.api.action.Command)9 Argument (org.apache.karaf.shell.api.action.Argument)6 Option (org.apache.karaf.shell.api.action.Option)6 ArrayList (java.util.ArrayList)5 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 HashSet (java.util.HashSet)3 List (java.util.List)3 HelpOption (org.apache.karaf.shell.impl.action.command.HelpOption)3 TreeMap (java.util.TreeMap)2 Service (org.apache.karaf.shell.api.action.lifecycle.Service)2 DeleteResponse (ddf.catalog.operation.DeleteResponse)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 DeleteRequestImpl (ddf.catalog.operation.impl.DeleteRequestImpl)1 QueryImpl (ddf.catalog.operation.impl.QueryImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1