Skip to content

Commit 75b7e95

Browse files
authored
Merge pull request cli#491 from arnottcr/version
command: default to toolchain version
2 parents 032390e + 408b565 commit 75b7e95

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

command/root.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"os"
77
"regexp"
8+
"runtime/debug"
89
"strings"
910

1011
"github.com/cli/cli/api"
@@ -15,16 +16,26 @@ import (
1516
"github.com/spf13/cobra"
1617
)
1718

18-
// Version is dynamically set at build time in the Makefile
19+
// Version is dynamically set by the toolchain or overriden by the Makefile.
1920
var Version = "DEV"
2021

21-
// BuildDate is dynamically set at build time in the Makefile
22-
var BuildDate = "YYYY-MM-DD"
22+
// BuildDate is dynamically set at build time in the Makefile.
23+
var BuildDate = "" // YYYY-MM-DD
2324

2425
var versionOutput = ""
2526

2627
func init() {
27-
RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate)
28+
if Version == "DEV" {
29+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
30+
Version = info.Main.Version
31+
}
32+
}
33+
Version = strings.TrimPrefix(Version, "v")
34+
if BuildDate == "" {
35+
RootCmd.Version = Version
36+
} else {
37+
RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate)
38+
}
2839
versionOutput = fmt.Sprintf("gh version %s\n%s\n", RootCmd.Version, changelogURL(Version))
2940
RootCmd.AddCommand(versionCmd)
3041
RootCmd.SetVersionTemplate(versionOutput)

0 commit comments

Comments
 (0)