-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
85 lines (69 loc) · 1.96 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
package goversion
import (
"fmt"
"strings"
)
// Version represent the go version information.
type Version struct {
Major int `json:"major"` // Major version number
Minor int `json:"minor"` // Minor version number
Patch int `json:"patch,omitempty"` // Patch version number
Pre string `json:"prerelease,omitempty"` // Pre release version identifier
}
// Clone returns a deep copy of instance.
func (v *Version) Clone() *Version {
return &Version{
Major: v.Major,
Minor: v.Minor,
Patch: v.Patch,
Pre: v.Pre,
}
}
// String returns go version string.
func (v *Version) String() string {
if v.IsPreRelease() {
return fmt.Sprintf("go%d.%d%s", v.Major, v.Minor, v.Pre)
}
if v.Patch == 0 {
return fmt.Sprintf("go%d.%d", v.Major, v.Minor)
}
return fmt.Sprintf("go%d.%d.%d", v.Major, v.Minor, v.Patch)
}
// BuildTag returns go build tag string.
//
// See also: https://golang.org/pkg/go/build/#hdr-Build_Constraints
func (v *Version) BuildTag() string {
return fmt.Sprintf("go%d.%d", v.Major, v.Minor)
}
// ReleaseTags returns release build tags.
//
// See also: https://golang.org/pkg/go/build/#Context
func (v *Version) ReleaseTags() []string {
res := make([]string, 0, v.Minor)
for i := 1; i <= v.Minor; i++ {
res = append(res, fmt.Sprintf("go1.%d", i))
}
return res
}
// IsPreRelease reports whether pre release version.
func (v *Version) IsPreRelease() bool {
return v.Pre != ""
}
// IsBeta reports whether beta version.
func (v *Version) IsBeta() bool {
return strings.HasPrefix(v.Pre, "beta")
}
// IsRC reports whether release candidate version.
func (v *Version) IsRC() bool {
return strings.HasPrefix(v.Pre, "rc")
}
// Equal reports whether v and u represent the same version instant.
func (v *Version) Equal(u *Version) bool {
if v == nil && u == nil {
return true
}
if v == nil || u == nil {
return false
}
return v.Major == u.Major && v.Minor == u.Minor && v.Patch == u.Patch && v.Pre == u.Pre
}