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;
}
}
}
}
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);
}
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()));
}
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);
}
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);
}
Aggregations