-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversions.go
More file actions
87 lines (70 loc) · 1.52 KB
/
Copy pathversions.go
File metadata and controls
87 lines (70 loc) · 1.52 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
package libracore
import (
"fmt"
"runtime/debug"
"sync"
"github.com/Masterminds/semver/v3"
)
var (
rawVersion = "0.1.0-DEV"
LibraVersion, _ = semver.NewVersion(rawVersion)
)
func VersionInfo() string {
application := "LibraCore"
version := "v" + LibraVersion.String()
info := getBuildInfo()
if info == nil {
return version
}
if info.Revision != "" {
version += "+" + info.Revision
}
osArch := info.GoOS + "/" + info.GoArch
date := info.RevisionTime
if date == "" {
date = "unknown"
}
versionInfo := fmt.Sprintf("%s %s %s BuildDate=%s", application, version, osArch, date)
return versionInfo
}
type buildInfo struct {
*debug.BuildInfo
VersionControlSystem string
Revision string
RevisionTime string
Modified bool
GoOS string
GoArch string
}
var (
buildInfoInstance *buildInfo
buildInfoOnce sync.Once
)
func getBuildInfo() *buildInfo {
buildInfoOnce.Do(func() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
buildInfoInstance = &buildInfo{
BuildInfo: info,
}
for _, s := range info.Settings {
switch s.Key {
case "vcs":
buildInfoInstance.VersionControlSystem = s.Value
case "vcs.revision":
buildInfoInstance.Revision = s.Value
case "vcs.time":
buildInfoInstance.RevisionTime = s.Value
case "vcs.modified":
buildInfoInstance.Modified = s.Value == "true"
case "GOOS":
buildInfoInstance.GoOS = s.Value
case "GOARCH":
buildInfoInstance.GoArch = s.Value
}
}
})
return buildInfoInstance
}