-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathbuildinfo.go
More file actions
121 lines (107 loc) · 2.79 KB
/
buildinfo.go
File metadata and controls
121 lines (107 loc) · 2.79 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
package buildinfo
import (
"fmt"
"runtime/debug"
"strings"
"sync"
"time"
"golang.org/x/mod/semver"
)
var (
buildInfo *debug.BuildInfo
buildInfoValid bool
readBuildInfo sync.Once
externalURL string
readExternalURL sync.Once
version string
readVersion sync.Once
// Injected with ldflags at build!
tag string
)
const (
// develPrefix is prefixed to developer versions of the application.
develPrefix = "v0.0.0-devel"
)
// Version returns the semantic version of the build.
// Use golang.org/x/mod/semver to compare versions.
func Version() string {
readVersion.Do(func() {
revision, valid := revision()
if valid {
revision = "+" + revision[:7]
}
if tag == "" {
// This occurs when the tag hasn't been injected,
// like when using "go run".
version = develPrefix + revision
return
}
version = "v" + tag
// The tag must be prefixed with "v" otherwise the
// semver library will return an empty string.
if semver.Build(version) == "" {
version += revision
}
})
return version
}
// VersionsMatch compares the two versions. It assumes the versions match if
// the major and the minor versions are equivalent. Patch versions are
// disregarded. If it detects that either version is a developer build it
// returns true.
func VersionsMatch(v1, v2 string) bool {
// Developer versions are disregarded...hopefully they know what they are
// doing.
if strings.HasPrefix(v1, develPrefix) || strings.HasPrefix(v2, develPrefix) {
return true
}
return semver.MajorMinor(v1) == semver.MajorMinor(v2)
}
// ExternalURL returns a URL referencing the current Coder version.
// For production builds, this will link directly to a release.
// For development builds, this will link to a commit.
func ExternalURL() string {
readExternalURL.Do(func() {
repo := "https://github.com/coder/coder"
revision, valid := revision()
if !valid {
externalURL = repo
return
}
externalURL = fmt.Sprintf("%s/commit/%s", repo, revision)
})
return externalURL
}
// Time returns when the Git revision was published.
func Time() (time.Time, bool) {
value, valid := find("vcs.time")
if !valid {
return time.Time{}, false
}
parsed, err := time.Parse(time.RFC3339, value)
if err != nil {
panic("couldn't parse time: " + err.Error())
}
return parsed, true
}
// revision returns the Git hash of the build.
func revision() (string, bool) {
return find("vcs.revision")
}
// find panics if a setting with the specific key was not
// found in the build info.
func find(key string) (string, bool) {
readBuildInfo.Do(func() {
buildInfo, buildInfoValid = debug.ReadBuildInfo()
})
if !buildInfoValid {
panic("couldn't read build info")
}
for _, setting := range buildInfo.Settings {
if setting.Key != key {
continue
}
return setting.Value, true
}
return "", false
}