Search in sources :

Example 1 with ExitShellRequest

use of org.springframework.shell.core.ExitShellRequest in project geode by apache.

the class Launcher method parseCommandLineCommand.

private int parseCommandLineCommand(final String... args) {
    Gfsh gfsh = null;
    try {
        gfsh = Gfsh.getInstance(false, args, new GfshConfig());
        this.startupTimeLogHelper.logStartupTime();
    } catch (ClassNotFoundException cnfex) {
        log(cnfex, gfsh);
    } catch (IOException ioex) {
        log(ioex, gfsh);
    } catch (IllegalStateException isex) {
        System.err.println("ERROR : " + isex.getMessage());
    }
    ExitShellRequest exitRequest = ExitShellRequest.NORMAL_EXIT;
    if (gfsh != null) {
        final String commandLineCommand = combineStrings(args);
        if (commandLineCommand.startsWith(HELP)) {
            if (commandLineCommand.equals(HELP)) {
                printUsage(gfsh, System.out);
            } else {
                // help is also available for commands which are not available under
                // allowedCommandLineCommands
                gfsh.executeCommand(commandLineCommand);
            }
        } else {
            boolean commandIsAllowed = false;
            for (String allowedCommandLineCommand : this.allowedCommandLineCommands) {
                if (commandLineCommand.startsWith(allowedCommandLineCommand)) {
                    commandIsAllowed = true;
                    break;
                }
            }
            if (!commandIsAllowed) {
                System.err.println(CliStrings.format(MSG_INVALID_COMMAND_OR_OPTION, CliUtil.arrayToString(args)));
                exitRequest = ExitShellRequest.FATAL_EXIT;
            } else {
                if (!gfsh.executeScriptLine(commandLineCommand)) {
                    if (gfsh.getLastExecutionStatus() != 0)
                        exitRequest = ExitShellRequest.FATAL_EXIT;
                } else if (gfsh.getLastExecutionStatus() != 0) {
                    exitRequest = ExitShellRequest.FATAL_EXIT;
                }
            }
        }
    }
    return exitRequest.getExitCode();
}
Also used : GfshConfig(org.apache.geode.management.internal.cli.shell.GfshConfig) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) IOException(java.io.IOException) ExitShellRequest(org.springframework.shell.core.ExitShellRequest)

Example 2 with ExitShellRequest

use of org.springframework.shell.core.ExitShellRequest in project geode by apache.

the class Launcher method parseOptions.

private int parseOptions(final String... args) {
    OptionSet parsedOptions;
    try {
        parsedOptions = this.commandLineParser.parse(args);
    } catch (OptionException e) {
        System.err.println(CliStrings.format(MSG_INVALID_COMMAND_OR_OPTION, CliUtil.arrayToString(args)));
        return ExitShellRequest.FATAL_EXIT.getExitCode();
    }
    boolean launchShell = true;
    boolean onlyPrintUsage = parsedOptions.has(HELP_OPTION);
    if (parsedOptions.has(EXECUTE_OPTION) || onlyPrintUsage) {
        launchShell = false;
    }
    Gfsh gfsh = null;
    try {
        gfsh = Gfsh.getInstance(launchShell, args, new GfshConfig());
        this.startupTimeLogHelper.logStartupTime();
    } catch (ClassNotFoundException cnfex) {
        log(cnfex, gfsh);
    } catch (IOException ioex) {
        log(ioex, gfsh);
    } catch (IllegalStateException isex) {
        System.err.println("ERROR : " + isex.getMessage());
    }
    ExitShellRequest exitRequest = ExitShellRequest.NORMAL_EXIT;
    if (gfsh != null) {
        try {
            if (launchShell) {
                gfsh.start();
                gfsh.waitForComplete();
                exitRequest = gfsh.getExitShellRequest();
            } else if (onlyPrintUsage) {
                printUsage(gfsh, System.out);
            } else {
                @SuppressWarnings("unchecked") List<String> commandsToExecute = (List<String>) parsedOptions.valuesOf(EXECUTE_OPTION);
                // Execute all of the commands in the list, one at a time.
                for (int i = 0; i < commandsToExecute.size() && exitRequest == ExitShellRequest.NORMAL_EXIT; i++) {
                    String command = commandsToExecute.get(i);
                    // sanitize the output string to not show the password
                    System.out.println(GfshParser.LINE_SEPARATOR + "(" + (i + 1) + ") Executing - " + GfshHistory.redact(command) + GfshParser.LINE_SEPARATOR);
                    if (!gfsh.executeScriptLine(command) || gfsh.getLastExecutionStatus() != 0) {
                        exitRequest = ExitShellRequest.FATAL_EXIT;
                    }
                }
            }
        } catch (InterruptedException iex) {
            log(iex, gfsh);
        }
    }
    return exitRequest.getExitCode();
}
Also used : GfshConfig(org.apache.geode.management.internal.cli.shell.GfshConfig) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) OptionException(joptsimple.OptionException) List(java.util.List) IOException(java.io.IOException) OptionSet(joptsimple.OptionSet) ExitShellRequest(org.springframework.shell.core.ExitShellRequest)

Example 3 with ExitShellRequest

use of org.springframework.shell.core.ExitShellRequest in project geode by apache.

the class ShellCommands method exit.

@CliCommand(value = { CliStrings.EXIT, "quit" }, help = CliStrings.EXIT__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public ExitShellRequest exit() throws IOException {
    Gfsh gfshInstance = getGfsh();
    gfshInstance.stop();
    ExitShellRequest exitShellRequest = gfshInstance.getExitShellRequest();
    if (exitShellRequest == null) {
        // shouldn't really happen, but we'll fallback to this anyway
        exitShellRequest = ExitShellRequest.NORMAL_EXIT;
    }
    return exitShellRequest;
}
Also used : Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) ExitShellRequest(org.springframework.shell.core.ExitShellRequest) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)3 ExitShellRequest (org.springframework.shell.core.ExitShellRequest)3 IOException (java.io.IOException)2 GfshConfig (org.apache.geode.management.internal.cli.shell.GfshConfig)2 List (java.util.List)1 OptionException (joptsimple.OptionException)1 OptionSet (joptsimple.OptionSet)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1