Skip to content

Commit 34cc84c

Browse files
committed
Provide consistent factory functions for top-level commands
1 parent c00fe73 commit 34cc84c

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

command/root.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ import (
2222
"github.com/cli/cli/internal/ghrepo"
2323
"github.com/cli/cli/internal/run"
2424
apiCmd "github.com/cli/cli/pkg/cmd/api"
25-
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
25+
gistCmd "github.com/cli/cli/pkg/cmd/gist"
2626
issueCmd "github.com/cli/cli/pkg/cmd/issue"
2727
prCmd "github.com/cli/cli/pkg/cmd/pr"
2828
repoCmd "github.com/cli/cli/pkg/cmd/repo"
29-
repoCloneCmd "github.com/cli/cli/pkg/cmd/repo/clone"
30-
repoCreateCmd "github.com/cli/cli/pkg/cmd/repo/create"
3129
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
32-
repoForkCmd "github.com/cli/cli/pkg/cmd/repo/fork"
33-
repoViewCmd "github.com/cli/cli/pkg/cmd/repo/view"
3430
"github.com/cli/cli/pkg/cmdutil"
3531
"github.com/cli/cli/pkg/iostreams"
3632
"github.com/cli/cli/utils"
@@ -123,15 +119,9 @@ func init() {
123119
return currentBranch, nil
124120
},
125121
}
126-
RootCmd.AddCommand(apiCmd.NewCmdApi(cmdFactory, nil))
127122

128-
gistCmd := &cobra.Command{
129-
Use: "gist",
130-
Short: "Create gists",
131-
Long: `Work with GitHub gists.`,
132-
}
133-
RootCmd.AddCommand(gistCmd)
134-
gistCmd.AddCommand(gistCreateCmd.NewCmdCreate(cmdFactory, nil))
123+
RootCmd.AddCommand(apiCmd.NewCmdApi(cmdFactory, nil))
124+
RootCmd.AddCommand(gistCmd.NewCmdGist(cmdFactory))
135125

136126
resolvedBaseRepo := func() (ghrepo.Interface, error) {
137127
httpClient, err := cmdFactory.HttpClient()
@@ -162,15 +152,9 @@ func init() {
162152

163153
repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo
164154

165-
RootCmd.AddCommand(repoCmd.Cmd)
166-
repoCmd.Cmd.AddCommand(repoViewCmd.NewCmdView(&repoResolvingCmdFactory, nil))
167-
repoCmd.Cmd.AddCommand(repoForkCmd.NewCmdFork(&repoResolvingCmdFactory, nil))
168-
repoCmd.Cmd.AddCommand(repoCloneCmd.NewCmdClone(cmdFactory, nil))
169-
repoCmd.Cmd.AddCommand(repoCreateCmd.NewCmdCreate(cmdFactory, nil))
170-
repoCmd.Cmd.AddCommand(creditsCmd.NewCmdRepoCredits(&repoResolvingCmdFactory, nil))
171-
172155
RootCmd.AddCommand(prCmd.NewCmdPR(&repoResolvingCmdFactory))
173156
RootCmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory))
157+
RootCmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory))
174158
RootCmd.AddCommand(creditsCmd.NewCmdCredits(cmdFactory, nil))
175159
}
176160

pkg/cmd/gist/gist.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package gist
2+
3+
import (
4+
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
5+
"github.com/cli/cli/pkg/cmdutil"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func NewCmdGist(f *cmdutil.Factory) *cobra.Command {
10+
cmd := &cobra.Command{
11+
Use: "gist",
12+
Short: "Create gists",
13+
Long: `Work with GitHub gists.`,
14+
}
15+
16+
cmd.AddCommand(gistCreateCmd.NewCmdCreate(f, nil))
17+
18+
return cmd
19+
}

pkg/cmd/repo/repo.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,40 @@ package repo
22

33
import (
44
"github.com/MakeNowJust/heredoc"
5+
repoCloneCmd "github.com/cli/cli/pkg/cmd/repo/clone"
6+
repoCreateCmd "github.com/cli/cli/pkg/cmd/repo/create"
7+
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
8+
repoForkCmd "github.com/cli/cli/pkg/cmd/repo/fork"
9+
repoViewCmd "github.com/cli/cli/pkg/cmd/repo/view"
10+
"github.com/cli/cli/pkg/cmdutil"
511
"github.com/spf13/cobra"
612
)
713

8-
var Cmd = &cobra.Command{
9-
Use: "repo <command>",
10-
Short: "Create, clone, fork, and view repositories",
11-
Long: `Work with GitHub repositories`,
12-
Example: heredoc.Doc(`
13-
$ gh repo create
14-
$ gh repo clone cli/cli
15-
$ gh repo view --web
16-
`),
17-
Annotations: map[string]string{
18-
"IsCore": "true",
19-
"help:arguments": `
20-
A repository can be supplied as an argument in any of the following formats:
21-
- "OWNER/REPO"
22-
- by URL, e.g. "https://github.com/OWNER/REPO"`},
14+
func NewCmdRepo(f *cmdutil.Factory) *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "repo <command>",
17+
Short: "Create, clone, fork, and view repositories",
18+
Long: `Work with GitHub repositories`,
19+
Example: heredoc.Doc(`
20+
$ gh repo create
21+
$ gh repo clone cli/cli
22+
$ gh repo view --web
23+
`),
24+
Annotations: map[string]string{
25+
"IsCore": "true",
26+
"help:arguments": heredoc.Doc(`
27+
A repository can be supplied as an argument in any of the following formats:
28+
- "OWNER/REPO"
29+
- by URL, e.g. "https://github.com/OWNER/REPO"
30+
`),
31+
},
32+
}
33+
34+
cmd.AddCommand(repoViewCmd.NewCmdView(f, nil))
35+
cmd.AddCommand(repoForkCmd.NewCmdFork(f, nil))
36+
cmd.AddCommand(repoCloneCmd.NewCmdClone(f, nil))
37+
cmd.AddCommand(repoCreateCmd.NewCmdCreate(f, nil))
38+
cmd.AddCommand(creditsCmd.NewCmdRepoCredits(f, nil))
39+
40+
return cmd
2341
}

0 commit comments

Comments
 (0)