From 8c4487c72e47d0c5c325b8acf9704208fd02ea0a Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:31:51 -0700 Subject: [PATCH] include date and dirty flag in version --- version.go | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/version.go b/version.go index 69cc206..47d8642 100644 --- a/version.go +++ b/version.go @@ -3,31 +3,47 @@ package main import ( _ "embed" "encoding/json" + "fmt" "runtime/debug" + "time" ) -var version string - //go:embed version.json var versionJSON []byte -func init() { +var version = buildVersion() + +func buildVersion() string { // Read version from embedded JSON file. var verMap map[string]string json.Unmarshal(versionJSON, &verMap) - version = verMap["version"] + release := verMap["version"] - // If running from a module, try to get the build info. - bi, ok := debug.ReadBuildInfo() + info, ok := debug.ReadBuildInfo() if !ok { - return + return release + " dev-build" } + var dirty bool + var day, revision string + // Append the revision to the version. - for i := range bi.Settings { - if bi.Settings[i].Key == "vcs.revision" { - version += "-" + bi.Settings[i].Value - break + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + revision = kv.Value[:7] + case "vcs.time": + t, _ := time.Parse(time.RFC3339, kv.Value) + day = t.UTC().Format("2006-01-02") + case "vcs.modified": + dirty = kv.Value == "true" } } + if dirty { + revision += "-dirty" + } + if revision != "" { + return fmt.Sprintf("%s %s-%s", release, day, revision) + } + return release + " dev-build" }