Skip to content

Commit

Permalink
version: use buildinfo vcs revision
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Apr 21, 2022
1 parent 33f7632 commit 6f46d94
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions version.go
Expand Up @@ -6,10 +6,20 @@ import (
"runtime/debug"
"strings"

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

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

// 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'
Expand All @@ -23,11 +33,9 @@ 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
}
// The version wasn't set by ldflags, so fallback to the VCS revision, and
// finally Go module version.
Version = stringz.DefaultEmpty(stringz.DefaultEmpty(Version, vcsRevision(bi)), bi.Main.Version)

if !includeDeps {
if Version == "(devel)" {
Expand Down

0 comments on commit 6f46d94

Please sign in to comment.