11package root
22
33import (
4- "fmt"
54 "net/http"
6- "regexp"
7- "strings"
85
96 "github.com/MakeNowJust/heredoc"
107 "github.com/cli/cli/api"
@@ -22,6 +19,7 @@ import (
2219 releaseCmd "github.com/cli/cli/pkg/cmd/release"
2320 repoCmd "github.com/cli/cli/pkg/cmd/repo"
2421 creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
22+ versionCmd "github.com/cli/cli/pkg/cmd/version"
2523 "github.com/cli/cli/pkg/cmdutil"
2624 "github.com/spf13/cobra"
2725)
@@ -49,23 +47,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
4947 },
5048 }
5149
52- version = strings .TrimPrefix (version , "v" )
53- if buildDate == "" {
54- cmd .Version = version
55- } else {
56- cmd .Version = fmt .Sprintf ("%s (%s)" , version , buildDate )
57- }
58- versionOutput := fmt .Sprintf ("gh version %s\n %s\n " , cmd .Version , changelogURL (version ))
59- cmd .AddCommand (& cobra.Command {
60- Use : "version" ,
61- Hidden : true ,
62- Run : func (cmd * cobra.Command , args []string ) {
63- fmt .Print (versionOutput )
64- },
65- })
66- cmd .SetVersionTemplate (versionOutput )
67- cmd .Flags ().Bool ("version" , false , "Show gh version" )
68-
6950 cmd .SetOut (f .IOStreams .Out )
7051 cmd .SetErr (f .IOStreams .ErrOut )
7152
@@ -74,7 +55,13 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
7455 cmd .SetUsageFunc (rootUsageFunc )
7556 cmd .SetFlagErrorFunc (rootFlagErrrorFunc )
7657
58+ formattedVersion := versionCmd .Format (version , buildDate )
59+ cmd .SetVersionTemplate (formattedVersion )
60+ cmd .Version = formattedVersion
61+ cmd .Flags ().Bool ("version" , false , "Show gh version" )
62+
7763 // Child commands
64+ cmd .AddCommand (versionCmd .NewCmdVersion (f , version , buildDate ))
7865 cmd .AddCommand (aliasCmd .NewCmdAlias (f ))
7966 cmd .AddCommand (authCmd .NewCmdAuth (f ))
8067 cmd .AddCommand (configCmd .NewCmdConfig (f ))
@@ -140,14 +127,3 @@ func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) {
140127 return baseRepo , nil
141128 }
142129}
143-
144- func changelogURL (version string ) string {
145- path := "https://github.com/cli/cli"
146- r := regexp .MustCompile (`^v?\d+\.\d+\.\d+(-[\w.]+)?$` )
147- if ! r .MatchString (version ) {
148- return fmt .Sprintf ("%s/releases/latest" , path )
149- }
150-
151- url := fmt .Sprintf ("%s/releases/tag/v%s" , path , strings .TrimPrefix (version , "v" ))
152- return url
153- }
0 commit comments