Merge pull request #221 from github/release-the-kraken

Add changelog link to `gh version` and `gh --version`
This commit is contained in:
Nate Smith 2020-01-13 18:08:50 -06:00 committed by GitHub
commit a9db4e8d21
2 changed files with 47 additions and 1 deletions

View file

@ -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
}

29
command/root_test.go Normal file
View file

@ -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)
}
}