X Tutup
Skip to content

Commit bda27a4

Browse files
committed
Extract version command
1 parent 1a1a630 commit bda27a4

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

pkg/cmd/root/root.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package root
22

33
import (
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-
}

pkg/cmd/version/version.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/cli/cli/pkg/cmdutil"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewCmdVersion(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "version",
15+
Hidden: true,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
fmt.Fprint(f.IOStreams.Out, Format(version, buildDate))
18+
},
19+
}
20+
21+
cmdutil.DisableAuthCheck(cmd)
22+
23+
return cmd
24+
}
25+
26+
func Format(version, buildDate string) string {
27+
version = strings.TrimPrefix(version, "v")
28+
29+
if buildDate != "" {
30+
version = fmt.Sprintf("%s (%s)", version, buildDate)
31+
}
32+
33+
return fmt.Sprintf("gh version %s\n%s\n", version, changelogURL(version))
34+
}
35+
36+
func changelogURL(version string) string {
37+
path := "https://github.com/cli/cli"
38+
r := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[\w.]+)?$`)
39+
if !r.MatchString(version) {
40+
return fmt.Sprintf("%s/releases/latest", path)
41+
}
42+
43+
url := fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v"))
44+
return url
45+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package root
1+
package version
22

33
import (
44
"testing"

0 commit comments

Comments
 (0)
X Tutup