Skip to content

Commit a9db4e8

Browse files
author
Nate Smith
authored
Merge pull request cli#221 from github/release-the-kraken
Add changelog link to `gh version` and `gh --version`
2 parents bf209d8 + ced6809 commit a9db4e8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

command/root.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"regexp"
78
"strings"
89

910
"github.com/github/gh-cli/api"
@@ -19,9 +20,13 @@ var Version = "DEV"
1920
// BuildDate is dynamically set at build time in the Makefile
2021
var BuildDate = "YYYY-MM-DD"
2122

23+
var versionOutput = ""
24+
2225
func init() {
2326
RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate)
27+
versionOutput = fmt.Sprintf("gh version %s\n%s\n", RootCmd.Version, changelogURL(RootCmd.Version))
2428
RootCmd.AddCommand(versionCmd)
29+
RootCmd.SetVersionTemplate(versionOutput)
2530

2631
RootCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
2732
RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
@@ -56,7 +61,7 @@ var versionCmd = &cobra.Command{
5661
Use: "version",
5762
Hidden: true,
5863
Run: func(cmd *cobra.Command, args []string) {
59-
fmt.Printf("gh version %s\n", RootCmd.Version)
64+
fmt.Printf(versionOutput)
6065
},
6166
}
6267

@@ -128,3 +133,15 @@ func colorableErr(cmd *cobra.Command) io.Writer {
128133
}
129134
return err
130135
}
136+
137+
func changelogURL(version string) string {
138+
path := "https://github.com/github/homebrew-gh"
139+
r := regexp.MustCompile(`^v\d+\.\d+.\d+$`)
140+
if !r.MatchString(version) {
141+
return fmt.Sprintf("%s/releases/latest", path)
142+
}
143+
144+
tag := version
145+
url := fmt.Sprintf("%s/releases/tag/%s", path, tag)
146+
return url
147+
}

command/root_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestChangelogURL(t *testing.T) {
9+
tag := "v0.3.2"
10+
url := fmt.Sprintf("https://github.com/github/homebrew-gh/releases/tag/v0.3.2")
11+
result := changelogURL(tag)
12+
if result != url {
13+
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
14+
}
15+
16+
tag = "0.3.5-90-gdd3f0e0"
17+
url = fmt.Sprintf("https://github.com/github/homebrew-gh/releases/latest")
18+
result = changelogURL(tag)
19+
if result != url {
20+
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
21+
}
22+
23+
tag = "deadbeef"
24+
url = fmt.Sprintf("https://github.com/github/homebrew-gh/releases/latest")
25+
result = changelogURL(tag)
26+
if result != url {
27+
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
28+
}
29+
}

0 commit comments

Comments
 (0)