Skip to content

Commit de6e99a

Browse files
committed
command: default to toolchain version
Since the Go toolchain is able to extract the module version at build time, we should use that as a default instead of DEV. This means customers installing via go-get will get the correct version. Unfortunately, the toolchain does not store when the build occurs, so BuildTime now defaults to the empty string. It is still set if build officially, just not for go-get.
1 parent 9dd9b8c commit de6e99a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

command/root.go

Lines changed: 16 additions & 5 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-
var Version = "DEV"
19+
// Version is dynamically set by the toolchain or overriden by the Makefile.
20+
var Version = func(info *debug.BuildInfo, ok bool) string {
21+
if !ok {
22+
return "(devel)"
23+
}
24+
return info.Main.Version
25+
}(debug.ReadBuildInfo())
2026

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

2430
var versionOutput = ""
2531

2632
func init() {
27-
RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate)
33+
Version = strings.TrimPrefix(info.Main.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)