ChronoTomato/internal/metadata/version.go
Sebastian Mark 712aced7da feat!: introduce ChronoTomatoVersion
- set version from `runtime/debug.Main.Version`
- use latest git tag as fallback
- allow version to be overwritten via ldflags
- this will break `go build .` and `go install .`
2024-11-10 09:47:00 +01:00

33 lines
775 B
Go

package metadata
import (
"os/exec"
"runtime/debug"
"strings"
)
var ChronoTomatoVersion = "" // The GoTomato 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 ChronoTomatoVersion == "" {
info, _ := debug.ReadBuildInfo()
if info.Main.Version != "(devel)" {
ChronoTomatoVersion = stripVersionPrefix(info.Main.Version)
} else {
ChronoTomatoVersion = getLatestTag()
}
}
}