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

Add module version support if tool is installed via go install. #29

Merged
merged 1 commit into from
Feb 7, 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
16 changes: 15 additions & 1 deletion version/verison.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"runtime"
"runtime/debug"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -60,7 +61,7 @@ type Info struct {

// GetVersionInfo represents known information on how this binary was built.
func GetVersionInfo() Info {
return Info{
info := Info{
ASCIIName: asciiName,
GitVersion: gitVersion,
GitCommit: gitCommit,
Expand All @@ -70,6 +71,19 @@ func GetVersionInfo() Info {
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

// If there is debug info for the module, this binary was installed outside
// the normal build process and might not have the ld flags set.
bi, ok := debug.ReadBuildInfo()
if !ok {
return info
}

// Version is set in artifacts built with -X sigs.k8s.io/release-utils/version.gitVersion=<version>
// Ensure version is also set when installed via go install <module>
info.GitVersion = bi.Main.Version

return info
}

// String returns the string representation of the version info
Expand Down