forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
84 lines (74 loc) · 1.52 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
package version
import (
"regexp"
"strconv"
"strings"
)
var (
numberRe = regexp.MustCompile("[0-9]+")
wordRe = regexp.MustCompile("[a-z]+")
)
func GreaterThan(a, b string) bool {
a = stripMetadata(a)
b = stripMetadata(b)
a = strings.TrimLeft(a, "v")
b = strings.TrimLeft(b, "v")
aSplit := periodDashSplit(a)
bSplit := periodDashSplit(b)
if len(bSplit) > len(aSplit) {
return !GreaterThan(b, a) && a != b
}
for i := 0; i < len(aSplit); i++ {
if i == len(bSplit) {
if _, err := strconv.Atoi(aSplit[i]); err == nil {
return true
}
return false
}
aWord := wordRe.FindString(aSplit[i])
bWord := wordRe.FindString(bSplit[i])
if aWord != "" && bWord != "" {
if strings.Compare(aWord, bWord) > 0 {
return true
}
if strings.Compare(bWord, aWord) > 0 {
return false
}
}
aMatch := numberRe.FindString(aSplit[i])
bMatch := numberRe.FindString(bSplit[i])
if aMatch == "" || bMatch == "" {
if strings.Compare(aSplit[i], bSplit[i]) > 0 {
return true
}
if strings.Compare(bSplit[i], aSplit[i]) > 0 {
return false
}
}
aNum, _ := strconv.Atoi(aMatch)
bNum, _ := strconv.Atoi(bMatch)
if aNum > bNum {
return true
}
if bNum > aNum {
return false
}
}
return false
}
func stripMetadata(v string) string {
split := strings.Split(v, "+")
if len(split) > 1 {
return split[0]
}
return v
}
func periodDashSplit(s string) []string {
return strings.FieldsFunc(s, func(r rune) bool {
switch r {
case '.', '-':
return true
}
return false
})
}