From 1921a74eec5038d5fd04606e6125f82bd329b93f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 13 Jan 2020 11:02:34 -0800 Subject: [PATCH 1/2] Add changelog func --- command/root.go | 15 ++++++++++++++- command/root_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 command/root_test.go diff --git a/command/root.go b/command/root.go index 033ee7284..6bf99c229 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" @@ -56,7 +57,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("gh version %s\n%s\n", RootCmd.Version, changelogURL(RootCmd.Version)) }, } @@ -128,3 +129,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) + } +} From ced6809e1e7354f597c3ef382e970a47fbb61c6e Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 13 Jan 2020 11:14:45 -0800 Subject: [PATCH 2/2] add changelog to --version --- command/root.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/command/root.go b/command/root.go index 6bf99c229..77cb51a48 100644 --- a/command/root.go +++ b/command/root.go @@ -20,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") @@ -57,7 +61,7 @@ var versionCmd = &cobra.Command{ Use: "version", Hidden: true, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("gh version %s\n%s\n", RootCmd.Version, changelogURL(RootCmd.Version)) + fmt.Printf(versionOutput) }, }