-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
121 lines (97 loc) · 2.39 KB
/
utils.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
package lib
import (
"fmt"
"strconv"
"strings"
)
// CompareVersions compares Aerospike Server versions
// if version1 == version2 returns 0
// else if version1 < version2 returns -1
// else returns 1
func CompareVersions(version1, version2 string) (int, error) {
if version1 == "" || version2 == "" {
return 0, fmt.Errorf("wrong versions to compare")
}
if version1 == version2 {
return 0, nil
}
// Ignoring extra comment tag... found in git source code build
v1 := strings.Split(version1, "-")[0]
v2 := strings.Split(version2, "-")[0]
if v1 == v2 {
return 0, nil
}
verElems1 := strings.Split(v1, ".")
verElems2 := strings.Split(v2, ".")
minLen := len(verElems1)
if len(verElems2) < minLen {
minLen = len(verElems2)
}
for i := 0; i < minLen; i++ {
ve1, err := strconv.Atoi(verElems1[i])
if err != nil {
return 0, fmt.Errorf("wrong version to compare")
}
ve2, err := strconv.Atoi(verElems2[i])
if err != nil {
return 0, fmt.Errorf("wrong version to compare")
}
if ve1 > ve2 {
return 1, nil
} else if ve1 < ve2 {
return -1, nil
}
}
if len(verElems1) > len(verElems2) {
return 1, nil
}
if len(verElems1) < len(verElems2) {
return -1, nil
}
return 0, nil
}
// CompareVersionsIgnoreRevision compares Aerospike Server versions ignoring
// revisions and builds.
// if version1 == version2 returns 0
// else if version1 < version2 returns -1
// else returns 1
func CompareVersionsIgnoreRevision(version1, version2 string) (int, error) {
if version1 == "" || version2 == "" {
return 0, fmt.Errorf("wrong versions to compare")
}
if version1 == version2 {
return 0, nil
}
// Ignoring extra comment tag... found in git source code build
v1 := strings.Split(version1, "-")[0]
v2 := strings.Split(version2, "-")[0]
if v1 == v2 {
return 0, nil
}
verElems1 := strings.Split(v1, ".")
verElems2 := strings.Split(v2, ".")
minLen := len(verElems1)
if len(verElems2) < minLen {
minLen = len(verElems2)
}
if minLen > 2 {
// Force comparison of only major and minor version.
minLen = 2
}
for i := 0; i < minLen; i++ {
ve1, err := strconv.Atoi(verElems1[i])
if err != nil {
return 0, fmt.Errorf("wrong version to compare")
}
ve2, err := strconv.Atoi(verElems2[i])
if err != nil {
return 0, fmt.Errorf("wrong version to compare")
}
if ve1 > ve2 {
return 1, nil
} else if ve1 < ve2 {
return -1, nil
}
}
return 0, nil
}