diff --git a/command/root.go b/command/root.go index 4fe631e8f..5703f30c2 100644 --- a/command/root.go +++ b/command/root.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "regexp" "strings" "github.com/github/gh-cli/api" @@ -19,9 +20,13 @@ var Version = "DEV" // 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) + versionOutput = fmt.Sprintf("gh version %s\n%s\n", RootCmd.Version, changelogURL(RootCmd.Version)) RootCmd.AddCommand(versionCmd) + RootCmd.SetVersionTemplate(versionOutput) RootCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") RootCmd.PersistentFlags().Bool("help", false, "Show help for command") @@ -56,7 +61,7 @@ var versionCmd = &cobra.Command{ Use: "version", Hidden: true, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("gh version %s\n", RootCmd.Version) + fmt.Printf(versionOutput) }, } @@ -128,3 +133,15 @@ func colorableErr(cmd *cobra.Command) io.Writer { } return err } + +func changelogURL(version string) string { + path := "https://github.com/github/homebrew-gh" + r := regexp.MustCompile(`^v\d+\.\d+.\d+$`) + if !r.MatchString(version) { + return fmt.Sprintf("%s/releases/latest", path) + } + + tag := version + url := fmt.Sprintf("%s/releases/tag/%s", path, tag) + return url +} diff --git a/command/root_test.go b/command/root_test.go new file mode 100644 index 000000000..42a934a76 --- /dev/null +++ b/command/root_test.go @@ -0,0 +1,29 @@ +package command + +import ( + "fmt" + "testing" +) + +func TestChangelogURL(t *testing.T) { + tag := "v0.3.2" + url := fmt.Sprintf("https://github.com/github/homebrew-gh/releases/tag/v0.3.2") + result := changelogURL(tag) + if result != url { + t.Errorf("expected %s to create url %s but got %s", tag, url, result) + } + + tag = "0.3.5-90-gdd3f0e0" + url = fmt.Sprintf("https://github.com/github/homebrew-gh/releases/latest") + result = changelogURL(tag) + if result != url { + t.Errorf("expected %s to create url %s but got %s", tag, url, result) + } + + tag = "deadbeef" + url = fmt.Sprintf("https://github.com/github/homebrew-gh/releases/latest") + result = changelogURL(tag) + if result != url { + t.Errorf("expected %s to create url %s but got %s", tag, url, result) + } +}