Search in sources :

Example 6 with CommandException

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

the class UpdateRegistryClient method doExecute.

@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
    final ControllerClient controllerClient = client.getControllerClient();
    final String id = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
    final RegistryClientEntity existingRegClient = controllerClient.getRegistryClient(id);
    if (existingRegClient == null) {
        throw new CommandException("Registry client does not exist for id " + id);
    }
    final String name = getArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
    final String url = getArg(properties, CommandOption.REGISTRY_CLIENT_URL);
    final String desc = getArg(properties, CommandOption.REGISTRY_CLIENT_DESC);
    if (StringUtils.isBlank(name) && StringUtils.isBlank(url) && StringUtils.isBlank(desc)) {
        throw new CommandException("Name, url, and desc were all blank, nothing to update");
    }
    if (StringUtils.isNotBlank(name)) {
        existingRegClient.getComponent().setName(name);
    }
    if (StringUtils.isNotBlank(url)) {
        existingRegClient.getComponent().setUri(url);
    }
    if (StringUtils.isNotBlank(desc)) {
        existingRegClient.getComponent().setDescription(desc);
    }
    final String clientId = getContext().getSession().getNiFiClientID();
    existingRegClient.getRevision().setClientId(clientId);
    controllerClient.updateRegistryClient(existingRegClient);
    return VoidResult.getInstance();
}
Also used : ControllerClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ControllerClient) RegistryClientEntity(org.apache.nifi.web.api.entity.RegistryClientEntity) CommandException(org.apache.nifi.toolkit.cli.api.CommandException)

Example 7 with CommandException

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

CommandException (org.apache.nifi.toolkit.cli.api.CommandException)7 Properties (java.util.Properties)3 Session (org.apache.nifi.toolkit.cli.api.Session)3 SessionException (org.apache.nifi.toolkit.cli.api.SessionException)3 IOException (java.io.IOException)2 ParseException (org.apache.commons.cli.ParseException)2 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)2 NiFiClientException (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 StringUtils (org.apache.commons.lang3.StringUtils)1 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)1 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)1 Context (org.apache.nifi.toolkit.cli.api.Context)1 ReferenceResolver (org.apache.nifi.toolkit.cli.api.ReferenceResolver)1 Referenceable (org.apache.nifi.toolkit.cli.api.Referenceable)1 Result (org.apache.nifi.toolkit.cli.api.Result)1