Improve CLI and messages (#26341)
Follow the CLI refactoring 1. Remove the "checkCommandFlags" helper 2. Unify the web startup message, make them have consistent names as `./gitea help` 3. Fine tune some other messages (see the diff)
This commit is contained in:
parent
ecb04cc324
commit
4f513474dc
2 changed files with 17 additions and 52 deletions
37
cmd/main.go
37
cmd/main.go
|
@ -6,8 +6,6 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -58,7 +56,6 @@ func appGlobalFlags() []cli.Flag {
|
|||
return []cli.Flag{
|
||||
// make the builtin flags at the top
|
||||
helpFlag,
|
||||
cli.VersionFlag,
|
||||
|
||||
// shared configuration flags, they are for global and for each sub-command at the same time
|
||||
// eg: such command is valid: "./gitea --config /tmp/app.ini web --config /tmp/app.ini", while it's discouraged indeed
|
||||
|
@ -120,36 +117,6 @@ func prepareWorkPathAndCustomConf(action cli.ActionFunc) func(ctx *cli.Context)
|
|||
}
|
||||
}
|
||||
|
||||
func reflectGet(v any, fieldName string) any {
|
||||
e := reflect.ValueOf(v).Elem()
|
||||
return e.FieldByName(fieldName).Interface()
|
||||
}
|
||||
|
||||
// https://cli.urfave.org/migrate-v1-to-v2/#flag-aliases-are-done-differently
|
||||
// Sadly v2 doesn't warn you if a comma is in the name. (https://github.com/urfave/cli/issues/1103)
|
||||
func checkCommandFlags(c any) bool {
|
||||
var cmds []*cli.Command
|
||||
if app, ok := c.(*cli.App); ok {
|
||||
cmds = app.Commands
|
||||
} else {
|
||||
cmds = c.(*cli.Command).Subcommands
|
||||
}
|
||||
ok := true
|
||||
for _, cmd := range cmds {
|
||||
for _, flag := range cmd.Flags {
|
||||
flagName := reflectGet(flag, "Name").(string)
|
||||
if strings.Contains(flagName, ",") {
|
||||
ok = false
|
||||
log.Error("cli.Flag can't have comma in its Name: %q, use Aliases instead", flagName)
|
||||
}
|
||||
}
|
||||
if !checkCommandFlags(cmd) {
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func NewMainApp() *cli.App {
|
||||
app := cli.NewApp()
|
||||
app.EnableBashCompletion = true
|
||||
|
@ -187,6 +154,7 @@ func NewMainApp() *cli.App {
|
|||
app.DefaultCommand = CmdWeb.Name
|
||||
|
||||
globalFlags := appGlobalFlags()
|
||||
app.Flags = append(app.Flags, cli.VersionFlag)
|
||||
app.Flags = append(app.Flags, globalFlags...)
|
||||
app.HideHelp = true // use our own help action to show helps (with more information like default config)
|
||||
app.Before = PrepareConsoleLoggerLevel(log.INFO)
|
||||
|
@ -196,8 +164,5 @@ func NewMainApp() *cli.App {
|
|||
app.Commands = append(app.Commands, subCmdWithConfig...)
|
||||
app.Commands = append(app.Commands, subCmdStandalone...)
|
||||
|
||||
if !checkCommandFlags(app) {
|
||||
panic("some flags are incorrect") // this is a runtime check to help developers
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue