Search in sources :

Example 1 with CommandProcessor

use of org.apache.nifi.toolkit.cli.impl.command.CommandProcessor 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 CommandProcessor

use of org.apache.nifi.toolkit.cli.impl.command.CommandProcessor 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 3 with CommandProcessor

use of org.apache.nifi.toolkit.cli.impl.command.CommandProcessor 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

Command (org.apache.nifi.toolkit.cli.api.Command)3 CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)3 Context (org.apache.nifi.toolkit.cli.api.Context)3 CommandProcessor (org.apache.nifi.toolkit.cli.impl.command.CommandProcessor)3 StandardContext (org.apache.nifi.toolkit.cli.impl.context.StandardContext)3 PrintStream (java.io.PrintStream)1 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)1 Session (org.apache.nifi.toolkit.cli.api.Session)1 NiFiClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory)1 NiFiRegistryClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory)1 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)1 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)1 Completer (org.jline.reader.Completer)1 EndOfFileException (org.jline.reader.EndOfFileException)1 LineReader (org.jline.reader.LineReader)1 ParsedLine (org.jline.reader.ParsedLine)1 UserInterruptException (org.jline.reader.UserInterruptException)1 Terminal (org.jline.terminal.Terminal)1