forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
57 lines (45 loc) · 1.1 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
package strategy
import (
"github.com/cloudfoundry/cli/cf/errors"
"strconv"
"strings"
)
type Version struct {
Major uint64
Minor uint64
Patch uint64
}
func ParseVersion(input string) (Version, error) {
parts := strings.Split(input, ".")
if len(parts) != 3 {
return Version{}, errors.NewWithFmt("Could not parse version number: %s", input)
}
major, err1 := strconv.ParseUint(parts[0], 10, 64)
minor, err2 := strconv.ParseUint(parts[1], 10, 64)
patch, err3 := strconv.ParseUint(parts[2], 10, 64)
if err1 != nil || err2 != nil || err3 != nil {
return Version{}, errors.NewWithFmt("Could not parse version number: %s", input)
}
return Version{major, minor, patch}, nil
}
func (version Version) LessThan(other Version) bool {
if version.Major < other.Major {
return true
}
if version.Major > other.Major {
return false
}
if version.Minor < other.Minor {
return true
}
if version.Minor > other.Minor {
return false
}
if version.Patch < other.Patch {
return true
}
return false
}
func (version Version) GreaterThanOrEqualTo(other Version) bool {
return !version.LessThan(other)
}