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.
This commit is contained in:
Colin Arnott 2020-02-18 07:55:16 +00:00
parent 9dd9b8c763
commit de6e99aa75
No known key found for this signature in database
GPG key ID: 0447A663F7F3E236

View file

@ -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
var Version = "DEV"
// Version is dynamically set by the toolchain or overriden by the Makefile.
var Version = func(info *debug.BuildInfo, ok bool) string {
if !ok {
return "(devel)"
}
return info.Main.Version
}(debug.ReadBuildInfo())
// 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)
Version = strings.TrimPrefix(info.Main.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)