-
Notifications
You must be signed in to change notification settings - Fork 24
/
version.go
65 lines (58 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
package main
import (
"fmt"
"strconv"
"strings"
)
// A Version wraps a major and minor integer version pair.
type Version struct {
Major int
Minor int
}
// ParseVersion returns a Version from a "major.minor" or "all" version string.
func ParseVersion(version string) (Version, error) {
if version == "all" {
return Version{-1, -1}, nil
}
split := strings.Split(version, ".")
if len(split) != 2 {
return Version{0, 0}, fmt.Errorf("invalid version string: %s", version)
}
majorNumber, err := strconv.Atoi(split[0])
if err != nil {
return Version{0, 0}, fmt.Errorf("invalid major version number: %s", split[0])
}
minorNumber, err := strconv.Atoi(split[1])
if err != nil {
return Version{0, 0}, fmt.Errorf("invalid minor version number: %s", split[1])
}
return Version{majorNumber, minorNumber}, nil
}
// Compare compares two versions, returning 1, 0, or -1 if the compared version
// is before, after, or equal to this version respectively. The "all versions"
// pseudo-version is equal to all other versions.
func (v Version) Compare(v2 Version) int {
if v.IsAll() || v2.IsAll() {
return 0
}
if v.Major < v2.Major {
return -1
} else if v.Major > v2.Major {
return 1
} else if v.Minor < v2.Minor {
return -1
} else if v.Minor > v2.Minor {
return 1
}
return 0
}
// IsAll returns true if this version is the "all versions" pseudo-version.
func (v Version) IsAll() bool {
return v.Major < 0
}
func (v Version) String() string {
if v.IsAll() {
return "all"
}
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
}