diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index b5b51970f..16276fd22 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -1,10 +1,7 @@ package root import ( - "fmt" "net/http" - "regexp" - "strings" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" @@ -22,6 +19,7 @@ import ( releaseCmd "github.com/cli/cli/pkg/cmd/release" repoCmd "github.com/cli/cli/pkg/cmd/repo" creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits" + versionCmd "github.com/cli/cli/pkg/cmd/version" "github.com/cli/cli/pkg/cmdutil" "github.com/spf13/cobra" ) @@ -49,23 +47,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { }, } - version = strings.TrimPrefix(version, "v") - if buildDate == "" { - cmd.Version = version - } else { - cmd.Version = fmt.Sprintf("%s (%s)", version, buildDate) - } - versionOutput := fmt.Sprintf("gh version %s\n%s\n", cmd.Version, changelogURL(version)) - cmd.AddCommand(&cobra.Command{ - Use: "version", - Hidden: true, - Run: func(cmd *cobra.Command, args []string) { - fmt.Print(versionOutput) - }, - }) - cmd.SetVersionTemplate(versionOutput) - cmd.Flags().Bool("version", false, "Show gh version") - cmd.SetOut(f.IOStreams.Out) cmd.SetErr(f.IOStreams.ErrOut) @@ -74,7 +55,13 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { cmd.SetUsageFunc(rootUsageFunc) cmd.SetFlagErrorFunc(rootFlagErrrorFunc) + formattedVersion := versionCmd.Format(version, buildDate) + cmd.SetVersionTemplate(formattedVersion) + cmd.Version = formattedVersion + cmd.Flags().Bool("version", false, "Show gh version") + // Child commands + cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate)) cmd.AddCommand(aliasCmd.NewCmdAlias(f)) cmd.AddCommand(authCmd.NewCmdAuth(f)) cmd.AddCommand(configCmd.NewCmdConfig(f)) @@ -140,14 +127,3 @@ func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) { return baseRepo, nil } } - -func changelogURL(version string) string { - path := "https://github.com/cli/cli" - r := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[\w.]+)?$`) - if !r.MatchString(version) { - return fmt.Sprintf("%s/releases/latest", path) - } - - url := fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v")) - return url -} diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go new file mode 100644 index 000000000..040a3132e --- /dev/null +++ b/pkg/cmd/version/version.go @@ -0,0 +1,45 @@ +package version + +import ( + "fmt" + "regexp" + "strings" + + "github.com/cli/cli/pkg/cmdutil" + "github.com/spf13/cobra" +) + +func NewCmdVersion(f *cmdutil.Factory, version, buildDate string) *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprint(f.IOStreams.Out, Format(version, buildDate)) + }, + } + + cmdutil.DisableAuthCheck(cmd) + + return cmd +} + +func Format(version, buildDate string) string { + version = strings.TrimPrefix(version, "v") + + if buildDate != "" { + version = fmt.Sprintf("%s (%s)", version, buildDate) + } + + return fmt.Sprintf("gh version %s\n%s\n", version, changelogURL(version)) +} + +func changelogURL(version string) string { + path := "https://github.com/cli/cli" + r := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[\w.]+)?$`) + if !r.MatchString(version) { + return fmt.Sprintf("%s/releases/latest", path) + } + + url := fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v")) + return url +} diff --git a/pkg/cmd/root/root_test.go b/pkg/cmd/version/version_test.go similarity index 98% rename from pkg/cmd/root/root_test.go rename to pkg/cmd/version/version_test.go index 714032ec3..9a1d49db3 100644 --- a/pkg/cmd/root/root_test.go +++ b/pkg/cmd/version/version_test.go @@ -1,4 +1,4 @@ -package root +package version import ( "testing"