-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
102 lines (85 loc) · 3.7 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package utils
import (
"strconv"
"strings"
)
// SemanticVersion represents a semantic version, following the Major.Minor.Patch
// format. Optionally, a version can include a commit revision as a metadata string
// appended after a plus sign (+).
type SemanticVersion struct {
Major int `json:"major"` // Major version, incremented for incompatible API changes.
Minor int `json:"minor"` // Minor version, incremented for backwards-compatible enhancements.
Patch int `json:"patch"` // Patch version, incremented for backwards-compatible bug fixes.
Commit string `json:"revision,omitempty"` // Optional commit revision for tracking specific builds.
}
// String returns the string representation of the SemanticVersion, excluding the
// commit revision. It adheres to the "Major.Minor.Patch" format.
func (v SemanticVersion) String() string {
if v.Commit != "" {
return strconv.Itoa(v.Major) + "." + strconv.Itoa(v.Minor) + "." + strconv.Itoa(v.Patch) + "+" + v.Commit
}
return strconv.Itoa(v.Major) + "." + strconv.Itoa(v.Minor) + "." + strconv.Itoa(v.Patch)
}
// String returns the string representation of the SemanticVersion, excluding the
// commit revision. It adheres to the "Major.Minor.Patch" format.
func (v SemanticVersion) StringVersion() string {
return strconv.Itoa(v.Major) + "." + strconv.Itoa(v.Minor) + "." + strconv.Itoa(v.Patch)
}
// ParseSemanticVersion parses a version string into a SemanticVersion struct.
// The function supports version strings in the "Major.Minor.Patch" format, with
// an optional commit revision appended after a plus sign (+). The 'v' prefix
// in version strings is also supported and ignored during parsing.
func ParseSemanticVersion(version string) SemanticVersion {
versions := strings.Split(version, "+")
var commit string
if len(versions) > 1 {
version = versions[0]
commit = versions[1]
}
version = strings.Replace(version, "v", "", 1)
parts := strings.Split(version, ".")
if len(parts) != 3 {
return SemanticVersion{}
}
major, _ := strconv.Atoi(parts[0])
minor, _ := strconv.Atoi(parts[1])
patch, _ := strconv.Atoi(parts[2])
return SemanticVersion{
Major: major,
Minor: minor,
Patch: patch,
Commit: commit,
}
}
// IsSemanticVersionGreaterOrEqualTo checks if the version represented by a string
// is greater than or equal to the provided SemanticVersion. The comparison takes
// into account the Major, Minor, and Patch components of the semantic version.
func IsSemanticVersionGreaterOrEqualTo(versionStr string, version SemanticVersion) bool {
parsedVersion := ParseSemanticVersion(versionStr)
if parsedVersion.Major == version.Major && parsedVersion.Minor == version.Minor && parsedVersion.Patch >= version.Patch {
return true
}
if parsedVersion.Major == version.Major && parsedVersion.Minor > version.Minor {
return true
}
if parsedVersion.Major > version.Major && parsedVersion.Minor == version.Minor && parsedVersion.Patch == version.Patch {
return true
}
return false
}
// IsSemanticVersionLowerOrEqualTo checks if the version represented by a string
// is lower than or equal to the provided SemanticVersion. The comparison considers
// the Major, Minor, and Patch components of the semantic version.
func IsSemanticVersionLowerOrEqualTo(versionStr string, version SemanticVersion) bool {
parsedVersion := ParseSemanticVersion(versionStr)
if parsedVersion.Major == version.Major && parsedVersion.Minor == version.Minor && parsedVersion.Patch < version.Patch {
return true
}
if parsedVersion.Major == version.Major && parsedVersion.Minor < version.Minor {
return true
}
if parsedVersion.Major < version.Major && parsedVersion.Minor == version.Minor && parsedVersion.Patch == version.Patch {
return true
}
return false
}