-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.go
101 lines (88 loc) · 1.87 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
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
package main
import (
"fmt"
"strconv"
"strings"
)
// Version section struct
// The version will follow semver.
type Version struct {
major int
minor int
patch int
message string
}
// Export as string
func (v Version) String() string {
m := "\n"
if v.message != "" {
m = v.message
}
return fmt.Sprintf("[%d.%d.%d]\n%s", v.major, v.minor, v.patch, m)
}
// Format version string like v1.0.0
func (v Version) VersionString() string {
return fmt.Sprintf("v%d.%d.%d", v.major, v.minor, v.patch)
}
// Bump patch version and create new version
func (v Version) BumpPatch(msg string) Version {
return Version{
major: v.major,
minor: v.minor,
patch: v.patch + 1,
message: msg,
}
}
// Bump minor version and create new version
func (v Version) BumpMinor(msg string) Version {
return Version{
major: v.major,
minor: v.minor + 1,
message: msg,
}
}
// Bump major version and create new version
func (v Version) BumpMajor(msg string) Version {
return Version{
major: v.major + 1,
message: msg,
}
}
// Create version struct from parsed string
func createVersion(major, minor, patch, message string) Version {
_major, _ := strconv.Atoi(major)
_minor, _ := strconv.Atoi(minor)
_patch, _ := strconv.Atoi(patch)
return Version{
major: _major,
minor: _minor,
patch: _patch,
message: strings.Trim(message, "\r\n"),
}
}
// Version slice type to define sort interface
type Versions []Version
func (v Versions) Len() int {
return len(v)
}
func (v Versions) Less(i, j int) bool {
if v[i].major < v[j].major {
return false
} else if v[i].major > v[j].major {
return true
}
if v[i].minor < v[j].minor {
return false
} else if v[i].minor > v[j].minor {
return true
}
if v[i].patch < v[j].patch {
return false
} else if v[i].patch > v[j].patch {
return true
}
return false
}
func (v Versions) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}