From 2032688c1f9ce981d1f8675a33339783ad472cf3 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sat, 9 Nov 2024 15:59:30 +0100 Subject: [PATCH] feat!: always retrieve version from `runtime/debug.Main.Version` or latest git tag - set version from runtime/debug.Main.Version - use latest git tag as fallback - allow GoTomatoVersion to be overwritten via ldflags - this will break `go build .` and `go install .` --- internal/metadata/version.go | 38 ++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/internal/metadata/version.go b/internal/metadata/version.go index ecac17b..1d76b3e 100644 --- a/internal/metadata/version.go +++ b/internal/metadata/version.go @@ -1,7 +1,37 @@ package metadata -import "strings" +import ( + "os/exec" + "runtime/debug" + "strings" +) -// This will be overwritten by goreleaser on build -var GoTomatoVersion = "devel" // The GoTomato version -var ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol version +var ( + GoTomatoVersion = "" // The GoTomato version + ProtocolVersion = "" // The protocol version +) + +func stripVersionPrefix(version string) string { + return strings.TrimLeft(version, "v") +} + +func getLatestTag() string { + bytes, _ := exec.Command("git", "describe", "--tags").Output() + output := strings.TrimSpace(string(bytes)) + return stripVersionPrefix(output) +} + +// set GoTomatoVersion from runtime/debug.Main.Version +// use latest git tag as fallback +// can be overwritten via ldflags (e,g. by goreleaser) +func init() { + if GoTomatoVersion == "" { + info, _ := debug.ReadBuildInfo() + if info.Main.Version != "(devel)" { + GoTomatoVersion = stripVersionPrefix(info.Main.Version) + } else { + GoTomatoVersion = getLatestTag() + } + } + ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] +}