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
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.maven.api.cli.Options;
31 import org.apache.maven.api.cli.mvnenc.EncryptOptions;
32 import org.apache.maven.cling.invoker.CommonsCliOptions;
33 import org.codehaus.plexus.interpolation.BasicInterpolator;
34 import org.codehaus.plexus.interpolation.InterpolationException;
35
36 import static org.apache.maven.cling.invoker.Utils.createInterpolator;
37
38 public class CommonsCliEncryptOptions extends CommonsCliOptions implements EncryptOptions {
39 public static CommonsCliEncryptOptions parse(String[] args) throws ParseException {
40 CLIManager cliManager = new CLIManager();
41 return new CommonsCliEncryptOptions(Options.SOURCE_CLI, cliManager, cliManager.parse(args));
42 }
43
44 protected CommonsCliEncryptOptions(String source, CLIManager cliManager, CommandLine commandLine) {
45 super(source, cliManager, commandLine);
46 }
47
48 private static CommonsCliEncryptOptions interpolate(
49 CommonsCliEncryptOptions options, Collection<Map<String, String>> properties) {
50 try {
51
52 BasicInterpolator interpolator = createInterpolator(properties);
53 CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
54 commandLineBuilder.setDeprecatedHandler(o -> {});
55 for (Option option : options.commandLine.getOptions()) {
56 if (!CLIManager.USER_PROPERTY.equals(option.getOpt())) {
57 List<String> values = option.getValuesList();
58 for (ListIterator<String> it = values.listIterator(); it.hasNext(); ) {
59 it.set(interpolator.interpolate(it.next()));
60 }
61 }
62 commandLineBuilder.addOption(option);
63 }
64 for (String arg : options.commandLine.getArgList()) {
65 commandLineBuilder.addArg(interpolator.interpolate(arg));
66 }
67 return new CommonsCliEncryptOptions(
68 options.source, (CLIManager) options.cliManager, commandLineBuilder.build());
69 } catch (InterpolationException e) {
70 throw new IllegalArgumentException("Could not interpolate CommonsCliOptions", e);
71 }
72 }
73
74 @Override
75 public Optional<Boolean> force() {
76 if (commandLine.hasOption(CLIManager.FORCE)) {
77 return Optional.of(Boolean.TRUE);
78 }
79 return Optional.empty();
80 }
81
82 @Override
83 public Optional<Boolean> yes() {
84 if (commandLine.hasOption(CLIManager.YES)) {
85 return Optional.of(Boolean.TRUE);
86 }
87 return Optional.empty();
88 }
89
90 @Override
91 public Optional<List<String>> goals() {
92 if (!commandLine.getArgList().isEmpty()) {
93 return Optional.of(commandLine.getArgList());
94 }
95 return Optional.empty();
96 }
97
98 @Override
99 public EncryptOptions interpolate(Collection<Map<String, String>> properties) {
100 return interpolate(this, properties);
101 }
102
103 protected static class CLIManager extends CommonsCliOptions.CLIManager {
104 public static final String FORCE = "f";
105 public static final String YES = "y";
106
107 @Override
108 protected void prepareOptions(org.apache.commons.cli.Options options) {
109 super.prepareOptions(options);
110 options.addOption(Option.builder(FORCE)
111 .longOpt("force")
112 .desc("Should overwrite without asking any configuration?")
113 .build());
114 options.addOption(Option.builder(YES)
115 .longOpt("yes")
116 .desc("Should imply user answered \"yes\" to all incoming questions?")
117 .build());
118 }
119
120 @Override
121 protected String commandLineSyntax(String command) {
122 return command + " [options] [goal]";
123 }
124 }
125 }