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

Allow the override of all version package variables #61

Merged
merged 1 commit into from Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 35 additions & 11 deletions version/version.go
Expand Up @@ -47,6 +47,12 @@ var (
buildDate = unknown
// flag to print the ascii name banner
asciiName = "true"
// goVersion is the used golang version.
goVersion = unknown
// compiler is the used golang compiler.
compiler = unknown
// platform is the used os/arch identifier.
platform = unknown
)

type Info struct {
Expand All @@ -64,14 +70,6 @@ type Info struct {
Description string `json:"-"`
}

func init() {
buildInfo := getBuildInfo()
gitVersion = getGitVersion(buildInfo)
gitCommit = getCommit(buildInfo)
gitTreeState = getDirty(buildInfo)
buildDate = getBuildDate(buildInfo)
}

func getBuildInfo() *debug.BuildInfo {
bi, ok := debug.ReadBuildInfo()
if !ok {
Expand Down Expand Up @@ -131,15 +129,41 @@ func getKey(bi *debug.BuildInfo, key string) string {

// GetVersionInfo represents known information on how this binary was built.
func GetVersionInfo() Info {
buildInfo := getBuildInfo()
gitVersion = getGitVersion(buildInfo)
if gitCommit == unknown {
gitCommit = getCommit(buildInfo)
}

if gitTreeState == unknown {
gitTreeState = getDirty(buildInfo)
}

if buildDate == unknown {
buildDate = getBuildDate(buildInfo)
}

if goVersion == unknown {
goVersion = runtime.Version()
}

if compiler == unknown {
compiler = runtime.Compiler
}

if platform == unknown {
platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
}

return Info{
ASCIIName: asciiName,
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
GoVersion: goVersion,
Compiler: compiler,
Platform: platform,
}
}

Expand Down