Skip to content

Commit 00ba826

Browse files
GiteaBotwxiaoguang
andauthored
Fix CLI sub-command handling (#25501) (#25517)
Backport #25501 by @wxiaoguang A regression of #25330 : The nil "Action" should be treated as "help" In old releases: `./gitea admin` show helps After #25330: `./gitea admin` panics (although the code returned `nil` if action is nil, but Golang's quirk is: nil in interface is not nil) With this PR: `./gitea admin` shows helps as the old releases. Co-authored-by: wxiaoguang <[email protected]>
1 parent 9bbb4d8 commit 00ba826

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

main.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func init() {
4747
// ./gitea -h
4848
// ./gitea web help
4949
// ./gitea web -h (due to cli lib limitation, this won't call our cmdHelp, so no extra info)
50-
// ./gitea admin help auth
50+
// ./gitea admin
51+
// ./gitea admin help
52+
// ./gitea admin auth help
5153
// ./gitea -c /tmp/app.ini -h
5254
// ./gitea -c /tmp/app.ini help
5355
// ./gitea help -c /tmp/app.ini
@@ -156,11 +158,7 @@ func prepareSubcommands(command *cli.Command, defaultFlags []cli.Flag) {
156158

157159
// prepareWorkPathAndCustomConf wraps the Action to prepare the work path and custom config
158160
// It can't use "Before", because each level's sub-command's Before will be called one by one, so the "init" would be done multiple times
159-
func prepareWorkPathAndCustomConf(a any) func(ctx *cli.Context) error {
160-
if a == nil {
161-
return nil
162-
}
163-
action := a.(func(*cli.Context) error)
161+
func prepareWorkPathAndCustomConf(action any) func(ctx *cli.Context) error {
164162
return func(ctx *cli.Context) error {
165163
var args setting.ArgWorkPathAndCustomConf
166164
curCtx := ctx
@@ -177,10 +175,11 @@ func prepareWorkPathAndCustomConf(a any) func(ctx *cli.Context) error {
177175
curCtx = curCtx.Parent()
178176
}
179177
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
180-
if ctx.Bool("help") {
178+
if ctx.Bool("help") || action == nil {
179+
// the default behavior of "urfave/cli": "nil action" means "show help"
181180
return cmdHelp.Action.(func(ctx *cli.Context) error)(ctx)
182181
}
183-
return action(ctx)
182+
return action.(func(*cli.Context) error)(ctx)
184183
}
185184
}
186185

0 commit comments

Comments
 (0)