Search in sources :

Example 6 with ReferenceResolver

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

the class CommandProcessor method processCommand.

// visible for testing
int processCommand(final String[] args, final Command command) throws ParseException {
    final CommandLine commandLine = parseCli(command, args);
    if (commandLine == null) {
        out.println("Unable to parse command line");
        return -1;
    }
    try {
        if (args.length == 1 && CommandOption.HELP.getLongName().equalsIgnoreCase(args[0])) {
            command.printUsage(null);
        } else {
            final Result result = command.execute(commandLine);
            if (result instanceof WritableResult) {
                final WritableResult writableResult = (WritableResult) result;
                writableResult.write(out);
            }
            // if the Result is Referenceable then create the resolver and store it in the holder for the next command
            if (result instanceof Referenceable) {
                final Referenceable referenceable = (Referenceable) result;
                final ReferenceResolver referenceResolver = referenceable.createReferenceResolver(context);
                // and can be used again if the current command didn't produce anything to resolve
                if (!referenceResolver.isEmpty()) {
                    backReferenceHolder.set(referenceResolver);
                }
            }
        }
        return 0;
    } catch (Exception e) {
        // so for those we don't need to print the usage every time
        if (e instanceof CommandException) {
            out.println();
            out.println("ERROR: " + e.getMessage());
            out.println();
        } else {
            command.printUsage(e.getMessage());
        }
        if (commandLine.hasOption(CommandOption.VERBOSE.getLongName())) {
            out.println();
            e.printStackTrace(out);
            out.println();
        }
        return -1;
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) Referenceable(org.apache.nifi.toolkit.cli.api.Referenceable) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) WritableResult(org.apache.nifi.toolkit.cli.api.WritableResult) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) ParseException(org.apache.commons.cli.ParseException) WritableResult(org.apache.nifi.toolkit.cli.api.WritableResult) Result(org.apache.nifi.toolkit.cli.api.Result)

Aggregations

ReferenceResolver (org.apache.nifi.toolkit.cli.api.ReferenceResolver)6 ResolvedReference (org.apache.nifi.toolkit.cli.api.ResolvedReference)4 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CommandOption (org.apache.nifi.toolkit.cli.impl.command.CommandOption)3 ParseException (org.apache.commons.cli.ParseException)2 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)2 ArrayList (java.util.ArrayList)1 CommandLine (org.apache.commons.cli.CommandLine)1 Bucket (org.apache.nifi.registry.bucket.Bucket)1 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)1 Context (org.apache.nifi.toolkit.cli.api.Context)1 Referenceable (org.apache.nifi.toolkit.cli.api.Referenceable)1 Result (org.apache.nifi.toolkit.cli.api.Result)1 WritableResult (org.apache.nifi.toolkit.cli.api.WritableResult)1 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)1 Test (org.junit.Test)1