use of org.apache.nifi.toolkit.cli.api.Command 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.Command in project nifi by apache.
the class CommandFactory method createTopLevelCommands.
public static Map<String, Command> createTopLevelCommands(final Context context) {
final List<Command> commandList = new ArrayList<>();
commandList.add(new Help());
commandList.add(new Exit());
final Map<String, Command> commandMap = new TreeMap<>();
commandList.stream().forEach(cmd -> {
cmd.initialize(context);
commandMap.put(cmd.getName(), cmd);
});
return Collections.unmodifiableMap(commandMap);
}
use of org.apache.nifi.toolkit.cli.api.Command 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;
}
}
use of org.apache.nifi.toolkit.cli.api.Command 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.Command 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);
}
Aggregations