summaryrefslogtreecommitdiffstats
path: root/security-tools/src/main/java/com/yahoo/vespa/security/tool/securityenv/CliOptions.java
blob: 38c6483a184bcaa4ac907dd971568146424b70df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.security.tool.securityenv;

import com.yahoo.security.tls.TransportSecurityUtils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;

import static java.util.stream.Collectors.joining;

/**
 * Defines the program's command line parameters.
 *
 * @author bjorncs
 */
class CliOptions {
    static final String SHELL_OPTION = "shell";
    static final String HELP_OPTION = "help";

    private static final Options OPTIONS = new Options()
            .addOption(
                    Option.builder("s")
                            .longOpt(SHELL_OPTION)
                            .hasArg(true)
                            .required(false)
                            .desc(String.format("Shell type. Shell type is auto-detected if option not present. Valid values: %s.",
                                                Arrays.stream(UnixShell.values())
                                                        .map(shell -> String.format("'%s'", shell.configName()))
                                                        .collect(joining(", ", "[", "]"))))
                            .build())
            .addOption(Option.builder("h")
                               .longOpt(HELP_OPTION)
                               .hasArg(false)
                               .required(false)
                               .desc("Show help")
                               .build());

    static CommandLine parseCliArguments(String[] cliArgs) throws ParseException {
        CommandLineParser parser = new DefaultParser();
        return parser.parse(OPTIONS, cliArgs);
    }

    static void printHelp(PrintStream out) {
        HelpFormatter formatter = new HelpFormatter();
        PrintWriter writer = new PrintWriter(out);
        formatter.printHelp(
                writer,
                formatter.getWidth(),
                "vespa-security-env <options>",
                String.format("Generates shell commands that defines environments variables based on the content of %s.",
                              TransportSecurityUtils.CONFIG_FILE_ENVIRONMENT_VARIABLE),
                OPTIONS,
                formatter.getLeftPadding(),
                formatter.getDescPadding(),
                String.format("The output may include the following variables:\n%s\n",
                              Arrays.stream(OutputVariable.values())
                                      .map(variable -> String.format(" - '%s': %s", variable.variableName(), variable.description()))
                                      .collect(joining("\n"))));
        writer.flush();
    }
}