-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathversions.go
More file actions
491 lines (422 loc) · 15.1 KB
/
versions.go
File metadata and controls
491 lines (422 loc) · 15.1 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
MongoDB Versions
The MongoDBVersion type provides support for interacting with MongoDB
versions. This type makes it possible to validate MongoDB version
numbers and ask common questions about MongoDB versions.
*/
package bond
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/blang/semver"
"github.com/pkg/errors"
)
const (
endOfLegacy = "4.5.0-alpha"
firstLTS = "5.0.0"
devReleaseTag = "alpha"
)
// MongoDBVersion encapsulates information about a MongoDB version.
// Use the associated methods to ask questions about MongoDB
// versions. All parsing of versions happens during construction, and
// individual method calls are very light-weight. Note that
// not all methods are applicable for all versions.
type MongoDBVersion interface {
// String returns a string representation of the MongoDB version number.
String() string
// Parsed returns the parsed version object for the version.
Parsed() semver.Version
// Series returns the first two components for the version.
Series() string
// IsReleaseCandidate returns true if the version is a release candidate.
IsReleaseCandidate() bool
// IsDevelopmentRelease returns true if the version is a development release.
IsDevelopmentRelease() bool
// DevelopmentReleaseNumber returns the development release number, if applicable.
DevelopmentReleaseNumber() int
// RCNumber returns the RC counter (or -1 if not a release candidate).
RCNumber() int
// IsLTS returns true if the release is long-term supported, i.e. the yearly release.
IsLTS() bool
// LTS returns most recent LTS series, which may be itself, if applicable.
LTS() string
// IsContinuous returns true if the release is a quarterly (non-LTS) release.
IsContinuous() bool
// IsRelease returns true if the version is a release.
IsRelease() bool
// IsDevelopmentBuild returns true for non-release versions.
IsDevelopmentBuild() bool
// IsStableSeries returns true if the legacy version is a stable series.
IsStableSeries() bool
// IsDevelopmentSeries returns true if the legacy version is a development series.
IsDevelopmentSeries() bool
// StableReleaseSeries returns true if the legacy version is a stable release series.
StableReleaseSeries() string
// IsInitialStableReleaseCandidate returns true if the legacy version is a release
// candidate for the initial release of a stable series.
IsInitialStableReleaseCandidate() bool
IsLessThan(version MongoDBVersion) bool
IsLessThanOrEqualTo(version MongoDBVersion) bool
IsGreaterThan(version MongoDBVersion) bool
IsGreaterThanOrEqualTo(version MongoDBVersion) bool
IsEqualTo(version MongoDBVersion) bool
IsNotEqualTo(version MongoDBVersion) bool
}
// LegacyMongoDBVersion is a structure representing a version identifier for legacy versions of
// MongoDB, which implements the MongoDBVersion interface.
type LegacyMongoDBVersion struct {
source string
parsed semver.Version
isRc bool
isDev bool
rcNumber int
series string
tag string
}
// NewMongoDBVersion is a structure representing a version identifier for versions of
// MongoDB, which implements the MongoDBVersion.
type NewMongoDBVersion struct {
LegacyMongoDBVersion // note not all fields are applicable to NewMongoDBVersion
isDevRelease bool
devReleaseNumber int
quarter string
}
// IsStableSeries is not applicable to new versions, so always return false.
func (v *NewMongoDBVersion) IsStableSeries() bool {
return false
}
// IsDevelopmentSeries is not applicable to new versions, so always return false.
func (v *NewMongoDBVersion) IsDevelopmentSeries() bool {
return false
}
// IsInitialStableReleaseCandidate is not applicable to new versions, so always return false.
func (v *NewMongoDBVersion) IsInitialStableReleaseCandidate() bool {
return false
}
// StableReleaseSeries is not applicable to new versions, so always return the empty string.
func (v *NewMongoDBVersion) StableReleaseSeries() string {
return ""
}
// Series returns the major and quarter for the version.
func (v *NewMongoDBVersion) Series() string {
return v.series
}
// IsLTS returns true if this is the first release of the year.
func (v *NewMongoDBVersion) IsLTS() bool {
return v.IsRelease() && v.Parsed().Minor == 0
}
// LTS returns the most recent LTS series.
func (v *NewMongoDBVersion) LTS() string {
firstLTSVersion, _ := semver.Parse(firstLTS)
if v.Parsed().LT(firstLTSVersion) {
// Return empty string for versions that are not preceded by an
// LTS series.
return ""
}
return fmt.Sprintf("%d.0", v.Parsed().Major)
}
// func IsContinuous returns true if the version is a continuous release.
func (v *NewMongoDBVersion) IsContinuous() bool {
return v.IsRelease() && v.Parsed().Minor != 0
}
// IsDevelopmentRelease returns true if the version is a development release.
func (v *NewMongoDBVersion) IsDevelopmentRelease() bool {
return v.isDevRelease
}
// DevelopmentReleaseNumber returns the number of the development release,
// or -1 if not applicable.
func (v *NewMongoDBVersion) DevelopmentReleaseNumber() int {
return v.devReleaseNumber
}
// CreateMongoDBVersion returns an implementation of the MongoDBVersion.
// If the parsed version is before 4.5.0, then we use the legacy structure.
// Otherwise, we use the modern versioning scheme.
func CreateMongoDBVersion(version string) (MongoDBVersion, error) {
endOfLegacyVersion, err := semver.Parse(endOfLegacy)
if err != nil {
return nil, errors.Wrap(err, "parsing end of legacy version")
}
v, err := createLegacyMongoDBVersion(version)
if err != nil {
return nil, errors.Wrap(err, "creating initial version")
}
if v.Parsed().LT(endOfLegacyVersion) {
return v, nil
}
return createNewMongoDBVersion(*v)
}
// createNewMongoDBVersion takes a string representing a MongoDBVersion and
// returns a NewMongoDBVersion object. All parsing of a version happens during this phase.
func createNewMongoDBVersion(parsedVersion LegacyMongoDBVersion) (*NewMongoDBVersion, error) {
v := &NewMongoDBVersion{LegacyMongoDBVersion: parsedVersion, devReleaseNumber: -1}
var err error
if len(v.String()) < 3 {
return nil, errors.Errorf("version '%s' is invalid", v.String())
}
v.quarter = v.String()[:3]
if strings.Contains(v.tag, devReleaseTag) {
v.isDev = false
v.isDevRelease = true
// Releases triggered by the waterfall use the git describe of
// the commit, we need to remove that before attempting to get
// the dev release number.
split := strings.Split(v.tag, "-")
if len(split) > 0 && len(split[0]) > len(devReleaseTag) {
v.devReleaseNumber, err = strconv.Atoi(split[0][len(devReleaseTag):])
if err != nil {
return nil, errors.Wrap(err, "parsing development release number")
}
}
}
return v, nil
}
// createLegacyMongoDBVersion takes a string representing a MongoDB version and
// returns a LegacyMongoDBVersion object. All parsing of a version happens during this phase.
func createLegacyMongoDBVersion(version string) (*LegacyMongoDBVersion, error) {
v := &LegacyMongoDBVersion{source: version, rcNumber: -1}
if strings.HasSuffix(version, "-") {
v.isDev = true
if !strings.Contains(version, "pre") {
version += "pre-"
}
}
if strings.Contains(version, "~") {
versionParts := strings.Split(version, "~")
version = versionParts[0]
version += "-pre-"
v.tag = strings.Join(versionParts[1:], "")
v.isDev = true
}
parsed, err := semver.Parse(version)
if err != nil {
return nil, errors.Wrapf(err, "parsing version '%s'", version)
}
v.parsed = parsed
if strings.Contains(version, "rc") {
v.isRc = true
}
tagParts := strings.Split(version, "-")
if len(tagParts) > 1 {
v.tag = strings.Join(tagParts[1:], "-")
if v.isRc {
// Prerelease may have +buildinfo suffix, like: 1.0.0-rc0+buildinfo
rcPart := strings.Split(tagParts[1], "+")
v.rcNumber, err = strconv.Atoi(rcPart[0][2:])
if err != nil {
return nil, errors.Wrap(err, "parsing release candidate number")
}
if len(tagParts) > 2 {
v.isDev = true
}
} else {
v.isDev = true
}
}
if len(version) < 3 {
return nil, errors.Errorf("version '%s' is invalid", version)
}
v.series = fmt.Sprintf("%d.%d", v.Parsed().Major, v.Parsed().Minor)
return v, err
}
// ConvertVersion takes an un-typed object and attempts to convert it to a
// version object. For use with compactor functions.
func ConvertVersion(v interface{}) (MongoDBVersion, error) {
switch version := v.(type) {
case *LegacyMongoDBVersion:
return version, nil
case LegacyMongoDBVersion:
return &version, nil
case *NewMongoDBVersion:
return version, nil
case NewMongoDBVersion:
return &version, nil
case MongoDBVersion:
return version, nil
case string:
output, err := CreateMongoDBVersion(version)
if err != nil {
return nil, err
}
return output, nil
case semver.Version:
return CreateMongoDBVersion(version.String())
default:
return nil, fmt.Errorf("%v is not a valid version type (%T)", version, version)
}
}
// String returns a string representation of the MongoDB version number.
func (v *LegacyMongoDBVersion) String() string {
return v.source
}
// Parsed returns the parsed version object for the version.
func (v *LegacyMongoDBVersion) Parsed() semver.Version {
return v.parsed
}
// Series return the release series, generally the first two
// components of a version. For example for 3.2.6, the series is 3.2.
func (v *LegacyMongoDBVersion) Series() string {
return v.series
}
// IsReleaseCandidate returns true for releases that have the "rc[0-9]"
// tag and false otherwise.
func (v *LegacyMongoDBVersion) IsReleaseCandidate() bool {
return v.IsRelease() && v.isRc
}
// IsStableSeries returns true for stable releases, ones where the
// second component of the version string (i.e. "Minor" in semantic
// versioning terms) are even, and false otherwise.
func (v *LegacyMongoDBVersion) IsStableSeries() bool {
return v.parsed.Minor%2 == 0
}
// IsDevelopmentSeries returns true for development (snapshot)
// releases. These versions are those where the second component
// (e.g. "Minor" in semantic versioning terms) are odd, and false
// otherwise.
func (v *LegacyMongoDBVersion) IsDevelopmentSeries() bool {
return !v.IsStableSeries()
}
// StableReleaseSeries returns a series string (e.g. X.Y) for this
// version. For stable releases, the output is the same as
// .Series(). For development releases, this method returns the *next*
// stable series.
func (v *LegacyMongoDBVersion) StableReleaseSeries() string {
if v.IsStableSeries() {
return v.Series()
}
if v.parsed.Minor < 9 {
return fmt.Sprintf("%d.%d", v.parsed.Major, v.parsed.Minor+1)
}
return fmt.Sprintf("%d.0", v.parsed.Major+1)
}
// IsRelease returns true for all version strings that refer to a
// release, including development, release candidate and GA releases,
// and false otherwise. Other builds, including test builds and
// "nightly" snapshots of MongoDB have version strings, but are not
// releases.
func (v *LegacyMongoDBVersion) IsRelease() bool {
return !v.isDev
}
// IsLTS isn't applicable to legacy versions so we return false.
func (v *LegacyMongoDBVersion) IsLTS() bool {
return false
}
// LTS isn't applicable to legacy version so we return an empty string.
func (v *LegacyMongoDBVersion) LTS() string {
return ""
}
// IsContinuous isn't applicable to legacy versions so return false.
func (v *LegacyMongoDBVersion) IsContinuous() bool {
return false
}
// IsDevelopmentRelease returns true if the version refers to a development release.
func (v *LegacyMongoDBVersion) IsDevelopmentRelease() bool {
return v.IsDevelopmentSeries() && v.IsRelease()
}
// DevelopmentReleaseNumber is not applicable to legacy versions, so it returns -1.
func (v *LegacyMongoDBVersion) DevelopmentReleaseNumber() int {
return -1
}
// IsDevelopmentBuild returns true for all non-release builds,
// including nightly snapshots and all testing and development
// builds.
func (v *LegacyMongoDBVersion) IsDevelopmentBuild() bool {
return v.isDev
}
// IsInitialStableReleaseCandidate returns true for release
// candidates for the initial public release of a new stable release
// series.
func (v *LegacyMongoDBVersion) IsInitialStableReleaseCandidate() bool {
if v.IsStableSeries() {
return v.parsed.Patch == 0 && v.IsReleaseCandidate()
}
return false
}
// RCNumber returns an integer for the RC counter. For non-rc releases,
// returns -1.
func (v *LegacyMongoDBVersion) RCNumber() int {
return v.rcNumber
}
// IsLessThan returns true when "version" is less than (e.g. earlier)
// than the object itself.
func (v *LegacyMongoDBVersion) IsLessThan(version MongoDBVersion) bool {
return v.Parsed().LT(version.Parsed())
}
// IsLessThanOrEqualTo returns true when "version" is less than or
// equal to (e.g. earlier or the same as) the object itself.
func (v *LegacyMongoDBVersion) IsLessThanOrEqualTo(version MongoDBVersion) bool {
// semver considers release candidates equal to GA, so we have to special case this
if v.IsEqualTo(version) {
return true
}
return v.Parsed().LT(version.Parsed())
}
// IsGreaterThan returns true when "version" is greater than (e.g. later)
// than the object itself.
func (v *LegacyMongoDBVersion) IsGreaterThan(version MongoDBVersion) bool {
return v.Parsed().GT(version.Parsed())
}
// IsGreaterThanOrEqualTo returns true when "version" is greater than
// or equal to (e.g. the same as or later than) the object itself.
func (v *LegacyMongoDBVersion) IsGreaterThanOrEqualTo(version MongoDBVersion) bool {
if v.IsEqualTo(version) {
return true
}
return v.Parsed().GT(version.Parsed())
}
// IsEqualTo returns true when "version" is the same as the object
// itself.
func (v *LegacyMongoDBVersion) IsEqualTo(version MongoDBVersion) bool {
return v.String() == version.String()
}
// IsNotEqualTo returns true when "version" is the different from the
// object itself.
func (v *LegacyMongoDBVersion) IsNotEqualTo(version MongoDBVersion) bool {
return v.String() != version.String()
}
/////////////////////////////////////////////
//
// Support for Sorting Slices of MongoDB Versions
//
/////////////////////////////////////////////
// MongoDBVersionSlice is an alias for []MongoDBVersion that supports
// the sort.Sorter interface, and makes it possible to sort slices of
// MongoDB versions.
type MongoDBVersionSlice []MongoDBVersion
// Len is required by the sort.Sorter interface. Returns
// the length of the slice.
func (s MongoDBVersionSlice) Len() int {
return len(s)
}
// Less is a required by the sort.Sorter interface. Uses blang/semver
// to compare two versions.
func (s MongoDBVersionSlice) Less(i, j int) bool {
left := s[i]
right := s[j]
return left.Parsed().LT(right.Parsed())
}
// Swap is a required by the sort.Sorter interface. Changes the
// position of two elements in the slice.
func (s MongoDBVersionSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// String() adds support for the Stringer interface, which makes it
// possible to print slices of MongoDB versions as comma separated
// lists.
func (s MongoDBVersionSlice) String() string {
var out []string
for _, v := range s {
if len(v.String()) == 0 {
// some elements end up empty.
continue
}
out = append(out, v.String())
}
return strings.Join(out, ", ")
}
// Sort provides a wrapper around sort.Sort() for slices of MongoDB
// versions objects.
func (s MongoDBVersionSlice) Sort() {
sort.Sort(s)
}