1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker.mvnenc;
20
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.function.Consumer;
27
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.ParseException;
31 import org.apache.maven.api.cli.Options;
32 import org.apache.maven.api.cli.ParserRequest;
33 import org.apache.maven.api.cli.mvnenc.EncryptOptions;
34 import org.apache.maven.cling.invoker.CommonsCliOptions;
35 import org.codehaus.plexus.interpolation.BasicInterpolator;
36 import org.codehaus.plexus.interpolation.InterpolationException;
37
38 import static org.apache.maven.cling.invoker.Utils.createInterpolator;
39
40
41
42
43 public class CommonsCliEncryptOptions extends CommonsCliOptions implements EncryptOptions {
44 public static CommonsCliEncryptOptions parse(String[] args) throws ParseException {
45 CLIManager cliManager = new CLIManager();
46 return new CommonsCliEncryptOptions(Options.SOURCE_CLI, cliManager, cliManager.parse(args));
47 }
48
49 protected CommonsCliEncryptOptions(String source, CLIManager cliManager, CommandLine commandLine) {
50 super(source, cliManager, commandLine);
51 }
52
53 private static CommonsCliEncryptOptions interpolate(
54 CommonsCliEncryptOptions options, Collection<Map<String, String>> properties) {
55 try {
56
57 BasicInterpolator interpolator = createInterpolator(properties);
58 CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
59 commandLineBuilder.setDeprecatedHandler(o -> {});
60 for (Option option : options.commandLine.getOptions()) {
61 if (!CLIManager.USER_PROPERTY.equals(option.getOpt())) {
62 List<String> values = option.getValuesList();
63 for (ListIterator<String> it = values.listIterator(); it.hasNext(); ) {
64 it.set(interpolator.interpolate(it.next()));
65 }
66 }
67 commandLineBuilder.addOption(option);
68 }
69 for (String arg : options.commandLine.getArgList()) {
70 commandLineBuilder.addArg(interpolator.interpolate(arg));
71 }
72 return new CommonsCliEncryptOptions(
73 options.source, (CLIManager) options.cliManager, commandLineBuilder.build());
74 } catch (InterpolationException e) {
75 throw new IllegalArgumentException("Could not interpolate CommonsCliOptions", e);
76 }
77 }
78
79 @Override
80 public Optional<Boolean> force() {
81 if (commandLine.hasOption(CLIManager.FORCE)) {
82 return Optional.of(Boolean.TRUE);
83 }
84 return Optional.empty();
85 }
86
87 @Override
88 public Optional<Boolean> yes() {
89 if (commandLine.hasOption(CLIManager.YES)) {
90 return Optional.of(Boolean.TRUE);
91 }
92 return Optional.empty();
93 }
94
95 @Override
96 public Optional<List<String>> goals() {
97 if (!commandLine.getArgList().isEmpty()) {
98 return Optional.of(commandLine.getArgList());
99 }
100 return Optional.empty();
101 }
102
103 @Override
104 public EncryptOptions interpolate(Collection<Map<String, String>> properties) {
105 return interpolate(this, properties);
106 }
107
108 @Override
109 public void displayHelp(ParserRequest request, Consumer<String> printStream) {
110 super.displayHelp(request, printStream);
111 printStream.accept("");
112
113 printStream.accept("Goals:");
114 printStream.accept(" diag - display encryption configuration diagnostic");
115 printStream.accept(" init - wizard to configure encryption (interactive only)");
116 printStream.accept(" encrypt - encrypts input");
117 printStream.accept(" decrypt - decrypts encrypted input");
118 printStream.accept("");
119 }
120
121 protected static class CLIManager extends CommonsCliOptions.CLIManager {
122 public static final String FORCE = "f";
123 public static final String YES = "y";
124
125 @Override
126 protected void prepareOptions(org.apache.commons.cli.Options options) {
127 super.prepareOptions(options);
128 options.addOption(Option.builder(FORCE)
129 .longOpt("force")
130 .desc("Should overwrite without asking any configuration?")
131 .build());
132 options.addOption(Option.builder(YES)
133 .longOpt("yes")
134 .desc("Should imply user answered \"yes\" to all incoming questions?")
135 .build());
136 }
137 }
138 }