From 69e1d3a17de44a372edcdd7b61914c7cab91c2f9 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sat, 9 Nov 2024 15:59:30 +0100 Subject: [PATCH] feat: 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 --- internal/metadata/version.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/metadata/version.go b/internal/metadata/version.go index ecac17b..b1ea77d 100644 --- a/internal/metadata/version.go +++ b/internal/metadata/version.go @@ -1,7 +1,32 @@ 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 getLatestTag() string { + output, _ := exec.Command("git", "describe", "--tags").Output() + stripped_output := strings.TrimSpace(string(output)) + return strings.TrimLeft(stripped_output, "v") +} + +// set version from runtime/debug.Main.Version +// use latest git tag as fallback +// allow GoTomatoVersion to be overwritten via ldflags +func init() { + if GoTomatoVersion == "" { + info, _ := debug.ReadBuildInfo() + GoTomatoVersion = info.Main.Version + if info.Main.Version == "(devel)" { + GoTomatoVersion = getLatestTag() + } + } + ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol version +}