-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
48 lines (42 loc) · 890 Bytes
/
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
package dhstore
import (
_ "embed"
"encoding/json"
"fmt"
"runtime/debug"
"time"
)
//go:embed version.json
var versionJSON []byte
var Version = buildVersion()
func buildVersion() string {
// Read version from embedded JSON file.
var verMap map[string]string
json.Unmarshal(versionJSON, &verMap)
release := verMap["version"]
var revision string
var day string
var dirty bool
info, ok := debug.ReadBuildInfo()
if !ok {
return release + " dev-build"
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
revision = kv.Value[:7]
case "vcs.time":
t, _ := time.Parse(time.RFC3339, kv.Value)
day = t.UTC().Format("2006-01-02")
case "vcs.modified":
dirty = kv.Value == "true"
}
}
if dirty {
revision += "-dirty"
}
if revision != "" {
return fmt.Sprintf("%s %s-%s", release, day, revision)
}
return release + " dev-build"
}