forked from superfly/flyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
114 lines (94 loc) · 1.99 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
108
109
110
111
112
113
114
package buildinfo
import (
"errors"
"os"
"strconv"
"time"
"runtime/debug"
"github.com/blang/semver"
"github.com/superfly/flyctl/terminal"
)
var (
buildDate = "<date>"
version = "<version>"
)
var (
parsedVersion semver.Version
parsedBuildDate time.Time
)
func init() {
loadMeta()
}
func loadMeta() {
var err error
parsedBuildDate, err = time.Parse(time.RFC3339, buildDate)
var parseErr *time.ParseError
if errors.As(err, &parseErr) && IsDev() {
parsedBuildDate = time.Now()
} else if err != nil {
panic(err)
}
if IsDev() {
envVersionNum := os.Getenv("FLY_DEV_VERSION_NUM")
versionNum := uint64(parsedBuildDate.Unix())
if envVersionNum != "" {
num, err := strconv.ParseUint(envVersionNum, 10, 64)
if err == nil {
versionNum = num
}
}
parsedVersion = semver.Version{
Pre: []semver.PRVersion{
{
VersionNum: versionNum,
IsNum: true,
},
},
Build: []string{"dev"},
}
} else {
parsedBuildDate = parsedBuildDate.UTC()
parsedVersion = semver.MustParse(version)
}
}
func Commit() string {
info, _ := debug.ReadBuildInfo();
var rev string = "<none>";
var dirty string = "";
for _, v := range info.Settings {
if v.Key == "vcs.revision" {
rev = v.Value;
}
if v.Key == "vcs.modified" {
if v.Value == "true" {
dirty = "-dirty"
} else {
dirty = "";
}
}
}
return rev + dirty;
}
func Version() semver.Version {
return parsedVersion
}
func BuildDate() time.Time {
return parsedBuildDate
}
func parseVesion(v string) semver.Version {
parsedV, err := semver.ParseTolerant(v)
if err != nil {
terminal.Warnf("error parsing version number '%s': %s\n", v, err)
return semver.Version{}
}
return parsedV
}
func IsVersionSame(otherVerison string) bool {
return parsedVersion.EQ(parseVesion(otherVerison))
}
func IsVersionOlder(otherVerison string) bool {
return parsedVersion.LT(parseVesion(otherVerison))
}
func IsVersionNewer(otherVerison string) bool {
return parsedVersion.GT(parseVesion(otherVerison))
}