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

version: use buildinfo vcs revision #11

Merged
merged 1 commit into from Apr 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
42 changes: 35 additions & 7 deletions version.go
Expand Up @@ -6,15 +6,47 @@ import (
"runtime/debug"
"strings"

"github.com/jzelinskie/stringz"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func findBuildSetting(bi *debug.BuildInfo, key string) string {
for _, setting := range bi.Settings {
if setting.Key == key {
return setting.Value
}
}
return ""
}

func vcsRevision(bi *debug.BuildInfo) string {
revision := findBuildSetting(bi, "vcs.revision")
if revision == "" {
return ""
}

revision = revision[:12] // Short SHA

if findBuildSetting(bi, "vcs.modified") == "true" {
revision = revision + "-dirty"
}

return revision
}

// Version is variable that holds program's version string.
// This should be set with the follow flags to the `go build` command:
// -ldflags '-X github.com/jzelinskie/cobrautil.Version=$YOUR_VERSION_HERE'
var Version string

// VersionWithFallbacks returns a string of the program version.
// If the version wasn't set by ldflags, falls back to the VCS revision, and
// finally Go module version.
func VersionWithFallbacks(bi *debug.BuildInfo) string {
return stringz.DefaultEmpty(stringz.DefaultEmpty(Version, vcsRevision(bi)), bi.Main.Version)
}

// UsageVersion introspects the process debug data for Go modules to return a
// version string.
func UsageVersion(programName string, includeDeps bool) string {
Expand All @@ -23,21 +55,17 @@ func UsageVersion(programName string, includeDeps bool) string {
panic("failed to read BuildInfo because the program was compiled with Go " + runtime.Version())
}

if Version == "" {
// The version wasn't set by ldflags, so fallback to the Go module version.
// Although, this value is pretty much guaranteed to just be "(devel)".
Version = bi.Main.Version
}
version := VersionWithFallbacks(bi)

if !includeDeps {
if Version == "(devel)" {
return fmt.Sprintf("%s development build (unknown exact version)", programName)
}
return fmt.Sprintf("%s %s", programName, Version)
return fmt.Sprintf("%s %s", programName, version)
}

var b strings.Builder
fmt.Fprintf(&b, "%s %s", bi.Path, Version)
fmt.Fprintf(&b, "%s %s", bi.Path, version)
for _, dep := range bi.Deps {
fmt.Fprintf(&b, "\n\t%s %s", dep.Path, dep.Version)
}
Expand Down