/
version.go
50 lines (44 loc) · 1.15 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
package commons
import (
"encoding/json"
"fmt"
"runtime"
)
var (
serviceVersion string
gitCommit string
buildDate string
)
// VersionInfo object contains version related info
type VersionInfo struct {
ServiceVersion string `json:"serviceVersion"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}
// GetVersion returns VersionInfo object
func GetVersion() VersionInfo {
return VersionInfo{
ServiceVersion: serviceVersion,
GitCommit: gitCommit,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}
// GetServiceVersion returns service version in string
func GetServiceVersion() string {
return serviceVersion
}
// GetVersionJSON returns VersionInfo object in JSON string
func GetVersionJSON() (string, error) {
info := GetVersion()
marshalled, err := json.MarshalIndent(&info, "", " ")
if err != nil {
return "", err
}
return string(marshalled), nil
}