Skip to content

Commit 435123f

Browse files
Switch cmd/ to use constructor functions. (#36962)
This is a step towards potentially splitting command groups into their own folders to clean up `cmd/` as one folder for all cli commands. Returning fresh command instances will also aid in adding tests as you don't need to concern yourself with the whole command tree being one mutable variable. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent bb1e22b commit 435123f

26 files changed

Lines changed: 651 additions & 578 deletions

cmd/actions.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import (
1313
"github.com/urfave/cli/v3"
1414
)
1515

16-
var (
17-
// CmdActions represents the available actions sub-commands.
18-
CmdActions = &cli.Command{
16+
func newActionsCommand() *cli.Command {
17+
return &cli.Command{
1918
Name: "actions",
2019
Usage: "Manage Gitea Actions",
2120
Commands: []*cli.Command{
22-
subcmdActionsGenRunnerToken,
21+
newActionsGenerateRunnerTokenCommand(),
2322
},
2423
}
24+
}
2525

26-
subcmdActionsGenRunnerToken = &cli.Command{
26+
func newActionsGenerateRunnerTokenCommand() *cli.Command {
27+
return &cli.Command{
2728
Name: "generate-runner-token",
2829
Usage: "Generate a new token for a runner to use to register with the server",
2930
Action: runGenerateActionsRunnerToken,
@@ -37,7 +38,7 @@ var (
3738
},
3839
},
3940
}
40-
)
41+
}
4142

4243
func runGenerateActionsRunnerToken(ctx context.Context, c *cli.Command) error {
4344
setting.MustInstalled()

cmd/admin.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,41 @@ import (
1818
"github.com/urfave/cli/v3"
1919
)
2020

21-
var (
22-
// CmdAdmin represents the available admin sub-command.
23-
CmdAdmin = &cli.Command{
21+
func newAdminCommand() *cli.Command {
22+
return &cli.Command{
2423
Name: "admin",
2524
Usage: "Perform common administrative operations",
2625
Commands: []*cli.Command{
27-
subcmdUser,
28-
subcmdRepoSyncReleases,
29-
subcmdRegenerate,
30-
subcmdAuth,
31-
subcmdSendMail,
26+
newUserCommand(),
27+
newRepoSyncReleasesCommand(),
28+
newRegenerateCommand(),
29+
newAuthCommand(),
30+
newSendMailCommand(),
3231
},
3332
}
33+
}
3434

35-
subcmdRepoSyncReleases = &cli.Command{
35+
func newRepoSyncReleasesCommand() *cli.Command {
36+
return &cli.Command{
3637
Name: "repo-sync-releases",
3738
Usage: "Synchronize repository releases with tags",
3839
Action: runRepoSyncReleases,
3940
}
41+
}
4042

41-
subcmdRegenerate = &cli.Command{
43+
func newRegenerateCommand() *cli.Command {
44+
return &cli.Command{
4245
Name: "regenerate",
4346
Usage: "Regenerate specific files",
4447
Commands: []*cli.Command{
45-
microcmdRegenHooks,
46-
microcmdRegenKeys,
48+
newRegenerateHooksCommand(),
49+
newRegenerateKeysCommand(),
4750
},
4851
}
52+
}
4953

50-
subcmdAuth = &cli.Command{
54+
func newAuthCommand() *cli.Command {
55+
return &cli.Command{
5156
Name: "auth",
5257
Usage: "Modify external auth providers",
5358
Commands: []*cli.Command{
@@ -59,12 +64,14 @@ var (
5964
microcmdAuthUpdateLdapSimpleAuth(),
6065
microcmdAuthAddSMTP(),
6166
microcmdAuthUpdateSMTP(),
62-
microcmdAuthList,
63-
microcmdAuthDelete,
67+
newAuthListCommand(),
68+
newAuthDeleteCommand(),
6469
},
6570
}
71+
}
6672

67-
subcmdSendMail = &cli.Command{
73+
func newSendMailCommand() *cli.Command {
74+
return &cli.Command{
6875
Name: "sendmail",
6976
Usage: "Send a message to all users",
7077
Action: runSendMail,
@@ -86,7 +93,7 @@ var (
8693
},
8794
},
8895
}
89-
)
96+
}
9097

9198
func idFlag() *cli.Int64Flag {
9299
return &cli.Int64Flag{

cmd/admin_auth.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ import (
1717
"github.com/urfave/cli/v3"
1818
)
1919

20-
var (
21-
microcmdAuthDelete = &cli.Command{
20+
func newAuthDeleteCommand() *cli.Command {
21+
return &cli.Command{
2222
Name: "delete",
2323
Usage: "Delete specific auth source",
2424
Flags: []cli.Flag{idFlag()},
2525
Action: runDeleteAuth,
2626
}
27-
microcmdAuthList = &cli.Command{
27+
}
28+
29+
func newAuthListCommand() *cli.Command {
30+
return &cli.Command{
2831
Name: "list",
2932
Usage: "List auth sources",
3033
Action: runListAuth,
@@ -55,7 +58,7 @@ var (
5558
},
5659
},
5760
}
58-
)
61+
}
5962

6063
func runListAuth(ctx context.Context, c *cli.Command) error {
6164
if err := initDB(ctx); err != nil {

cmd/admin_regenerate.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ import (
1313
"github.com/urfave/cli/v3"
1414
)
1515

16-
var (
17-
microcmdRegenHooks = &cli.Command{
16+
func newRegenerateHooksCommand() *cli.Command {
17+
return &cli.Command{
1818
Name: "hooks",
1919
Usage: "Regenerate git-hooks",
2020
Action: runRegenerateHooks,
2121
}
22+
}
2223

23-
microcmdRegenKeys = &cli.Command{
24+
func newRegenerateKeysCommand() *cli.Command {
25+
return &cli.Command{
2426
Name: "keys",
2527
Usage: "Regenerate authorized_keys file",
2628
Action: runRegenerateKeys,
2729
}
28-
)
30+
}
2931

3032
func runRegenerateHooks(ctx context.Context, _ *cli.Command) error {
3133
if err := initDB(ctx); err != nil {

cmd/admin_user.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import (
77
"github.com/urfave/cli/v3"
88
)
99

10-
var subcmdUser = &cli.Command{
11-
Name: "user",
12-
Usage: "Modify users",
13-
Commands: []*cli.Command{
14-
microcmdUserCreate(),
15-
microcmdUserList,
16-
microcmdUserChangePassword(),
17-
microcmdUserDelete(),
18-
microcmdUserGenerateAccessToken,
19-
microcmdUserMustChangePassword(),
20-
},
10+
func newUserCommand() *cli.Command {
11+
return &cli.Command{
12+
Name: "user",
13+
Usage: "Modify users",
14+
Commands: []*cli.Command{
15+
microcmdUserCreate(),
16+
newUserListCommand(),
17+
microcmdUserChangePassword(),
18+
microcmdUserDelete(),
19+
newUserGenerateAccessTokenCommand(),
20+
microcmdUserMustChangePassword(),
21+
},
22+
}
2123
}

cmd/admin_user_generate_access_token.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@ import (
1414
"github.com/urfave/cli/v3"
1515
)
1616

17-
var microcmdUserGenerateAccessToken = &cli.Command{
18-
Name: "generate-access-token",
19-
Usage: "Generate an access token for a specific user",
20-
Flags: []cli.Flag{
21-
&cli.StringFlag{
22-
Name: "username",
23-
Aliases: []string{"u"},
24-
Usage: "Username",
17+
func newUserGenerateAccessTokenCommand() *cli.Command {
18+
return &cli.Command{
19+
Name: "generate-access-token",
20+
Usage: "Generate an access token for a specific user",
21+
Flags: []cli.Flag{
22+
&cli.StringFlag{
23+
Name: "username",
24+
Aliases: []string{"u"},
25+
Usage: "Username",
26+
},
27+
&cli.StringFlag{
28+
Name: "token-name",
29+
Aliases: []string{"t"},
30+
Usage: "Token name",
31+
Value: "gitea-admin",
32+
},
33+
&cli.BoolFlag{
34+
Name: "raw",
35+
Usage: "Display only the token value",
36+
},
37+
&cli.StringFlag{
38+
Name: "scopes",
39+
Value: "all",
40+
Usage: `Comma separated list of scopes to apply to access token, examples: "all", "public-only,read:issue", "write:repository,write:user"`,
41+
},
2542
},
26-
&cli.StringFlag{
27-
Name: "token-name",
28-
Aliases: []string{"t"},
29-
Usage: "Token name",
30-
Value: "gitea-admin",
31-
},
32-
&cli.BoolFlag{
33-
Name: "raw",
34-
Usage: "Display only the token value",
35-
},
36-
&cli.StringFlag{
37-
Name: "scopes",
38-
Value: "all",
39-
Usage: `Comma separated list of scopes to apply to access token, examples: "all", "public-only,read:issue", "write:repository,write:user"`,
40-
},
41-
},
42-
Action: runGenerateAccessToken,
43+
Action: runGenerateAccessToken,
44+
}
4345
}
4446

4547
func runGenerateAccessToken(ctx context.Context, c *cli.Command) error {

cmd/admin_user_list.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ import (
1414
"github.com/urfave/cli/v3"
1515
)
1616

17-
var microcmdUserList = &cli.Command{
18-
Name: "list",
19-
Usage: "List users",
20-
Action: runListUsers,
21-
Flags: []cli.Flag{
22-
&cli.BoolFlag{
23-
Name: "admin",
24-
Usage: "List only admin users",
17+
func newUserListCommand() *cli.Command {
18+
return &cli.Command{
19+
Name: "list",
20+
Usage: "List users",
21+
Action: runListUsers,
22+
Flags: []cli.Flag{
23+
&cli.BoolFlag{
24+
Name: "admin",
25+
Usage: "List only admin users",
26+
},
2527
},
26-
},
28+
}
2729
}
2830

2931
func runListUsers(ctx context.Context, c *cli.Command) error {

cmd/docs.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ import (
1313
"github.com/urfave/cli/v3"
1414
)
1515

16-
// CmdDocs represents the available docs sub-command.
17-
var CmdDocs = &cli.Command{
18-
Name: "docs",
19-
Usage: "Output CLI documentation",
20-
Description: "A command to output Gitea's CLI documentation, optionally to a file.",
21-
Action: runDocs,
22-
Flags: []cli.Flag{
23-
&cli.BoolFlag{
24-
Name: "man",
25-
Usage: "Output man pages instead",
16+
func newDocsCommand() *cli.Command {
17+
return &cli.Command{
18+
Name: "docs",
19+
Usage: "Output CLI documentation",
20+
Description: "A command to output Gitea's CLI documentation, optionally to a file.",
21+
Action: runDocs,
22+
Flags: []cli.Flag{
23+
&cli.BoolFlag{
24+
Name: "man",
25+
Usage: "Output man pages instead",
26+
},
27+
&cli.StringFlag{
28+
Name: "output",
29+
Aliases: []string{"o"},
30+
Usage: "Path to output to instead of stdout (will overwrite if exists)",
31+
},
2632
},
27-
&cli.StringFlag{
28-
Name: "output",
29-
Aliases: []string{"o"},
30-
Usage: "Path to output to instead of stdout (will overwrite if exists)",
31-
},
32-
},
33+
}
3334
}
3435

3536
func runDocs(_ context.Context, cmd *cli.Command) error {

0 commit comments

Comments
 (0)