feat: add buildinfo package for version information

This commit is contained in:
Mathis Maquenne
2026-07-30 11:30:08 +02:00
parent cab49d2818
commit 785bb58cb5
+74
View File
@@ -0,0 +1,74 @@
package buildinfo
import (
"fmt"
"runtime"
"runtime/debug"
"strings"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
// devVersion is the version reported by builds that were not produced by a
// release. Both the update check and `godeez update` refuse to run on them.
const devVersion = "dev"
var (
version = devVersion
commit = ""
date = ""
)
func Version() string {
if version != devVersion {
return version
}
if info, ok := debug.ReadBuildInfo(); ok {
if v := releaseVersion(info.Main.Version); v != "" {
return v
}
}
return devVersion
}
func releaseVersion(v string) string {
if !semver.IsValid(v) {
return ""
}
if semver.Build(v) != "" || module.IsPseudoVersion(v) {
return ""
}
return strings.TrimPrefix(v, "v")
}
func IsDev() bool {
return Version() == devVersion
}
func Commit() string {
if commit != "" {
return commit
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
return s.Value
}
}
}
return ""
}
func Date() string {
return date
}
func UserAgent() string {
return fmt.Sprintf("godeez/%s (%s/%s)", Version(), runtime.GOOS, runtime.GOARCH)
}