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
This commit is contained in:
Sebastian Mark 2024-11-09 15:59:30 +01:00
parent c937a7b904
commit 61b66ce98f

View file

@ -1,7 +1,37 @@
package metadata package metadata
import "strings" import (
"os/exec"
"runtime/debug"
"strings"
)
// This will be overwritten by goreleaser on build var (
var GoTomatoVersion = "devel" // The GoTomato version GoTomatoVersion = "" // The GoTomato version
var ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol 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]
}