View Javadoc
1   package org.apache.maven.plugins.gpg;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.codehaus.plexus.util.Os;
27  import org.codehaus.plexus.util.StringUtils;
28  import org.codehaus.plexus.util.cli.CommandLineException;
29  import org.codehaus.plexus.util.cli.CommandLineUtils;
30  import org.codehaus.plexus.util.cli.Commandline;
31  import org.codehaus.plexus.util.cli.StreamConsumer;
32  
33  /**
34   * Parse the output of <code>gpg --version</code> and exposes these as dedicated objects.
35   * 
36   * Supported:
37   * <ul>
38   *   <li>gpg version, i.e. gpg (GnuPG) 2.2.15</li>
39   * </ul>
40   * Unsupported:
41   * <ul>
42   *   <li>libgcrypt version</li>
43   *   <li>Home</li>
44   *   <li>Supported algorithms (Pubkey, Cipher, Hash, Compression)</li>
45   * </ul>
46   * 
47   * @author Robert Scholte
48   * @since 3.0.0
49   */
50  public class GpgVersionParser
51  {
52      private final GpgVersionConsumer consumer;
53  
54      private GpgVersionParser( GpgVersionConsumer consumer )
55      {
56          this.consumer = consumer;
57  
58      }
59  
60      public static GpgVersionParser parse( String executable )
61      {
62          Commandline cmd = new Commandline();
63  
64          if ( StringUtils.isNotEmpty( executable ) )
65          {
66              cmd.setExecutable( executable );
67          }
68          else
69          {
70              cmd.setExecutable( "gpg" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ) );
71          }
72  
73  
74          cmd.createArg().setValue( "--version" );
75  
76          GpgVersionConsumer out = new GpgVersionConsumer();
77  
78          try
79          {
80             CommandLineUtils.executeCommandLine( cmd, null, out, null );
81          }
82          catch ( CommandLineException e )
83          {
84              // TODO probably a dedicated exception
85          }
86  
87          return new GpgVersionParser( out );
88      }
89  
90      public GpgVersion getGpgVersion()
91      {
92          return consumer.getGpgVersion();
93  
94      }
95  
96      /**
97       * Consumes the output of {@code gpg --version}
98       *
99       * @author Robert Scholte
100      * @since 3.0.0
101      */
102     static class GpgVersionConsumer
103         implements StreamConsumer
104     {
105         private final Pattern gpgVersionPattern = Pattern.compile( "gpg \\([^)]+\\) .+" );
106 
107         private GpgVersion gpgVersion;
108 
109         @Override
110         public void consumeLine( String line )
111             throws IOException
112         {
113             Matcher m = gpgVersionPattern.matcher( line );
114             if ( m.matches() )
115             {
116                 gpgVersion = GpgVersion.parse( m.group() );
117             }
118         }
119         
120         public GpgVersion getGpgVersion()
121         {
122             return gpgVersion;
123         }
124     }
125 
126 }