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  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              // now that we have properties, interpolate all arguments
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 }