From dbe26c4fca373cf71bfc5d53a7876b7c7786cb4c Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Fri, 21 Jun 2024 23:02:31 +0200 Subject: [PATCH] fix: --version flag includes release version Porting fix from https://github.com/ipfs/rainbow/pull/149 --- CHANGELOG.md | 2 ++ version.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db96df..a89d08d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ The following emojis are used to highlight certain changes: ### Fixed +- Release tag version is now included in `--version` output. + ### Security ## [v0.2.3] diff --git a/version.go b/version.go index 4c15936..c59c5a1 100644 --- a/version.go +++ b/version.go @@ -1,22 +1,33 @@ package main import ( + _ "embed" + "encoding/json" + "fmt" "runtime/debug" "time" ) +//go:embed version.json +var versionJSON []byte + var name = "someguy" var version = buildVersion() var userAgent = name + "/" + version func buildVersion() string { + // Read version from embedded JSON file. + var verMap map[string]string + json.Unmarshal(versionJSON, &verMap) + release := verMap["version"] + var revision string var day string var dirty bool info, ok := debug.ReadBuildInfo() if !ok { - return "dev-build" + return release + " dev-build" } for _, kv := range info.Settings { switch kv.Key { @@ -33,7 +44,7 @@ func buildVersion() string { revision += "-dirty" } if revision != "" { - return day + "-" + revision + return fmt.Sprintf("%s %s-%s", release, day, revision) } - return "dev-build" + return release + " dev-build" }