From bc95eb440c75cd67a140732ca96aa09a05a9c189 Mon Sep 17 00:00:00 2001 From: lihong Date: Thu, 27 Jul 2023 17:34:40 +0800 Subject: [PATCH] version --- version.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 version.go diff --git a/version.go b/version.go new file mode 100644 index 0000000..59cd085 --- /dev/null +++ b/version.go @@ -0,0 +1,50 @@ +package gdk + +import ( + "encoding/json" + "fmt" + "runtime" +) + +// ldflags +var ( + version string + gitBranch string + gitTag string + gitCommit string + gitTreeState string + buildDate string +) + +// Info contains versioning information. +type Info struct { + Version string `json:"version"` + GitBranch string `json:"gitBranch"` + GitTag string `json:"gitTag"` + GitCommit string `json:"gitCommit"` + GitTreeState string `json:"gitTreeState"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` +} + +// String returns info as a human-friendly version string. +func (info Info) String() string { + b, _ := json.MarshalIndent(info, "", "\t") + return string(b) +} + +func Version() Info { + return Info{ + Version: version, + GitBranch: gitBranch, + GitTag: gitTag, + GitCommit: gitCommit, + GitTreeState: gitTreeState, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +}