-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
75 lines (64 loc) · 1.25 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
73
74
75
package oskit
import (
"encoding/json"
"os"
"runtime"
"time"
"github.com/airdb/toolbox/oskit"
"github.com/airdb/toolbox/utilkit"
)
// Build version info.
type BuildInfo struct {
GoVersion string
Env string
Repo string
Version string
Swagger string `json:"swagger,omitempty"`
Build string
BuildTime string
Uptime string
CreatedAt time.Time
}
var (
Env string
Repo string
Version string
Swagger string
Build string
BuildTime string
Uptime string
CreatedAt time.Time
)
func Init() {
// Init the loc.
loc, _ := time.LoadLocation("Asia/Shanghai")
// Set timezone.
CreatedAt = time.Now().In(loc)
Env = os.Getenv("ENV")
if Env == "" {
Env = oskit.DeployStageDev
}
}
func GetBuildInfo() *BuildInfo {
return &BuildInfo{
GoVersion: runtime.Version(),
Env: oskit.GetDeployStage(),
Repo: Repo,
Version: Version,
Swagger: Swagger,
Build: Build,
BuildTime: BuildTime,
CreatedAt: CreatedAt,
Uptime: time.Since(CreatedAt).String(),
}
}
func (info *BuildInfo) ToString() string {
out, err := json.Marshal(info)
if err != nil {
panic(err)
}
return string(out)
}
func (info *BuildInfo) ToProject() string {
return utilkit.LastStringWithSplit(info.Repo, "/")
}