Search in sources :

Example 1 with CommandGroup

use of org.apache.nifi.toolkit.cli.api.CommandGroup in project nifi by apache.

the class CLIMain method runInteractiveCLI.

/**
 * Runs the interactive CLI.
 *
 * @throws IOException if an error occurs
 */
private static void runInteractiveCLI() throws IOException {
    // Logger.getLogger("org.jline").setLevel(Level.FINE);
    try (final Terminal terminal = TerminalBuilder.builder().name(SHELL_NAME).system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
        final PrintStream output = new PrintStream(terminal.output(), true)) {
        printHeader(BANNER_FILE, output);
        final Context context = createContext(output, true);
        final Map<String, Command> topLevelCommands = CommandFactory.createTopLevelCommands(context);
        final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
        final CommandProcessor commandProcessor = new CommandProcessor(topLevelCommands, commandGroups, context);
        final Completer completer = new CLICompleter(topLevelCommands.values(), commandGroups.values());
        final LineReader reader = LineReaderBuilder.builder().appName(SHELL_NAME).terminal(terminal).completer(completer).build();
        reader.setOpt(LineReader.Option.AUTO_FRESH_LINE);
        reader.unsetOpt(LineReader.Option.INSERT_TAB);
        while (true) {
            try {
                final String line = reader.readLine(PROMPT);
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                final ParsedLine parsedLine = reader.getParsedLine();
                final String[] parsedArgs = parsedLine.words().toArray(new String[parsedLine.words().size()]);
                commandProcessor.process(parsedArgs);
            } catch (UserInterruptException e) {
            // Ignore
            } catch (EndOfFileException e) {
                return;
            }
        }
    }
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) PrintStream(java.io.PrintStream) EndOfFileException(org.jline.reader.EndOfFileException) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) Completer(org.jline.reader.Completer) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal) Command(org.apache.nifi.toolkit.cli.api.Command) LineReader(org.jline.reader.LineReader) ParsedLine(org.jline.reader.ParsedLine) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)

Example 2 with CommandGroup

use of org.apache.nifi.toolkit.cli.api.CommandGroup in project nifi by apache.

the class CommandProcessor method processGroupCommand.

private int processGroupCommand(final String commandGroupStr, final String[] args) {
    if (args.length <= 1) {
        printBasicUsage("No command provided to " + commandGroupStr);
        return -1;
    }
    final String commandStr = args[1];
    final CommandGroup commandGroup = commandGroups.get(commandGroupStr);
    final Command command = commandGroup.getCommands().stream().filter(c -> c.getName().equals(commandStr)).findFirst().orElse(null);
    if (command == null) {
        printBasicUsage("Unknown command '" + commandGroupStr + " " + commandStr + "'");
        return -1;
    }
    try {
        final String[] otherArgs = Arrays.copyOfRange(args, 2, args.length, String[].class);
        return processCommand(otherArgs, command);
    } catch (Exception e) {
        command.printUsage(e.getMessage());
        return -1;
    }
}
Also used : Command(org.apache.nifi.toolkit.cli.api.Command) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) ParseException(org.apache.commons.cli.ParseException)

Example 3 with CommandGroup

use of org.apache.nifi.toolkit.cli.api.CommandGroup in project nifi by apache.

the class NiFiCLIMainRunner method main.

public static void main(String[] args) {
    final String[] cmdArgs = ("registry list-buckets help " + "").split("[ ]");
    final Session session = new InMemorySession();
    final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
    final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
    final Context context = new StandardContext.Builder().output(System.out).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).build();
    final Map<String, Command> commands = CommandFactory.createTopLevelCommands(context);
    final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
    final CommandProcessor processor = new CommandProcessor(commands, commandGroups, context);
    processor.process(cmdArgs);
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) NiFiClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) Command(org.apache.nifi.toolkit.cli.api.Command) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession)

Example 4 with CommandGroup

use of org.apache.nifi.toolkit.cli.api.CommandGroup in project nifi by apache.

the class TestCLICompleter method setupCompleter.

@BeforeClass
public static void setupCompleter() {
    final Session session = new InMemorySession();
    final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
    final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
    final Context context = new StandardContext.Builder().output(System.out).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).build();
    final Map<String, Command> commands = CommandFactory.createTopLevelCommands(context);
    final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
    completer = new CLICompleter(commands.values(), commandGroups.values());
    lineReader = Mockito.mock(LineReader.class);
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) NiFiClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory) NiFiRegistryCommandGroup(org.apache.nifi.toolkit.cli.impl.command.registry.NiFiRegistryCommandGroup) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) Command(org.apache.nifi.toolkit.cli.api.Command) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) LineReader(org.jline.reader.LineReader) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) BeforeClass(org.junit.BeforeClass)

Example 5 with CommandGroup

use of org.apache.nifi.toolkit.cli.api.CommandGroup in project nifi by apache.

the class CLIMain method runSingleCommand.

/**
 * Handles running a single command and exiting, non-interactive mode.
 *
 * @param args the args passed in from the command line
 */
private static int runSingleCommand(final String[] args) {
    final Context context = createContext(System.out, false);
    final Map<String, Command> topLevelCommands = CommandFactory.createTopLevelCommands(context);
    final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
    final CommandProcessor commandProcessor = new CommandProcessor(topLevelCommands, commandGroups, context);
    return commandProcessor.process(args);
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) Command(org.apache.nifi.toolkit.cli.api.Command) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)

Aggregations

CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)6 Command (org.apache.nifi.toolkit.cli.api.Command)5 Context (org.apache.nifi.toolkit.cli.api.Context)4 StandardContext (org.apache.nifi.toolkit.cli.impl.context.StandardContext)4 CommandProcessor (org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)3 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)2 Session (org.apache.nifi.toolkit.cli.api.Session)2 NiFiClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory)2 NiFiRegistryClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory)2 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)2 NiFiRegistryCommandGroup (org.apache.nifi.toolkit.cli.impl.command.registry.NiFiRegistryCommandGroup)2 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)2 LineReader (org.jline.reader.LineReader)2 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 ParseException (org.apache.commons.cli.ParseException)1 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)1 DemoCommandGroup (org.apache.nifi.toolkit.cli.impl.command.composite.DemoCommandGroup)1 NiFiCommandGroup (org.apache.nifi.toolkit.cli.impl.command.nifi.NiFiCommandGroup)1