diff --git a/command/root.go b/command/root.go index 0864bfca7..4f9d5ba09 100644 --- a/command/root.go +++ b/command/root.go @@ -5,6 +5,7 @@ import ( "io" "os" "regexp" + "runtime/debug" "strings" "github.com/cli/cli/api" @@ -15,16 +16,26 @@ import ( "github.com/spf13/cobra" ) -// Version is dynamically set at build time in the Makefile +// Version is dynamically set by the toolchain or overriden by the Makefile. var Version = "DEV" -// BuildDate is dynamically set at build time in the Makefile -var BuildDate = "YYYY-MM-DD" +// BuildDate is dynamically set at build time in the Makefile. +var BuildDate = "" // YYYY-MM-DD var versionOutput = "" func init() { - RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate) + if Version == "DEV" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" { + Version = info.Main.Version + } + } + Version = strings.TrimPrefix(Version, "v") + if BuildDate == "" { + RootCmd.Version = Version + } else { + RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate) + } versionOutput = fmt.Sprintf("gh version %s\n%s\n", RootCmd.Version, changelogURL(Version)) RootCmd.AddCommand(versionCmd) RootCmd.SetVersionTemplate(versionOutput)