View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Implementation of {@link EncryptOptions} (base + mvnenc).
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              // now that we have properties, interpolate all arguments
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         // we have no DI here (to discover)
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 }