-
Notifications
You must be signed in to change notification settings - Fork 211
/
version.go
59 lines (50 loc) · 1.66 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
// Package version includes methods to compare versions between nodes.
package version
import (
"errors"
"fmt"
"strconv"
"strings"
)
// CheckNodeVersion checks if a request version is more recent then the given min version. returns a bool and an error
func CheckNodeVersion(reqVersion string, minVersion string) (bool, error) {
// TODO : semantic versioning comparison is a pain, refine this or use a lib
splt := strings.Split(reqVersion, "/")
if len(splt) > 2 {
return false, errors.New("invalid client version")
}
if len(splt) == 2 {
reqVersion = splt[1]
}
// if same version string don't do anything
if reqVersion == minVersion {
return true, nil
}
// take each version number and convert to ints
reqsplit, minsplit := strings.Split(reqVersion, "."), strings.Split(minVersion, ".")
if len(reqsplit) != 3 || len(minsplit) != 3 {
return false, fmt.Errorf("one of provided versions isn't valid %v / %v", reqVersion, minVersion)
}
numa, numb := [3]int64{}, [3]int64{}
for i := 0; i < 3; i++ {
var err error
numa[i], err = strconv.ParseInt(reqsplit[i], 10, 8)
if err != nil {
return false, fmt.Errorf(" could not read version number %v", reqVersion)
}
numb[i], err = strconv.ParseInt(minsplit[i], 10, 8)
if err != nil {
return false, fmt.Errorf(" could not read version number %v", reqVersion)
}
}
for i := 0; i < 3; i++ {
if numa[i] > numb[i] {
return true, nil
}
if numa[i] != numb[i] {
return false, nil
}
}
// this should not happen, it means the versions perfectly match but we already tested that above
return false, fmt.Errorf("error comparing node version strings. req (%v) != min (%v)", reqVersion, minVersion)
}