Skip to content

Commit 4f51347

Browse files
authored
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)
1 parent ecb04cc commit 4f51347

File tree

2 files changed

+17
-52
lines changed

2 files changed

+17
-52
lines changed

cmd/main.go

+1-36
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package cmd
66
import (
77
"fmt"
88
"os"
9-
"reflect"
10-
"strings"
119

1210
"code.gitea.io/gitea/modules/log"
1311
"code.gitea.io/gitea/modules/setting"
@@ -58,7 +56,6 @@ func appGlobalFlags() []cli.Flag {
5856
return []cli.Flag{
5957
// make the builtin flags at the top
6058
helpFlag,
61-
cli.VersionFlag,
6259

6360
// shared configuration flags, they are for global and for each sub-command at the same time
6461
// 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)
120117
}
121118
}
122119

123-
func reflectGet(v any, fieldName string) any {
124-
e := reflect.ValueOf(v).Elem()
125-
return e.FieldByName(fieldName).Interface()
126-
}
127-
128-
// https://cli.urfave.org/migrate-v1-to-v2/#flag-aliases-are-done-differently
129-
// Sadly v2 doesn't warn you if a comma is in the name. (https://github.com/urfave/cli/issues/1103)
130-
func checkCommandFlags(c any) bool {
131-
var cmds []*cli.Command
132-
if app, ok := c.(*cli.App); ok {
133-
cmds = app.Commands
134-
} else {
135-
cmds = c.(*cli.Command).Subcommands
136-
}
137-
ok := true
138-
for _, cmd := range cmds {
139-
for _, flag := range cmd.Flags {
140-
flagName := reflectGet(flag, "Name").(string)
141-
if strings.Contains(flagName, ",") {
142-
ok = false
143-
log.Error("cli.Flag can't have comma in its Name: %q, use Aliases instead", flagName)
144-
}
145-
}
146-
if !checkCommandFlags(cmd) {
147-
ok = false
148-
}
149-
}
150-
return ok
151-
}
152-
153120
func NewMainApp() *cli.App {
154121
app := cli.NewApp()
155122
app.EnableBashCompletion = true
@@ -187,6 +154,7 @@ func NewMainApp() *cli.App {
187154
app.DefaultCommand = CmdWeb.Name
188155

189156
globalFlags := appGlobalFlags()
157+
app.Flags = append(app.Flags, cli.VersionFlag)
190158
app.Flags = append(app.Flags, globalFlags...)
191159
app.HideHelp = true // use our own help action to show helps (with more information like default config)
192160
app.Before = PrepareConsoleLoggerLevel(log.INFO)
@@ -196,8 +164,5 @@ func NewMainApp() *cli.App {
196164
app.Commands = append(app.Commands, subCmdWithConfig...)
197165
app.Commands = append(app.Commands, subCmdStandalone...)
198166

199-
if !checkCommandFlags(app) {
200-
panic("some flags are incorrect") // this is a runtime check to help developers
201-
}
202167
return app
203168
}

cmd/web.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,18 @@ func createPIDFile(pidPath string) {
107107
}
108108
}
109109

110-
func serveInstall(ctx *cli.Context) error {
110+
func showWebStartupMessage(msg string) {
111111
log.Info("Gitea version: %s%s", setting.AppVer, setting.AppBuiltWith)
112-
log.Info("App path: %s", setting.AppPath)
113-
log.Info("Work path: %s", setting.AppWorkPath)
114-
log.Info("Custom path: %s", setting.CustomPath)
115-
log.Info("Config file: %s", setting.CustomConf)
116-
log.Info("Prepare to run install page")
112+
log.Info("* RunMode: %s", setting.RunMode)
113+
log.Info("* AppPath: %s", setting.AppPath)
114+
log.Info("* WorkPath: %s", setting.AppWorkPath)
115+
log.Info("* CustomPath: %s", setting.CustomPath)
116+
log.Info("* ConfigFile: %s", setting.CustomConf)
117+
log.Info("%s", msg)
118+
}
119+
120+
func serveInstall(ctx *cli.Context) error {
121+
showWebStartupMessage("Prepare to run install page")
117122

118123
routers.InitWebInstallPage(graceful.GetManager().HammerContext())
119124

@@ -150,29 +155,24 @@ func serveInstalled(ctx *cli.Context) error {
150155
setting.LoadCommonSettings()
151156
setting.MustInstalled()
152157

153-
log.Info("Gitea version: %s%s", setting.AppVer, setting.AppBuiltWith)
154-
log.Info("App path: %s", setting.AppPath)
155-
log.Info("Work path: %s", setting.AppWorkPath)
156-
log.Info("Custom path: %s", setting.CustomPath)
157-
log.Info("Config file: %s", setting.CustomConf)
158-
log.Info("Run mode: %s", setting.RunMode)
159-
log.Info("Prepare to run web server")
158+
showWebStartupMessage("Prepare to run web server")
160159

161160
if setting.AppWorkPathMismatch {
162161
log.Error("WORK_PATH from config %q doesn't match other paths from environment variables or command arguments. "+
163-
"Only WORK_PATH in config should be set and used. Please remove the other outdated work paths from environment variables and command arguments", setting.CustomConf)
162+
"Only WORK_PATH in config should be set and used. Please make sure the path in config file is correct, "+
163+
"remove the other outdated work paths from environment variables and command arguments", setting.CustomConf)
164164
}
165165

166166
rootCfg := setting.CfgProvider
167167
if rootCfg.Section("").Key("WORK_PATH").String() == "" {
168168
saveCfg, err := rootCfg.PrepareSaving()
169169
if err != nil {
170-
log.Error("Unable to prepare saving WORK_PATH=%s to config %q: %v\nYou must set it manually, otherwise there might be bugs when accessing the git repositories.", setting.AppWorkPath, setting.CustomConf, err)
170+
log.Error("Unable to prepare saving WORK_PATH=%s to config %q: %v\nYou should set it manually, otherwise there might be bugs when accessing the git repositories.", setting.AppWorkPath, setting.CustomConf, err)
171171
} else {
172172
rootCfg.Section("").Key("WORK_PATH").SetValue(setting.AppWorkPath)
173173
saveCfg.Section("").Key("WORK_PATH").SetValue(setting.AppWorkPath)
174174
if err = saveCfg.Save(); err != nil {
175-
log.Error("Unable to update WORK_PATH=%s to config %q: %v\nYou must set it manually, otherwise there might be bugs when accessing the git repositories.", setting.AppWorkPath, setting.CustomConf, err)
175+
log.Error("Unable to update WORK_PATH=%s to config %q: %v\nYou should set it manually, otherwise there might be bugs when accessing the git repositories.", setting.AppWorkPath, setting.CustomConf, err)
176176
}
177177
}
178178
}

0 commit comments

Comments
 (0)