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.List;
22  import java.util.Optional;
23  import java.util.function.Consumer;
24  
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.Option;
27  import org.apache.commons.cli.ParseException;
28  import org.apache.maven.api.cli.Options;
29  import org.apache.maven.api.cli.ParserRequest;
30  import org.apache.maven.api.cli.mvnenc.EncryptOptions;
31  import org.apache.maven.cling.invoker.CommonsCliOptions;
32  
33  /**
34   * Implementation of {@link EncryptOptions} (base + mvnenc).
35   */
36  public class CommonsCliEncryptOptions extends CommonsCliOptions implements EncryptOptions {
37      public static CommonsCliEncryptOptions parse(String[] args) throws ParseException {
38          CLIManager cliManager = new CLIManager();
39          return new CommonsCliEncryptOptions(Options.SOURCE_CLI, cliManager, cliManager.parse(args));
40      }
41  
42      protected CommonsCliEncryptOptions(String source, CLIManager cliManager, CommandLine commandLine) {
43          super(source, cliManager, commandLine);
44      }
45  
46      @Override
47      public Optional<Boolean> force() {
48          if (commandLine.hasOption(CLIManager.FORCE)) {
49              return Optional.of(Boolean.TRUE);
50          }
51          return Optional.empty();
52      }
53  
54      @Override
55      public Optional<Boolean> yes() {
56          if (commandLine.hasOption(CLIManager.YES)) {
57              return Optional.of(Boolean.TRUE);
58          }
59          return Optional.empty();
60      }
61  
62      @Override
63      public Optional<List<String>> goals() {
64          if (!commandLine.getArgList().isEmpty()) {
65              return Optional.of(commandLine.getArgList());
66          }
67          return Optional.empty();
68      }
69  
70      @Override
71      public void displayHelp(ParserRequest request, Consumer<String> printStream) {
72          super.displayHelp(request, printStream);
73          printStream.accept("");
74          // we have no DI here (to discover)
75          printStream.accept("Goals:");
76          printStream.accept("  diag - display encryption configuration diagnostic");
77          printStream.accept("  init - wizard to configure encryption (interactive only)");
78          printStream.accept("  encrypt - encrypts input");
79          printStream.accept("  decrypt - decrypts encrypted input");
80          printStream.accept("");
81      }
82  
83      @Override
84      protected CommonsCliEncryptOptions copy(
85              String source, CommonsCliOptions.CLIManager cliManager, CommandLine commandLine) {
86          return new CommonsCliEncryptOptions(source, (CLIManager) cliManager, commandLine);
87      }
88  
89      protected static class CLIManager extends CommonsCliOptions.CLIManager {
90          public static final String FORCE = "f";
91          public static final String YES = "y";
92  
93          @Override
94          protected void prepareOptions(org.apache.commons.cli.Options options) {
95              super.prepareOptions(options);
96              options.addOption(Option.builder(FORCE)
97                      .longOpt("force")
98                      .desc("Should overwrite without asking any configuration?")
99                      .build());
100             options.addOption(Option.builder(YES)
101                     .longOpt("yes")
102                     .desc("Should imply user answered \"yes\" to all incoming questions?")
103                     .build());
104         }
105     }
106 }