-
Notifications
You must be signed in to change notification settings - Fork 18
/
version.go
299 lines (264 loc) · 8.15 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package versionutils
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
)
// SemverLowerBound is the "nil" value for changelog versions
// It is not itself a valid version but it allows us to use our semver validation on the v0.0.1 edge case
const (
SemverNilVersionValue = "v0.0.0"
SemverMinimumVersion = "v0.0.1"
)
var (
InvalidSemverVersionError = func(tag string) error {
return eris.Errorf("Tag %s is not a valid semver version, must be of the form vX.Y.Z[-<label>#]", tag)
}
)
type Version struct {
Major int
Minor int
Patch int
// optional to support a version like "1.0.0-rc1", where "rc" is the label and "1" is the label version
// for comparisons:
// - "1.0.0-rc1" is greater than "0.X.Y" and less than "1.0.0"
// - "1.0.0-rc5" is greater than "1.0.0-rc1"
// - "1.0.0-aX" is not greater than or less than "1.0.0-bY", except by convention
Label string
LabelVersion int
}
func NewVersion(major, minor, patch int, label string, labelVersion int) *Version {
return &Version{
Major: major,
Minor: minor,
Patch: patch,
Label: label,
LabelVersion: labelVersion,
}
}
func (v *Version) String() string {
if v.LabelVersion == 0 {
return fmt.Sprintf("v%d.%d.%d", v.Major, v.Minor, v.Patch)
}
return fmt.Sprintf("v%d.%d.%d-%s%d", v.Major, v.Minor, v.Patch, v.Label, v.LabelVersion)
}
// In order, returns isGreaterThanOrEqualTo, isDeterminable, err
// isDeterminable is for incomparable versions because they have different labels, e.g. 1.0.0-foo1 vs 1.0.0-bar2
// If you want to tiebreak indeterminate comparisons using alphanumeric ordering, try MustIsGreaterThanOrEqualTo
func (v *Version) IsGreaterThanOrEqualToPtr(lesser *Version) (bool, bool, error) {
if v == nil {
return false, true, eris.Errorf("cannot compare versions, greater version is nil")
}
if lesser == nil {
return false, true, eris.Errorf("cannot compare versions, lesser version is nil")
}
isGtrEq, determinable := v.IsGreaterThanOrEqualTo(*lesser)
return isGtrEq, determinable, nil
}
// In order, returns isGreaterThanOrEqualTo, isDeterminable
// isDeterminable is for incomparable versions because they have different labels, e.g. 1.0.0-foo1 vs 1.0.0-bar2
// If you want to tiebreak indeterminate comparisons using alphanumeric ordering, try MustIsGreaterThanOrEqualTo
func (v Version) IsGreaterThanOrEqualTo(lesser Version) (bool, bool) {
if v.Equals(&lesser) {
return true, false
}
return v.IsGreaterThan(lesser)
}
// In order, returns isGreaterThanOrEqualTo, isDeterminable, err
// isDeterminable is for incomparable versions because they have different labels, e.g. 1.0.0-foo1 vs 1.0.0-bar2
// If you want to tiebreak indeterminate comparisons using alphanumeric ordering, try MustIsGreaterThan
func (v *Version) IsGreaterThanPtr(lesser *Version) (bool, bool, error) {
if v == nil {
return false, true, eris.Errorf("cannot compare versions, greater version is nil")
}
if lesser == nil {
return false, true, eris.Errorf("cannot compare versions, lesser version is nil")
}
isGreater, determinable := v.IsGreaterThan(*lesser)
return isGreater, determinable, nil
}
// In order, returns isGreaterThanOrEqualTo, isDeterminable
// isDeterminable is for incomparable versions because they have different labels, e.g. 1.0.0-foo1 vs 1.0.0-bar2
// If you want to tiebreak indeterminate comparisons using alphanumeric ordering, try MustIsGreaterThan
func (v Version) IsGreaterThan(lesser Version) (bool, bool) {
if v.Major > lesser.Major {
return true, true
} else if v.Major < lesser.Major {
return false, true
}
if v.Minor > lesser.Minor {
return true, true
} else if v.Minor < lesser.Minor {
return false, true
}
if v.Patch > lesser.Patch {
return true, true
} else if v.Patch < lesser.Patch {
return false, true
}
if len(v.Label) == 0 && lesser.Label != "" {
return true, true
} else if len(v.Label) > 0 && len(lesser.Label) == 0 {
return false, true
}
if v.Label != lesser.Label {
return false, false
}
if v.LabelVersion > lesser.LabelVersion {
return true, true
} else if v.LabelVersion < lesser.LabelVersion {
return false, true
}
return false, true
}
// for incomparable versions, default to alphanumeric sort on label
// e.g. 1.0.0-foo1 > 1.0.0-bar2
func (v Version) MustIsGreaterThanOrEqualTo(lesser Version) bool {
if v.Equals(&lesser) {
return true
}
return v.MustIsGreaterThan(lesser)
}
// for incomparable versions, default to alphanumeric sort on label
// e.g. 1.0.0-foo1 > 1.0.0-bar2
func (v Version) MustIsGreaterThan(lesser Version) bool {
isGtr, determinable := v.IsGreaterThan(lesser)
if determinable {
return isGtr
}
// if we can't compare versions (i.e., different labels) then default to alphanumeric sort
return v.Label > lesser.Label
}
func (v *Version) Equals(other *Version) bool {
return *v == *other
}
func (v *Version) IncrementVersion(breakingChange, newFeature bool) *Version {
newMajor := v.Major
newMinor := v.Minor
newPatch := v.Patch
newLabelVersion := v.LabelVersion
if v.LabelVersion != 0 {
newLabelVersion = v.LabelVersion + 1
} else if v.Major == 0 {
if !breakingChange {
newPatch = v.Patch + 1
} else {
newMinor = v.Minor + 1
newPatch = 0
}
} else {
if breakingChange {
newMajor = v.Major + 1
newMinor = 0
newPatch = 0
} else if newFeature {
newMinor = v.Minor + 1
newPatch = 0
} else {
newPatch = v.Patch + 1
}
}
return &Version{
Major: newMajor,
Minor: newMinor,
Patch: newPatch,
Label: v.Label,
LabelVersion: newLabelVersion,
}
}
func Zero() Version {
return Version{
Major: 0,
Minor: 0,
Patch: 0,
}
}
func StableApiVersion() Version {
return Version{
Major: 1,
Minor: 0,
Patch: 0,
}
}
func IsGreaterThanTag(greaterTag, lesserTag string) (bool, bool, error) {
greaterVersion, err := ParseVersion(greaterTag)
if err != nil {
return false, false, err
}
lesserVersion, err := ParseVersion(lesserTag)
if err != nil {
return false, false, err
}
return greaterVersion.IsGreaterThanPtr(lesserVersion)
}
func ParseVersion(tag string) (*Version, error) {
if !MatchesRegex(tag) {
return nil, InvalidSemverVersionError(tag)
}
versionString := tag[1:]
splitOnHyphen := strings.SplitN(versionString, "-", 2)
labelAndVersion := ""
if len(splitOnHyphen) > 1 {
labelAndVersion = splitOnHyphen[1]
versionString = splitOnHyphen[0]
}
versionParts := strings.Split(versionString, ".")
if len(versionParts) != 3 {
return nil, eris.Errorf("Version %s is not a valid semver version", versionString)
}
major, err := strconv.Atoi(versionParts[0])
if err != nil {
return nil, eris.Errorf("Major version %s is not valid", versionParts[0])
}
minor, err := strconv.Atoi(versionParts[1])
if err != nil {
return nil, eris.Errorf("Minor version %s is not valid", versionParts[1])
}
patch, err := strconv.Atoi(versionParts[2])
if err != nil {
return nil, eris.Errorf("Patch version %s is not valid", versionParts[2])
}
version := &Version{
Major: major,
Minor: minor,
Patch: patch,
}
if labelAndVersion != "" {
label, labelVersion, err := parseLabelVersion(labelAndVersion)
if err != nil {
return nil, err
}
version.Label = label
version.LabelVersion = labelVersion
}
isGtEq, _ := version.IsGreaterThanOrEqualTo(Zero())
if !isGtEq {
return nil, eris.Errorf("Version %s is not greater than or equal to v0.0.0", tag)
}
return version, nil
}
func parseLabelVersion(labelAndVersion string) (string, int, error) {
numsRegex := regexp.MustCompile("[0-9]+")
numberGroups := numsRegex.FindAllString(labelAndVersion, -1)
if len(numberGroups) == 0 {
// valid label with no version, eg "wasm"
return labelAndVersion, 0, nil
}
label := numsRegex.ReplaceAllString(labelAndVersion, "")
labelVersionToParse := numberGroups[0]
labelVersion, err := strconv.Atoi(labelVersionToParse)
if err != nil {
return "", 0, errors.Wrapf(err, "invalid label version %s", labelVersionToParse)
}
return label, labelVersion, nil
}
func MatchesRegex(tag string) bool {
regex := regexp.MustCompile("(v[0-9]+[.][0-9]+[.][0-9]+(-[a-z]+)*(-[a-z]+[0-9]*)?$)")
return regex.MatchString(tag)
}
func GetImageVersion(version *Version) string {
return version.String()[1:]
}