forked from bosun-monitor/bosun
/
version.go
56 lines (48 loc) · 1.53 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
// Package version holds some version data common to bosun and scollector.
// Most of these values will be inserted at build time with `-ldFlags` directives for official builds.
package version // import "bosun.org/_version"
import (
"fmt"
"os"
"time"
"github.com/kardianos/osext"
)
// These variables will be set at linking time for official builds.
// build.go will set date and sha, but `go get` will set none of these.
var (
// Version number for official releases Updated manually before each release.
Version = "0.8.0"
// Set to any non-empty value by official release script
OfficialBuild string
// Date and time of build. Should be in YYYYMMDDHHMMSS format
VersionDate string
// VersionSHA should be set at build time as the most recent commit hash.
VersionSHA string
)
// Get a string representing the version information for the current binary.
func GetVersionInfo(app string) string {
var sha, build string
version := ShortVersion()
if buildTime, err := time.Parse("20060102150405", VersionDate); err == nil {
build = " built " + buildTime.Format(time.RFC3339)
} else {
currentFilePath, err := osext.Executable()
if err == nil {
info, err := os.Stat(currentFilePath)
if err == nil {
build = " last modified " + info.ModTime().Format(time.RFC3339)
}
}
}
if VersionSHA != "" {
sha = fmt.Sprintf(" (%s)", VersionSHA)
}
return fmt.Sprintf("%s version %s%s%s", app, version, sha, build)
}
func ShortVersion() string {
version := Version
if OfficialBuild == "" {
version += "-dev"
}
return version
}