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.io.InterruptedIOException;
22  import java.util.ArrayList;
23  import java.util.function.Consumer;
24  
25  import org.apache.maven.api.annotations.Nullable;
26  import org.apache.maven.api.cli.InvokerRequest;
27  import org.apache.maven.api.cli.mvnenc.EncryptOptions;
28  import org.apache.maven.api.services.Lookup;
29  import org.apache.maven.cling.invoker.LookupContext;
30  import org.apache.maven.cling.invoker.LookupInvoker;
31  import org.apache.maven.cling.utils.CLIReportingUtils;
32  import org.jline.reader.LineReaderBuilder;
33  import org.jline.reader.UserInterruptException;
34  import org.jline.terminal.Terminal;
35  import org.jline.utils.AttributedStyle;
36  import org.jline.utils.Colors;
37  
38  /**
39   * mvnenc invoker implementation.
40   */
41  public class EncryptInvoker extends LookupInvoker<EncryptContext> {
42  
43      public static final int OK = 0; // OK
44      public static final int ERROR = 1; // "generic" error
45      public static final int BAD_OPERATION = 2; // bad user input or alike
46      public static final int CANCELED = 3; // user canceled
47  
48      public EncryptInvoker(Lookup protoLookup) {
49          this(protoLookup, null);
50      }
51  
52      public EncryptInvoker(Lookup protoLookup, @Nullable Consumer<LookupContext> contextConsumer) {
53          super(protoLookup, contextConsumer);
54      }
55  
56      @Override
57      protected EncryptContext createContext(InvokerRequest invokerRequest) {
58          return new EncryptContext(invokerRequest);
59      }
60  
61      @Override
62      protected void lookup(EncryptContext context) throws Exception {
63          if (context.goals == null) {
64              super.lookup(context);
65              context.goals = context.lookup.lookupMap(Goal.class);
66          }
67      }
68  
69      @Override
70      protected int execute(EncryptContext context) throws Exception {
71          try {
72              context.header = new ArrayList<>();
73              context.style = new AttributedStyle();
74              context.addInHeader(
75                      context.style.italic().bold().foreground(Colors.rgbColor("green")),
76                      "Maven Encryption " + CLIReportingUtils.showVersionMinimal());
77              context.addInHeader("Tool for secure password management on workstations.");
78              context.addInHeader("This tool is part of Apache Maven 4 distribution.");
79              context.addInHeader("");
80  
81              context.terminal.handle(
82                      Terminal.Signal.INT, signal -> Thread.currentThread().interrupt());
83  
84              context.reader =
85                      LineReaderBuilder.builder().terminal(context.terminal).build();
86  
87              EncryptOptions options = (EncryptOptions) context.invokerRequest.options();
88              if (options.goals().isEmpty() || options.goals().get().size() != 1) {
89                  return badGoalsErrorMessage("No goal or multiple goals specified, specify only one goal.", context);
90              }
91  
92              String goalName = options.goals().get().get(0);
93              Goal goal = context.goals.get(goalName);
94  
95              if (goal == null) {
96                  return badGoalsErrorMessage("Unknown goal: " + goalName, context);
97              }
98  
99              return goal.execute(context);
100         } catch (InterruptedException | InterruptedIOException | UserInterruptException e) {
101             context.logger.error("Goal canceled by user.");
102             return CANCELED;
103         } catch (Exception e) {
104             if (context.invokerRequest.options().showErrors().orElse(false)) {
105                 context.logger.error(e.getMessage(), e);
106             } else {
107                 context.logger.error(e.getMessage());
108             }
109             return ERROR;
110         }
111     }
112 
113     protected int badGoalsErrorMessage(String message, EncryptContext context) {
114         context.logger.error(message);
115         context.logger.error("Supported goals are: " + String.join(", ", context.goals.keySet()));
116         context.logger.error("Use -h to display help.");
117         return BAD_OPERATION;
118     }
119 }