loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improve "gitea doctor" sub-command and fix "help" commands (#26072)

Replace #21790

And close #25965 by the way (it needs a separate fix for 1.20)

Major changes:

1. Move "gitea convert" to "gitea doctor conver". The old "gitea doctor"
still works as a hidden sub-command (to avoid breaking)
2. Do not write "doctor.log" by default, it's not useful in most cases
and causes bugs like 25965
3. Improve documents
4. Fix the "help" commands. Before, the "./gitea doctor" can't show the
sub-command help correctly (regression of the last cli/v2 refactoring)

After this PR:

```
./gitea help # show all sub-commands for the app
./gitea doctor # show the sub-commands for the "doctor"
./gitea doctor help # show the sub-commands for the "doctor", as above
```

authored by

wxiaoguang and committed by
GitHub
1ce51a55 7a687cac

+62 -76
+4 -4
cmd/convert.go cmd/doctor_convert.go
··· 13 13 "github.com/urfave/cli/v2" 14 14 ) 15 15 16 - // CmdConvert represents the available convert sub-command. 17 - var CmdConvert = &cli.Command{ 16 + // cmdDoctorConvert represents the available convert sub-command. 17 + var cmdDoctorConvert = &cli.Command{ 18 18 Name: "convert", 19 19 Usage: "Convert the database", 20 20 Description: "A command to convert an existing MySQL database from utf8 to utf8mb4 or MSSQL database from varchar to nvarchar", 21 - Action: runConvert, 21 + Action: runDoctorConvert, 22 22 } 23 23 24 - func runConvert(ctx *cli.Context) error { 24 + func runDoctorConvert(ctx *cli.Context) error { 25 25 stdCtx, cancel := installSignals() 26 26 defer cancel() 27 27
+18 -16
cmd/doctor.go
··· 22 22 "xorm.io/xorm" 23 23 ) 24 24 25 - // CmdDoctor represents the available doctor sub-command. 26 - var CmdDoctor = &cli.Command{ 27 - Name: "doctor", 25 + var cmdDoctorCheck = &cli.Command{ 26 + Name: "check", 28 27 Usage: "Diagnose and optionally fix problems", 29 28 Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.", 30 - Action: runDoctor, 29 + Action: runDoctorCheck, 31 30 Flags: []cli.Flag{ 32 31 &cli.BoolFlag{ 33 32 Name: "list", ··· 51 50 }, 52 51 &cli.StringFlag{ 53 52 Name: "log-file", 54 - Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`, 53 + Usage: `Name of the log file (no verbose log output by default). Set to "-" to output to stdout`, 55 54 }, 56 55 &cli.BoolFlag{ 57 56 Name: "color", ··· 59 58 Usage: "Use color for outputted information", 60 59 }, 61 60 }, 61 + } 62 + 63 + // CmdDoctor represents the available doctor sub-command. 64 + var CmdDoctor = &cli.Command{ 65 + Name: "doctor", 66 + Usage: "Diagnose and optionally fix problems", 67 + Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.", 68 + 62 69 Subcommands: []*cli.Command{ 70 + cmdDoctorCheck, 63 71 cmdRecreateTable, 72 + cmdDoctorConvert, 64 73 }, 65 74 } 66 75 ··· 133 142 setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr) 134 143 135 144 logFile := ctx.String("log-file") 136 - if !ctx.IsSet("log-file") { 137 - logFile = "doctor.log" 138 - } 139 - 140 - if len(logFile) == 0 { 141 - // if no doctor log-file is set, do not show any log from default logger 142 - return 143 - } 144 - 145 - if logFile == "-" { 145 + if logFile == "" { 146 + return // if no doctor log-file is set, do not show any log from default logger 147 + } else if logFile == "-" { 146 148 setupConsoleLogger(log.TRACE, colorize, os.Stdout) 147 149 } else { 148 150 logFile, _ = filepath.Abs(logFile) ··· 156 158 } 157 159 } 158 160 159 - func runDoctor(ctx *cli.Context) error { 161 + func runDoctorCheck(ctx *cli.Context) error { 160 162 stdCtx, cancel := installSignals() 161 163 defer cancel() 162 164
+14 -7
cmd/main.go
··· 11 11 12 12 "code.gitea.io/gitea/modules/log" 13 13 "code.gitea.io/gitea/modules/setting" 14 + "code.gitea.io/gitea/modules/util" 14 15 15 16 "github.com/urfave/cli/v2" 16 17 ) ··· 23 24 Usage: "Shows a list of commands or help for one command", 24 25 ArgsUsage: "[command]", 25 26 Action: func(c *cli.Context) (err error) { 26 - args := c.Args() 27 - if args.Present() { 28 - err = cli.ShowCommandHelp(c, args.First()) 27 + lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea, {Command:nil} 28 + targetCmdIdx := 0 29 + if c.Command.Name == "help" { 30 + targetCmdIdx = 1 31 + } 32 + if lineage[targetCmdIdx+1].Command != nil { 33 + err = cli.ShowCommandHelp(lineage[targetCmdIdx+1], lineage[targetCmdIdx].Command.Name) 29 34 } else { 30 35 err = cli.ShowAppHelp(c) 31 36 } ··· 94 99 func prepareWorkPathAndCustomConf(action cli.ActionFunc) func(ctx *cli.Context) error { 95 100 return func(ctx *cli.Context) error { 96 101 var args setting.ArgWorkPathAndCustomConf 97 - ctxLineage := ctx.Lineage() 98 - for i := len(ctxLineage) - 1; i >= 0; i-- { 99 - curCtx := ctxLineage[i] 102 + // from children to parent, check the global flags 103 + for _, curCtx := range ctx.Lineage() { 100 104 if curCtx.IsSet("work-path") && args.WorkPath == "" { 101 105 args.WorkPath = curCtx.String("work-path") 102 106 } ··· 159 163 CmdAdmin, 160 164 CmdMigrate, 161 165 CmdKeys, 162 - CmdConvert, 163 166 CmdDoctor, 164 167 CmdManager, 165 168 CmdEmbedded, ··· 169 172 CmdActions, 170 173 cmdHelp(), // the "help" sub-command was used to show the more information for "work path" and "custom config" 171 174 } 175 + 176 + cmdConvert := util.ToPointer(*cmdDoctorConvert) 177 + cmdConvert.Hidden = true // still support the legacy "./gitea doctor" by the hidden sub-command, remove it in next release 178 + subCmdWithConfig = append(subCmdWithConfig, cmdConvert) 172 179 173 180 // these sub-commands do not need the config file, and they do not depend on any path or environment variable. 174 181 subCmdStandalone := []*cli.Command{
+13 -26
docs/content/doc/administration/command-line.en-us.md
··· 388 388 Migrates the database. This command can be used to run other commands before starting the server for the first time. 389 389 This command is idempotent. 390 390 391 - ### convert 391 + ### doctor check 392 392 393 - Converts an existing MySQL database from utf8 to utf8mb4. 393 + Diagnose and potentially fix problems with the current Gitea instance. 394 + Several checks are run by default, but additional ones can be run: 394 395 395 - ### doctor 396 - 397 - Diagnose the problems of current Gitea instance according the given configuration. 398 - Currently there are a check list below: 396 + - `gitea doctor check --list` - will list all the available checks 397 + - `gitea doctor check --all` - will run all available checks 398 + - `gitea doctor check --default` - will run the default checks 399 + - `gitea doctor check --run [check(s),]...` - will run the named checks 399 400 400 - - Check if OpenSSH authorized_keys file id correct 401 - When your Gitea instance support OpenSSH, your Gitea instance binary path will be written to `authorized_keys` 402 - when there is any public key added or changed on your Gitea instance. 403 - Sometimes if you moved or renamed your Gitea binary when upgrade and you haven't run `Update the '.ssh/authorized_keys' file with Gitea SSH keys. (Not needed for the built-in SSH server.)` on your Admin Panel. Then all pull/push via SSH will not be work. 404 - This check will help you to check if it works well. 405 - 406 - For contributors, if you want to add more checks, you can write a new function like `func(ctx *cli.Context) ([]string, error)` and 407 - append it to `doctor.go`. 408 - 409 - ```go 410 - var checklist = []check{ 411 - { 412 - title: "Check if OpenSSH authorized_keys file id correct", 413 - f: runDoctorLocationMoved, 414 - }, 415 - // more checks please append here 416 - } 417 - ``` 418 - 419 - This function will receive a command line context and return a list of details about the problems or error. 401 + Some problems can be automatically fixed by passing the `--fix` option. 402 + Extra logging can be set with `--log-file=...`. 420 403 421 404 #### doctor recreate-table 422 405 ··· 447 430 ``` 448 431 449 432 It is highly recommended to back-up your database before running these commands. 433 + 434 + ### doctor convert 435 + 436 + Converts a MySQL database from utf8 to utf8mb4 or a MSSQL database from varchar to nvarchar. 450 437 451 438 ### manager 452 439
+13 -23
docs/content/doc/administration/command-line.zh-cn.md
··· 361 361 362 362 迁移数据库。该命令可用于在首次启动服务器之前运行其他命令。此命令是幂等的。 363 363 364 - ### convert 365 - 366 - 将现有的 MySQL 数据库从 utf8 转换为 utf8mb4。 367 - 368 - ### doctor 369 - 370 - 根据给定的配置诊断当前 Gitea 实例的问题。目前有以下检查清单: 371 - 372 - - 检查 OpenSSH 的 authorized_keys 文件是否正确 373 - 当您的 Gitea 实例支持 OpenSSH 时,当您的 Gitea 实例添加或更改任何公钥时,Gitea 实例的二进制路径将被写入 `authorized_keys` 文件。 374 - 有时,如果您在升级时移动或重命名了 Gitea 二进制文件,并且您没有在管理面板上运行“使用 Gitea 的 SSH 密钥更新「.ssh/authorized_keys」文件”操作。那么通过 SSH 的所有拉取/推送操作将无法正常工作。 375 - 此检查将帮助您检查它是否正常工作。 364 + ### doctor check 376 365 377 - 对于贡献者,如果您想添加更多的检查项,您可以编写一个新的函数,如 `func(ctx *cli.Context) ([]string, error)`,并将其追加到 `doctor.go` 文件中。 366 + 对 Gitea 实例进行诊断,可以修复一些可修复的问题。 367 + 默认只运行部分检查,额外的检查可以参考: 378 368 379 - ```go 380 - var checklist = []check{ 381 - { 382 - title: "Check if OpenSSH authorized_keys file id correct", 383 - f: runDoctorLocationMoved, 384 - }, 385 - // more checks please append here 386 - } 387 - ``` 369 + - `gitea doctor check --list` - 列出所有可用的检查 370 + - `gitea doctor check --all` - 运行所有可用的检查 371 + - `gitea doctor check --default` - 运行默认的检查 372 + - `gitea doctor check --run [check(s),]...` - 运行指定的名字的检查 388 373 389 - 此函数将接收一个命令行上下文,并返回有关问题或错误的详细信息列表。 374 + 有些问题可以通过设置 `--fix` 选项进行自动修复。 375 + 额外的日志可以通过 `--log-file=...` 进行设置。 390 376 391 377 #### doctor recreate-table 392 378 ··· 415 401 ``` 416 402 417 403 强烈建议在运行这些命令之前备份您的数据库。 404 + 405 + ### doctor convert 406 + 407 + 将现有的 MySQL 数据库从 utf8 转换为 utf8mb4,或者把 MSSQL 数据库从 varchar 转换为 nvarchar。 418 408 419 409 ### manager 420 410