forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare.go
More file actions
121 lines (106 loc) · 3.18 KB
/
compare.go
File metadata and controls
121 lines (106 loc) · 3.18 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
// Package versioncmp provides functions for comparing version strings.
//
// Version strings are dot-separated integers with an optional
// pre-release suffix. A pre-release suffix is an arbitrary string with a
// leading dash character. All functions ignore these suffixes, so "1.2" and
// "1.2-rc" are considered equivalent.
package versioncmp
import (
"strconv"
"strings"
)
const (
rcString = "-rc"
ceEdition = "-ce"
)
// compare compares two versions of Docker to decipher which came first.
//
// compare returns -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
func compare(v1, v2 string) int {
// Replace RC string with "." to make the RC number appear as simply
// another sub-version.
v1 = strings.Replace(v1, rcString, ".", -1)
v2 = strings.Replace(v2, rcString, ".", -1)
// All releases before the community edition (differentiated by
// presence of the "ce" string in the version string) are "less than"
// any community edition release (first occurring in March 2017).
if strings.Contains(v1, ceEdition) && !strings.Contains(v2, ceEdition) {
return 1
}
if !strings.Contains(v1, ceEdition) && strings.Contains(v2, ceEdition) {
return -1
}
// Without this tag, both are pre-CE versions.
if !strings.Contains(v1, ceEdition) && !strings.Contains(v2, ceEdition) {
return compareNumeric(v1, v2)
}
return compareCE(v1, v2)
}
// compareCE ("Community Edition") will differentiate between versions of
// Docker that use the versioning scheme
// {{release-year}}.{{release-month}}-{{ce|ee}}-{{rcnum|""}}
//
// This will be every release after 1.13.1.
func compareCE(v1, v2 string) int {
return compareNumeric(
strings.Replace(v1, ceEdition, "", -1),
strings.Replace(v2, ceEdition, "", -1),
)
}
// compareNumeric compares two version that use pre-17.03 Docker.
//
// Non-numeric segments in either argument are considered equal, so
// compare("1.a", "1.b") == 0, but compare("2.a", "1.b") == 1.
func compareNumeric(v1, v2 string) int {
if n := strings.IndexByte(v1, '-'); n != -1 {
v1 = v1[:n]
}
if n := strings.IndexByte(v2, '-'); n != -1 {
v2 = v2[:n]
}
var (
currTab = strings.Split(v1, ".")
otherTab = strings.Split(v2, ".")
)
max := len(currTab)
if len(otherTab) > max {
max = len(otherTab)
}
for i := 0; i < max; i++ {
var currInt, otherInt int
if len(currTab) > i {
currInt, _ = strconv.Atoi(currTab[i])
}
if len(otherTab) > i {
otherInt, _ = strconv.Atoi(otherTab[i])
}
if currInt > otherInt {
return 1
}
if otherInt > currInt {
return -1
}
}
return 0
}
// LessThan checks if a version is less than another.
func LessThan(v, other string) bool {
return compare(v, other) == -1
}
// LessThanOrEqualTo checks if a version is less than or equal to another.
func LessThanOrEqualTo(v, other string) bool {
return compare(v, other) <= 0
}
// GreaterThan checks if a version is greater than another.
func GreaterThan(v, other string) bool {
return compare(v, other) == 1
}
// GreaterThanOrEqualTo checks if a version is greater than or equal to
// another.
func GreaterThanOrEqualTo(v, other string) bool {
return compare(v, other) >= 0
}
// Equal checks if a version is equal to another.
func Equal(v, other string) bool {
return compare(v, other) == 0
}