1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-24 08:57:03 -05:00

Fix CLI sub-command handling (#25501)

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.
This commit is contained in:
wxiaoguang 2023-06-26 13:45:27 +08:00 committed by GitHub
parent 50dc2d5f5f
commit e409e14bdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

15
main.go
View file

@ -47,7 +47,9 @@ func init() {
// ./gitea -h // ./gitea -h
// ./gitea web help // ./gitea web help
// ./gitea web -h (due to cli lib limitation, this won't call our cmdHelp, so no extra info) // ./gitea web -h (due to cli lib limitation, this won't call our cmdHelp, so no extra info)
// ./gitea admin help auth // ./gitea admin
// ./gitea admin help
// ./gitea admin auth help
// ./gitea -c /tmp/app.ini -h // ./gitea -c /tmp/app.ini -h
// ./gitea -c /tmp/app.ini help // ./gitea -c /tmp/app.ini help
// ./gitea help -c /tmp/app.ini // ./gitea help -c /tmp/app.ini
@ -156,11 +158,7 @@ func prepareSubcommands(command *cli.Command, defaultFlags []cli.Flag) {
// prepareWorkPathAndCustomConf wraps the Action to prepare the work path and custom config // prepareWorkPathAndCustomConf wraps the Action to prepare the work path and custom config
// 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 // 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
func prepareWorkPathAndCustomConf(a any) func(ctx *cli.Context) error { func prepareWorkPathAndCustomConf(action any) func(ctx *cli.Context) error {
if a == nil {
return nil
}
action := a.(func(*cli.Context) error)
return func(ctx *cli.Context) error { return func(ctx *cli.Context) error {
var args setting.ArgWorkPathAndCustomConf var args setting.ArgWorkPathAndCustomConf
curCtx := ctx curCtx := ctx
@ -177,10 +175,11 @@ func prepareWorkPathAndCustomConf(a any) func(ctx *cli.Context) error {
curCtx = curCtx.Parent() curCtx = curCtx.Parent()
} }
setting.InitWorkPathAndCommonConfig(os.Getenv, args) setting.InitWorkPathAndCommonConfig(os.Getenv, args)
if ctx.Bool("help") { if ctx.Bool("help") || action == nil {
// the default behavior of "urfave/cli": "nil action" means "show help"
return cmdHelp.Action.(func(ctx *cli.Context) error)(ctx) return cmdHelp.Action.(func(ctx *cli.Context) error)(ctx)
} }
return action(ctx) return action.(func(*cli.Context) error)(ctx)
} }
} }