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.plugins.gpg;
20  
21  import java.io.IOException;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.codehaus.plexus.util.Os;
27  import org.codehaus.plexus.util.cli.CommandLineException;
28  import org.codehaus.plexus.util.cli.CommandLineUtils;
29  import org.codehaus.plexus.util.cli.Commandline;
30  import org.codehaus.plexus.util.cli.StreamConsumer;
31  
32  /**
33   * Parse the output of <code>gpg --version</code> and exposes these as dedicated objects.
34   *
35   * Supported:
36   * <ul>
37   *   <li>gpg version, i.e. gpg (GnuPG) 2.2.15</li>
38   * </ul>
39   * Unsupported:
40   * <ul>
41   *   <li>libgcrypt version</li>
42   *   <li>Home</li>
43   *   <li>Supported algorithms (Pubkey, Cipher, Hash, Compression)</li>
44   * </ul>
45   *
46   * @author Robert Scholte
47   * @since 3.0.0
48   */
49  public class GpgVersionParser {
50      private final GpgVersionConsumer consumer;
51  
52      private GpgVersionParser(GpgVersionConsumer consumer) {
53          this.consumer = consumer;
54      }
55  
56      public static GpgVersionParser parse(String executable) throws MojoExecutionException {
57          Commandline cmd = new Commandline();
58  
59          if (executable != null && !executable.isEmpty()) {
60              cmd.setExecutable(executable);
61          } else {
62              cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
63          }
64  
65          cmd.createArg().setValue("--version");
66  
67          GpgVersionConsumer out = new GpgVersionConsumer();
68  
69          try {
70              CommandLineUtils.executeCommandLine(cmd, null, out, null);
71          } catch (CommandLineException e) {
72              throw new MojoExecutionException("failed to execute gpg", e);
73          }
74  
75          return new GpgVersionParser(out);
76      }
77  
78      public GpgVersion getGpgVersion() {
79          return consumer.getGpgVersion();
80      }
81  
82      /**
83       * Consumes the output of {@code gpg --version}
84       *
85       * @author Robert Scholte
86       * @since 3.0.0
87       */
88      static class GpgVersionConsumer implements StreamConsumer {
89          private final Pattern gpgVersionPattern = Pattern.compile("gpg \\([^)]+\\) .+");
90  
91          private GpgVersion gpgVersion;
92  
93          @Override
94          public void consumeLine(String line) throws IOException {
95              Matcher m = gpgVersionPattern.matcher(line);
96              if (m.matches()) {
97                  gpgVersion = GpgVersion.parse(m.group());
98              }
99          }
100 
101         public GpgVersion getGpgVersion() {
102             return gpgVersion;
103         }
104     }
105 }