-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
executable file
·72 lines (61 loc) · 1.61 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package version
import (
"encoding/json"
"fmt"
"runtime"
"github.com/gosuri/uitable"
)
var (
GitVersion = "v0.0.0" // tag
BuildDate = "1970-01-01T00:00:00Z" // 构建时间
GitCommit = "$Format:%H$" // Commit ID
GitTreeState = "dirty" // Git 树的状态,"clean" or "dirty"
)
type Info struct {
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}
func Get() Info {
return Info{
GitVersion: GitVersion,
GitCommit: GitCommit,
GitTreeState: GitTreeState,
BuildDate: BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}
func (info Info) ToJSON() string {
s, err := json.Marshal(info)
if err != nil {
return ""
}
return string(s)
}
func (info Info) String() string {
s, err := info.Text()
if err != nil {
return info.GitVersion
}
return string(s)
}
func (info Info) Text() ([]byte, error) {
table := uitable.New()
table.RightAlign(0)
table.MaxColWidth = 80
table.Separator = " "
table.AddRow("gitVersion:", info.GitVersion)
table.AddRow("gitCommit:", info.GitCommit)
table.AddRow("gitTreeState:", info.GitTreeState)
table.AddRow("buildData:", info.BuildDate)
table.AddRow("goVersion:", info.GoVersion)
table.AddRow("compiler:", info.Compiler)
table.AddRow("platform:", info.Platform)
return table.Bytes(), nil
}