2022-08-17 08:13:57 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/debug"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is set through ldflags='-X ...' in the Makefile
|
2022-09-07 09:49:42 +00:00
|
|
|
var taggedRelease string = "unknown"
|
2022-08-17 08:13:57 +00:00
|
|
|
|
|
|
|
func FormatVersion() string {
|
2022-09-07 09:49:42 +00:00
|
|
|
var commitHash string = ""
|
2022-08-17 08:13:57 +00:00
|
|
|
var dirty string = ""
|
|
|
|
|
|
|
|
info, ok := debug.ReadBuildInfo()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
for _, setting := range info.Settings {
|
|
|
|
if setting.Key == "vcs.revision" {
|
2022-09-07 09:49:42 +00:00
|
|
|
commitHash = "+" + setting.Value
|
|
|
|
if len(commitHash) > 8 {
|
|
|
|
commitHash = commitHash[:8]
|
|
|
|
}
|
2022-08-17 08:13:57 +00:00
|
|
|
} else if setting.Key == "vcs.modified" {
|
|
|
|
modified, err := strconv.ParseBool(setting.Value)
|
|
|
|
if err == nil && modified {
|
|
|
|
dirty = "-dirty"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 09:49:42 +00:00
|
|
|
return taggedRelease + commitHash + dirty
|
2022-08-17 08:13:57 +00:00
|
|
|
}
|