-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
107 lines (92 loc) · 2.37 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
102
103
104
105
106
107
package cli
import (
"fmt"
"strconv"
"strings"
ansi "github.com/multiverse-os/cli/terminal/ansi"
)
type VersionComponent int
const (
Major VersionComponent = iota
Minor
Patch
)
type Version struct {
Major int
Minor int
Patch int
Build BuildInformation
}
func MarshalVersion(version string) Version {
components := strings.Split(version, ".")
if len(components) == 3 {
major, err := strconv.Atoi(components[0])
minor, err := strconv.Atoi(components[1])
patch, err := strconv.Atoi(components[2])
if err != nil {
return Version{Major: major, Minor: minor, Patch: patch}
}
}
return Version{Major: 0, Minor: 0, Patch: 0}
}
type BuildInformation struct {
Source string
Commit string
Signature string
CompiledAt string
}
//func (b Build) AddDeveloper(name, email string) {
// b.Developers = append(b.Developers, Developer{
// Name: name,
// Email: email,
// })
//}
func (vc VersionComponent) String() string {
switch vc {
case Major:
return "Major"
case Minor:
return "Minor"
default: // Patch
return "Patch"
}
}
func (v Version) ColorString() string {
var colorVersion []string
for _, versionComponent := range strings.Split(v.String(), ".") {
if versionComponent == "0" {
colorVersion = append(colorVersion, ansi.Light(ansi.SkyBlue(versionComponent)))
} else {
colorVersion = append(colorVersion, ansi.Bold(ansi.Purple(versionComponent)))
}
}
return ansi.Light(ansi.Blue("[")) +
ansi.Light(ansi.Blue("v")) +
strings.Join(colorVersion, ansi.White(".")) +
ansi.Light(ansi.Blue("]"))
}
func (v Version) undefined() bool {
return v.Major == 0 &&
v.Minor == 0 &&
v.Patch == 0
}
// /////////////////////////////////////////////////////////////////////////////
// TODO: Add sorting
func (v Version) IsSame(version Version) bool {
return v.Major == version.Major &&
v.Minor == version.Minor &&
v.Patch == version.Patch
}
func (v Version) IsOlderThan(version Version) bool {
return v.Major < version.Major ||
(v.Major == version.Major && (v.Minor < version.Minor ||
(v.Minor == version.Minor && v.Patch < version.Patch)))
}
func (v Version) IsNewerThan(version Version) bool {
return v.Major > version.Major ||
(v.Major == version.Major && (v.Minor > version.Minor ||
(v.Minor == version.Minor && v.Patch > version.Patch)))
}
func (v Version) String() string {
return fmt.Sprintf("%v.%v.%v", v.Major, v.Minor, v.Patch)
}