1
1
package org .jfrog .build .extractor .go .extractor ;
2
2
3
+ import com .github .zafarkhaja .semver .Version ;
3
4
import org .apache .commons .lang3 .StringUtils ;
4
5
import org .jfrog .build .api .util .Log ;
5
6
6
7
import java .io .File ;
7
- import java .util .regex .Matcher ;
8
- import java .util .regex .Pattern ;
8
+ import java .util .Optional ;
9
9
10
10
/**
11
11
* @author BarakH
@@ -14,10 +14,9 @@ public class GoVersionUtils {
14
14
15
15
public static final String INCOMPATIBLE = "+incompatible" ;
16
16
public static final int ZERO_OR_ONE = 0 ;
17
- // The regular expression used here is derived from the SemVer specification: https://semver.org/
18
- protected static final Pattern VERSION_PATTERN = Pattern .compile (
19
- "v(0|[1-9]\\ d*)\\ .(0|[1-9]\\ d*)\\ .(0|[1-9]\\ d*)(?:-((?:0|[1-9]\\ d*|\\ d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\ ." +
20
- "(?:0|[1-9]\\ d*|\\ d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\ +([0-9a-zA-Z-]+(?:\\ .[0-9a-zA-Z-]+)*))?$" );
17
+
18
+ private GoVersionUtils () {
19
+ }
21
20
22
21
/**
23
22
* @param version full version string
@@ -28,16 +27,11 @@ public static int getMajorVersion(String version, Log log) {
28
27
return 0 ;
29
28
}
30
29
version = getCleanVersion (version );
31
- Matcher matcher = VERSION_PATTERN .matcher (version );
32
- if (matcher .matches ()) {
33
- String major = matcher .group (1 );
34
- if (!StringUtils .isEmpty (major )) {
35
- try {
36
- return Integer .parseInt (major );
37
- } catch (NumberFormatException e ) {
38
- log .error ("Failed to parse major version of " + version , e );
39
- }
40
- }
30
+ Optional <Version > parsedVersion = Version .tryParse (StringUtils .removeStart (version , "v" ));
31
+ if (parsedVersion .isPresent ()) {
32
+ return (int ) parsedVersion .get ().majorVersion ();
33
+ } else {
34
+ log .debug ("Failed to parse major version of " + version );
41
35
}
42
36
return 0 ;
43
37
}
0 commit comments