-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathversion.go
More file actions
141 lines (121 loc) · 3.32 KB
/
Copy pathversion.go
File metadata and controls
141 lines (121 loc) · 3.32 KB
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
133
134
135
136
137
138
139
140
141
package version
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
versionPrefix string = "v"
versionLevelSeparator string = "."
)
// Version represents the major, minor and build values.
//
// Making a new Version is as easy as:
//
// v := &Version{Major: 1, Minor: 2, Build: 3}
//
type Version struct {
// Major represents the major version value.
Major uint64
// Minor represents the minor version value.
Minor uint64
// Build represents the build version value.
Build uint64
}
// Parse parses a version string into a Version object.
func Parse(version string) (*Version, error) {
v := new(Version)
var majorStr, minorStr, buildStr string
var err error
// split the string
segs := strings.Split(version, versionLevelSeparator)
majorStr = segs[0]
switch len(segs) {
case 3: // v1.2.3
buildStr = segs[2]
fallthrough
case 2: // v1.2
minorStr = segs[1]
case 1: // v1
default: // something else unexpected
return nil, errors.New("version: Version only supports up to three segments, major.minor.build")
}
if v.Major, err = parseUInt(majorStr); err != nil {
return nil, err
}
if v.Minor, err = parseUInt(minorStr); err != nil {
return nil, err
}
if v.Build, err = parseUInt(buildStr); err != nil {
return nil, err
}
return v, nil
}
// parseUInt safely parses a string to produce a uint64.
func parseUInt(s string) (uint64, error) {
// ignore the 'v' if there is one
if strings.HasPrefix(s, versionPrefix) {
s = strings.TrimLeft(s, versionPrefix)
}
// "" is OK - just zero
if len(s) == 0 {
return 0, nil
}
return strconv.ParseUint(s, 10, 64)
}
// Increase jumps to the next version by increasing each component by the
// specified amount.
//
// A new Version object will be returned.
func (v *Version) Increase(majorInc, minorInc, buildInc uint64) *Version {
newV := &Version{v.Major, v.Minor, v.Build}
newV.IncreaseHere(majorInc, minorInc, buildInc)
return newV
}
// IncreaseHere jumps to the next version by increasing each component by the
// specified amount. This Version object is modified.
func (v *Version) IncreaseHere(majorInc, minorInc, buildInc uint64) {
if majorInc > 0 {
v.Major += majorInc
v.Minor = 0
v.Build = 0
}
if minorInc > 0 {
v.Minor += minorInc
v.Build = 0
}
if buildInc > 0 {
v.Build += buildInc
}
}
// String gets the full version string representing this version.
func (v *Version) String() string {
return versionPrefix + v.StringNumber()
}
// StringShort gets the short string representing this version.
// So v1.0.0 becomes v1.
func (v *Version) StringShort() string {
return versionPrefix + v.StringShortNumber()
}
// StringNumber gets the short string representing this version
// without the prefix. E.g. v1.1.0 becomes 1.1
func (v *Version) StringNumber() string {
return strings.Join([]string{fmt.Sprintf("%d", v.Major),
fmt.Sprintf("%d", v.Minor),
fmt.Sprintf("%d", v.Build),
}, versionLevelSeparator)
}
// StringShortNumber gets the short string representing this version
// without the prefix. E.g. v1.1.0 becomes 1.1
func (v *Version) StringShortNumber() string {
segs := make([]string, 1, 3)
segs[0] = fmt.Sprintf("%d", v.Major)
if v.Minor > 0 || v.Build > 0 {
segs = append(segs, fmt.Sprintf("%d", v.Minor))
}
if v.Build > 0 {
segs = append(segs, fmt.Sprintf("%d", v.Build))
}
return strings.Join(segs, versionLevelSeparator)
}