-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.go
56 lines (48 loc) · 1.09 KB
/
info.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 main
import (
"os"
"runtime"
"runtime/debug"
"text/template"
)
// Set by LDLFLAGS on build
var (
Version string
Branch string
BuildDate string
)
// inspired by https://github.com/prometheus/common/blob/main/version/info.go
var infoTmpl = `{{.program}} version {{.version}}
(revision: {{.revision}}, date: {{.buildDate}} go: {{.goVersion}}, platform: {{.platform}})
`
func PrintInfo() {
info := map[string]string{
"program": os.Args[0],
"version": Version,
"branch": Branch,
"revision": "unknown",
"buildDate": BuildDate,
"goVersion": runtime.Version(),
"platform": runtime.GOOS + "/" + runtime.GOARCH,
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
// info["version"] = buildInfo.Main.Version
dirty := false
for _, v := range buildInfo.Settings {
if v.Key == "vcs.revision" {
info["revision"] = v.Value
}
if v.Key == "vcs.modified" {
if v.Value == "true" {
dirty = true
}
}
}
if dirty {
info["revision"] += "-dirty"
}
}
tmpl, _ := template.New("info").Parse(infoTmpl)
tmpl.Execute(os.Stdout, info)
return
}