-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeversion_test.go
132 lines (110 loc) · 2.45 KB
/
nodeversion_test.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
package gocb
import (
"fmt"
"strconv"
"strings"
)
type NodeVersion struct {
Major int
Minor int
Patch int
Build int
Edition string
IsMock bool
}
func (v NodeVersion) Equal(ov NodeVersion) bool {
if v.Major == ov.Major && v.Minor == ov.Minor &&
v.Patch == ov.Patch && v.Edition == ov.Edition {
return true
}
return false
}
func (v NodeVersion) Higher(ov NodeVersion) bool {
if v.Major > ov.Major {
return true
} else if v.Major < ov.Major {
return false
}
if v.Minor > ov.Minor {
return true
} else if v.Minor < ov.Minor {
return false
}
if v.Patch > ov.Patch {
return true
} else if v.Patch < ov.Patch {
return false
}
if v.Build > ov.Build {
return true
} else if v.Build < ov.Build {
return false
}
editionMap := map[string]int{
"Community": 1,
"Enterprise": 2,
}
vEditionVal, ok := editionMap[v.Edition]
if !ok {
vEditionVal = 0
}
ovEditionVal, ok := editionMap[ov.Edition]
if !ok {
ovEditionVal = 0
}
if vEditionVal > ovEditionVal {
return true
}
return false
}
func (v NodeVersion) Lower(ov NodeVersion) bool {
return !v.Higher(ov) && !v.Equal(ov)
}
func newNodeVersion(version string, isMock bool) (*NodeVersion, error) {
nodeVersion, err := nodeVersionFromString(version)
if err != nil {
return nil, err
}
nodeVersion.IsMock = isMock
return nodeVersion, nil
}
func nodeVersionFromString(version string) (*NodeVersion, error) {
vSplit := strings.Split(version, ".")
lenSplit := len(vSplit)
if lenSplit == 0 {
return nil, fmt.Errorf("must provide at least a major version")
}
var err error
nodeVersion := NodeVersion{}
nodeVersion.Major, err = strconv.Atoi(vSplit[0])
if err != nil {
return nil, fmt.Errorf("major version is not a valid integer")
}
if lenSplit == 1 {
return &nodeVersion, nil
}
nodeVersion.Minor, err = strconv.Atoi(vSplit[1])
if err != nil {
return nil, fmt.Errorf("minor version is not a valid integer")
}
if lenSplit == 2 {
return &nodeVersion, nil
}
nodeVersion.Patch, err = strconv.Atoi(vSplit[2])
if err != nil {
return nil, fmt.Errorf("patch version is not a valid integer")
}
if lenSplit == 3 {
return &nodeVersion, nil
}
buildEdition := strings.Split(vSplit[3], "_")
nodeVersion.Build, err = strconv.Atoi(buildEdition[0])
if err != nil {
return nil, fmt.Errorf("build version is not a valid integer")
}
if len(buildEdition) == 1 {
return &nodeVersion, nil
}
nodeVersion.Edition = buildEdition[1]
return &nodeVersion, nil
}