Improve version metadata

This commit is contained in:
Daniel 2024-04-11 14:59:01 +02:00
parent ae1468fea1
commit a5b6129e6f

View file

@ -12,23 +12,32 @@ import (
var ( var (
name string name string
license string
version = "dev build" version = "dev build"
buildSource = "[source unknown]" buildSource = "unknown"
buildTime = "[build time unknown]" buildTime = "unknown"
license = "[license unknown]"
info *Info info *Info
loadInfo sync.Once loadInfo sync.Once
) )
func init() {
// Convert version string space placeholders.
version = strings.ReplaceAll(version, "_", " ")
buildSource = strings.ReplaceAll(buildSource, "_", " ")
buildTime = strings.ReplaceAll(buildTime, "_", " ")
}
// Info holds the programs meta information. // Info holds the programs meta information.
type Info struct { type Info struct { //nolint:maligned
Name string Name string
Version string Version string
License string License string
Source string Source string
BuildTime string BuildTime string
CGO bool
Commit string Commit string
CommitTime string CommitTime string
@ -56,12 +65,15 @@ func GetInfo() *Info {
buildSettings[setting.Key] = setting.Value buildSettings[setting.Key] = setting.Value
} }
fmt.Println(buildSettings)
info = &Info{ info = &Info{
Name: name, Name: name,
Version: version, Version: version,
License: license, License: license,
Source: buildSource, Source: buildSource,
BuildTime: buildTime, BuildTime: buildTime,
CGO: buildSettings["CGO_ENABLED"] == "1",
Commit: buildSettings["vcs.revision"], Commit: buildSettings["vcs.revision"],
CommitTime: buildSettings["vcs.time"], CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true", Dirty: buildSettings["vcs.modified"] == "true",
@ -69,10 +81,10 @@ func GetInfo() *Info {
} }
if info.Commit == "" { if info.Commit == "" {
info.Commit = "[commit unknown]" info.Commit = "unknown"
} }
if info.CommitTime == "" { if info.CommitTime == "" {
info.CommitTime = "[commit time unknown]" info.CommitTime = "unknown"
} }
}) })
@ -99,11 +111,19 @@ func FullVersion() string {
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version())) builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))
// Build info. // Build info.
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)) cgoInfo := "-cgo"
if info.CGO {
cgoInfo = "+cgo"
}
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s %s) for %s/%s\n", runtime.Version(), runtime.Compiler, cgoInfo, runtime.GOOS, runtime.GOARCH))
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime)) builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))
// Commit info. // Commit info.
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit)) dirtyInfo := "clean"
if info.Dirty {
dirtyInfo = "dirty"
}
builder.WriteString(fmt.Sprintf("\ncommit %s (%s)\n", info.Commit, dirtyInfo))
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime)) builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source)) builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))
@ -121,7 +141,7 @@ func CheckVersion() error {
return nil // testing on windows return nil // testing on windows
default: default:
// check version information // check version information
if name == "[NAME]" || license == "[license unknown]" { if name == "" || license == "" {
return errors.New("must call SetInfo() before calling CheckVersion()") return errors.New("must call SetInfo() before calling CheckVersion()")
} }
} }