Search in sources :

Example 1 with Context

use of org.apache.nifi.toolkit.cli.api.Context 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 Context

use of org.apache.nifi.toolkit.cli.api.Context 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 Context

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

the class TestCommandProcessor method testCommandProcessor.

@Test
public void testCommandProcessor() throws ParseException {
    final List<String> results = new ArrayList<>();
    results.add("foo1");
    results.add("foo2");
    final CommandA command = new CommandA(results);
    final Context context = Mockito.mock(Context.class);
    Mockito.when(context.getOutput()).thenReturn(System.out);
    // run the command once to set the previous results
    final CommandProcessor processor = new CommandProcessor(Collections.emptyMap(), Collections.emptyMap(), context);
    processor.processCommand(new String[] {}, command);
    // run it again and &1 should be resolved to foo1
    processor.processCommand(new String[] { "-" + CommandOption.BUCKET_ID.getShortName(), "&1" }, command);
    final CommandLine cli1 = command.getCli();
    Assert.assertEquals("foo1", cli1.getOptionValue(CommandOption.BUCKET_ID.getShortName()));
    // run it again and &2 should be resolved to foo1
    processor.processCommand(new String[] { "-" + CommandOption.BUCKET_ID.getShortName(), "&2" }, command);
    final CommandLine cli2 = command.getCli();
    Assert.assertEquals("foo2", cli2.getOptionValue(CommandOption.BUCKET_ID.getShortName()));
    // run it again and &1 should be resolved to foo1
    processor.processCommand(new String[] { "-" + CommandOption.BUCKET_ID.getShortName(), "b1", "-" + CommandOption.FLOW_ID.getShortName(), "&1" }, command);
    final CommandLine cli3 = command.getCli();
    Assert.assertEquals("foo1", cli3.getOptionValue(CommandOption.FLOW_ID.getShortName()));
}
Also used : Context(org.apache.nifi.toolkit.cli.api.Context) CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with Context

use of org.apache.nifi.toolkit.cli.api.Context 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 Context

use of org.apache.nifi.toolkit.cli.api.Context 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

Context (org.apache.nifi.toolkit.cli.api.Context)5 Command (org.apache.nifi.toolkit.cli.api.Command)4 CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)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 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 CommandLine (org.apache.commons.cli.CommandLine)1 NiFiRegistryCommandGroup (org.apache.nifi.toolkit.cli.impl.command.registry.NiFiRegistryCommandGroup)1 Completer (org.jline.reader.Completer)1 EndOfFileException (org.jline.reader.EndOfFileException)1 ParsedLine (org.jline.reader.ParsedLine)1 UserInterruptException (org.jline.reader.UserInterruptException)1