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