Search in sources :

Example 1 with AnsiParagraphBuilder

use of com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder in project halyard by spinnaker.

the class AbstractRemoteActionCommand method runRemoteAction.

protected void runRemoteAction(OperationHandler<RemoteAction> operation) {
    RemoteAction action = operation.get();
    String scriptPath = action.getScriptPath();
    if (StringUtils.isEmpty(scriptPath)) {
        return;
    }
    boolean shouldRun;
    if (autoRun == null) {
        shouldRun = action.isAutoRun();
    } else {
        shouldRun = action.isAutoRun() && autoRun;
    }
    if (!shouldRun) {
        AnsiStoryBuilder storyBuilder = new AnsiStoryBuilder();
        AnsiParagraphBuilder paragraphBuilder = storyBuilder.addParagraph();
        paragraphBuilder.addSnippet(action.getScriptDescription());
        storyBuilder.addNewline();
        paragraphBuilder = storyBuilder.addParagraph();
        paragraphBuilder.addSnippet("Please run the following script:");
        storyBuilder.addNewline();
        paragraphBuilder = storyBuilder.addParagraph();
        paragraphBuilder.addSnippet(action.getScriptPath()).addStyle(AnsiStyle.UNDERLINE);
        AnsiUi.raw(storyBuilder.toString());
    } else {
        List<String> command = new ArrayList<>();
        command.add(scriptPath);
        JobRequest request = new JobRequest().setTokenizedCommand(command);
        JobExecutor executor = getJobExecutor();
        String jobId = executor.startJobFromStandardStreams(request);
        JobStatus status = null;
        try {
            status = executor.backoffWait(jobId);
        } catch (InterruptedException e) {
            AnsiUi.failure("Interrupted.");
            System.exit(1);
        }
        if (status.getResult() != JobStatus.Result.SUCCESS) {
            AnsiUi.error("Error encountered running script. See above output for more details.");
            System.exit(1);
        }
    }
}
Also used : AnsiParagraphBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) RemoteAction(com.netflix.spinnaker.halyard.core.RemoteAction) ArrayList(java.util.ArrayList) AnsiStoryBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiStoryBuilder)

Example 2 with AnsiParagraphBuilder

use of com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder in project halyard by spinnaker.

the class NestableCommand method formatParameter.

private static void formatParameter(AnsiStoryBuilder story, ParameterDescription parameter, int indentWidth) {
    AnsiParagraphBuilder paragraph = story.addParagraph().setIndentWidth(indentWidth);
    paragraph.addSnippet(parameter.getNames()).addStyle(AnsiStyle.BOLD);
    if (parameter.getDefault() != null) {
        paragraph.addSnippet("=");
        paragraph.addSnippet(parameter.getDefault().toString()).addStyle(AnsiStyle.UNDERLINE);
    }
    if (parameter.getParameter().required()) {
        paragraph.addSnippet(" (required)");
    }
    if (parameter.getParameter().password()) {
        paragraph.addSnippet(" (sensitive data - user will be prompted)");
    }
    paragraph = story.addParagraph().setIndentWidth(indentWidth * 2);
    paragraph.addSnippet(parameter.getDescription());
    story.addNewline();
}
Also used : AnsiParagraphBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder)

Example 3 with AnsiParagraphBuilder

use of com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder in project halyard by spinnaker.

the class NestableCommand method showHelp.

protected void showHelp() {
    AnsiStoryBuilder story = new AnsiStoryBuilder();
    int indentWidth = 2;
    AnsiParagraphBuilder paragraph = story.addParagraph();
    paragraph.addSnippet(getCommandName().toUpperCase()).addStyle(AnsiStyle.BOLD);
    story.addNewline();
    paragraph = story.addParagraph().setIndentWidth(indentWidth);
    String longDescription = getLongDescription() != null ? getLongDescription() : getDescription();
    paragraph.addSnippet(longDescription);
    story.addNewline();
    String usage = fullCommandName;
    if (!commander.getParameters().isEmpty()) {
        usage += " [parameters]";
    }
    if (!subcommands.isEmpty()) {
        usage += " [subcommands]";
    }
    paragraph = story.addParagraph();
    paragraph.addSnippet("USAGE").addStyle(AnsiStyle.BOLD);
    story.addNewline();
    paragraph = story.addParagraph().setIndentWidth(indentWidth);
    paragraph.addSnippet(usage);
    story.addNewline();
    List<ParameterDescription> parameters = commander.getParameters();
    parameters.sort(Comparator.comparing(ParameterDescription::getNames));
    int parameterCount = 0;
    if (!parameters.isEmpty()) {
        paragraph = story.addParagraph();
        paragraph.addSnippet("GLOBAL PARAMETERS").addStyle(AnsiStyle.BOLD);
        story.addNewline();
        for (ParameterDescription parameter : parameters) {
            if (GlobalOptions.isGlobalOption(parameter.getLongestName())) {
                formatParameter(story, parameter, indentWidth);
                parameterCount++;
            }
        }
    }
    if (parameters.size() > parameterCount) {
        paragraph = story.addParagraph();
        paragraph.addSnippet("PARAMETERS").addStyle(AnsiStyle.BOLD);
        story.addNewline();
        ParameterDescription mainParameter = commander.getMainParameter();
        if (mainParameter != null) {
            paragraph = story.addParagraph().setIndentWidth(indentWidth);
            paragraph.addSnippet(getMainParameter().toUpperCase()).addStyle(AnsiStyle.UNDERLINE);
            paragraph = story.addParagraph().setIndentWidth(indentWidth * 2);
            paragraph.addSnippet(mainParameter.getDescription());
            story.addNewline();
        }
        for (ParameterDescription parameter : parameters) {
            if (!GlobalOptions.isGlobalOption(parameter.getLongestName())) {
                formatParameter(story, parameter, indentWidth);
            }
        }
    }
    if (!subcommands.isEmpty()) {
        int maxLen = -1;
        for (String key : subcommands.keySet()) {
            if (key.length() > maxLen) {
                maxLen = key.length();
            }
        }
        paragraph = story.addParagraph();
        paragraph.addSnippet("SUBCOMMANDS").addStyle(AnsiStyle.BOLD);
        story.addNewline();
        List<String> keys = new ArrayList<>(subcommands.keySet());
        keys.sort(String::compareTo);
        for (String key : keys) {
            paragraph = story.addParagraph().setIndentWidth(indentWidth);
            paragraph.addSnippet(key).addStyle(AnsiStyle.BOLD);
            NestableCommand subcommand = subcommands.get(key);
            if (subcommand instanceof DeprecatedCommand) {
                paragraph.addSnippet(" ");
                paragraph.addSnippet("(Deprecated)").addStyle(AnsiStyle.UNDERLINE);
            }
            paragraph = story.addParagraph().setIndentWidth(indentWidth * 2);
            String shortDescription = subcommand.getShortDescription() != null ? subcommand.getShortDescription() : subcommand.getDescription();
            paragraph.addSnippet(shortDescription);
            story.addNewline();
        }
    }
    AnsiPrinter.out.println(story.toString());
}
Also used : AnsiParagraphBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder) ArrayList(java.util.ArrayList) ParameterDescription(com.beust.jcommander.ParameterDescription) AnsiStoryBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiStoryBuilder)

Aggregations

AnsiParagraphBuilder (com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder)3 AnsiStoryBuilder (com.netflix.spinnaker.halyard.cli.ui.v1.AnsiStoryBuilder)2 ArrayList (java.util.ArrayList)2 ParameterDescription (com.beust.jcommander.ParameterDescription)1 RemoteAction (com.netflix.spinnaker.halyard.core.RemoteAction)1 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)1 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)1 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)1