Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use build info when available (go install) #265

Merged
merged 3 commits into from
May 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion buildversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

package buildversion

import (
"fmt"
"runtime/debug"
)

const DefaultVersion = "0.0.0-dev"

var (
// these are overwritten/populated via build CLI
BuildVersion = "0.0.0-dev"
BuildVersion = DefaultVersion
BuildTime = ""
BuildCommit = ""
)
Expand All @@ -31,3 +38,16 @@ var packageManager = "source"
func PackageManager() string {
return packageManager
}

func init() {
// Use build info from debug package if available, and if no build info is
// provided via build CLI.
info, available := debug.ReadBuildInfo()
// info.Main.Version will be "" when debugging, and "(devel)" when building with no arguments
if available && info.Main.Version != "" && info.Main.Version != "(devel)" && BuildTime == "" && BuildCommit == "" && BuildVersion == DefaultVersion {
BuildVersion = info.Main.Version
BuildCommit = fmt.Sprintf("(unknown, mod sum: %q)", info.Main.Sum)
BuildTime = "(unknown)"
}

}