Search in sources :

Example 1 with GfshConfig

use of org.apache.geode.management.internal.cli.shell.GfshConfig 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 GfshConfig

use of org.apache.geode.management.internal.cli.shell.GfshConfig 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)

Aggregations

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