-
Notifications
You must be signed in to change notification settings - Fork 54
/
version.go
51 lines (44 loc) · 927 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
49
50
51
package main
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 v struct {
Version string `json:"version"`
}
json.Unmarshal(versionJSON, &v)
release := v.Version
info, ok := debug.ReadBuildInfo()
if !ok {
return release + " dev-build"
}
var dirty bool
var day, revision string
// Append the revision to the version.
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"
}