dev: improve version output (#4483)

This commit is contained in:
Ludovic Fernandez 2024-03-11 20:28:27 +01:00 committed by GitHub
parent 85e1dee09a
commit d18acc5b51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,31 +13,64 @@ var (
goVersion = "unknown" goVersion = "unknown"
// Populated by goreleaser during build // Populated by goreleaser during build
version = "master" version = "unknown"
commit = "?" commit = "?"
date = "" date = ""
) )
func main() { func main() {
if buildInfo, available := debug.ReadBuildInfo(); available { info := createBuildInfo()
goVersion = buildInfo.GoVersion
if date == "" {
version = buildInfo.Main.Version
commit = fmt.Sprintf("(unknown, mod sum: %q)", buildInfo.Main.Sum)
date = "(unknown)"
}
}
info := commands.BuildInfo{
GoVersion: goVersion,
Version: version,
Commit: commit,
Date: date,
}
if err := commands.Execute(info); err != nil { if err := commands.Execute(info); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)
os.Exit(exitcodes.Failure) os.Exit(exitcodes.Failure)
} }
} }
func createBuildInfo() commands.BuildInfo {
info := commands.BuildInfo{
Commit: commit,
Version: version,
GoVersion: goVersion,
Date: date,
}
if buildInfo, available := debug.ReadBuildInfo(); available {
info.GoVersion = buildInfo.GoVersion
if date == "" {
info.Version = buildInfo.Main.Version
var revision string
var modified string
for _, setting := range buildInfo.Settings {
// The `vcs.xxx` information is only available with `go build`.
// This information is are not available with `go install` or `go run`.
switch setting.Key {
case "vcs.time":
info.Date = setting.Value
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
modified = setting.Value
}
}
if revision == "" {
revision = "unknown"
}
if modified == "" {
modified = "?"
}
if info.Date == "" {
info.Date = "(unknown)"
}
info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)", revision, modified, buildInfo.Main.Sum)
}
}
return info
}