+++ /dev/null
-// Copyright 2019 The Gitea Authors. All rights reserved.
-// SPDX-License-Identifier: MIT
-
-package cmd
-
-import (
- "fmt"
-
- "code.gitea.io/gitea/models/db"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
-
- "github.com/urfave/cli/v2"
-)
-
-// CmdConvert represents the available convert sub-command.
-var CmdConvert = &cli.Command{
- Name: "convert",
- Usage: "Convert the database",
- Description: "A command to convert an existing MySQL database from utf8 to utf8mb4 or MSSQL database from varchar to nvarchar",
- Action: runConvert,
-}
-
-func runConvert(ctx *cli.Context) error {
- stdCtx, cancel := installSignals()
- defer cancel()
-
- if err := initDB(stdCtx); err != nil {
- return err
- }
-
- log.Info("AppPath: %s", setting.AppPath)
- log.Info("AppWorkPath: %s", setting.AppWorkPath)
- log.Info("Custom path: %s", setting.CustomPath)
- log.Info("Log path: %s", setting.Log.RootPath)
- log.Info("Configuration file: %s", setting.CustomConf)
-
- switch {
- case setting.Database.Type.IsMySQL():
- if err := db.ConvertUtf8ToUtf8mb4(); err != nil {
- log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err)
- return err
- }
- fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")
- case setting.Database.Type.IsMSSQL():
- if err := db.ConvertVarcharToNVarchar(); err != nil {
- log.Fatal("Failed to convert database from varchar to nvarchar: %v", err)
- return err
- }
- fmt.Println("Converted successfully, please confirm your database's all columns character is NVARCHAR now")
- default:
- fmt.Println("This command can only be used with a MySQL or MSSQL database")
- }
-
- return nil
-}
"xorm.io/xorm"
)
-// CmdDoctor represents the available doctor sub-command.
-var CmdDoctor = &cli.Command{
- Name: "doctor",
+var cmdDoctorCheck = &cli.Command{
+ Name: "check",
Usage: "Diagnose and optionally fix problems",
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.",
- Action: runDoctor,
+ Action: runDoctorCheck,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "list",
},
&cli.StringFlag{
Name: "log-file",
- Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`,
+ Usage: `Name of the log file (no verbose log output by default). Set to "-" to output to stdout`,
},
&cli.BoolFlag{
Name: "color",
Usage: "Use color for outputted information",
},
},
+}
+
+// CmdDoctor represents the available doctor sub-command.
+var CmdDoctor = &cli.Command{
+ Name: "doctor",
+ Usage: "Diagnose and optionally fix problems",
+ 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.",
+
Subcommands: []*cli.Command{
+ cmdDoctorCheck,
cmdRecreateTable,
+ cmdDoctorConvert,
},
}
setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr)
logFile := ctx.String("log-file")
- if !ctx.IsSet("log-file") {
- logFile = "doctor.log"
- }
-
- if len(logFile) == 0 {
- // if no doctor log-file is set, do not show any log from default logger
- return
- }
-
- if logFile == "-" {
+ if logFile == "" {
+ return // if no doctor log-file is set, do not show any log from default logger
+ } else if logFile == "-" {
setupConsoleLogger(log.TRACE, colorize, os.Stdout)
} else {
logFile, _ = filepath.Abs(logFile)
}
}
-func runDoctor(ctx *cli.Context) error {
+func runDoctorCheck(ctx *cli.Context) error {
stdCtx, cancel := installSignals()
defer cancel()
--- /dev/null
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package cmd
+
+import (
+ "fmt"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/urfave/cli/v2"
+)
+
+// cmdDoctorConvert represents the available convert sub-command.
+var cmdDoctorConvert = &cli.Command{
+ Name: "convert",
+ Usage: "Convert the database",
+ Description: "A command to convert an existing MySQL database from utf8 to utf8mb4 or MSSQL database from varchar to nvarchar",
+ Action: runDoctorConvert,
+}
+
+func runDoctorConvert(ctx *cli.Context) error {
+ stdCtx, cancel := installSignals()
+ defer cancel()
+
+ if err := initDB(stdCtx); err != nil {
+ return err
+ }
+
+ log.Info("AppPath: %s", setting.AppPath)
+ log.Info("AppWorkPath: %s", setting.AppWorkPath)
+ log.Info("Custom path: %s", setting.CustomPath)
+ log.Info("Log path: %s", setting.Log.RootPath)
+ log.Info("Configuration file: %s", setting.CustomConf)
+
+ switch {
+ case setting.Database.Type.IsMySQL():
+ if err := db.ConvertUtf8ToUtf8mb4(); err != nil {
+ log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err)
+ return err
+ }
+ fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")
+ case setting.Database.Type.IsMSSQL():
+ if err := db.ConvertVarcharToNVarchar(); err != nil {
+ log.Fatal("Failed to convert database from varchar to nvarchar: %v", err)
+ return err
+ }
+ fmt.Println("Converted successfully, please confirm your database's all columns character is NVARCHAR now")
+ default:
+ fmt.Println("This command can only be used with a MySQL or MSSQL database")
+ }
+
+ return nil
+}
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
"github.com/urfave/cli/v2"
)
Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]",
Action: func(c *cli.Context) (err error) {
- args := c.Args()
- if args.Present() {
- err = cli.ShowCommandHelp(c, args.First())
+ lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea, {Command:nil}
+ targetCmdIdx := 0
+ if c.Command.Name == "help" {
+ targetCmdIdx = 1
+ }
+ if lineage[targetCmdIdx+1].Command != nil {
+ err = cli.ShowCommandHelp(lineage[targetCmdIdx+1], lineage[targetCmdIdx].Command.Name)
} else {
err = cli.ShowAppHelp(c)
}
func prepareWorkPathAndCustomConf(action cli.ActionFunc) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error {
var args setting.ArgWorkPathAndCustomConf
- ctxLineage := ctx.Lineage()
- for i := len(ctxLineage) - 1; i >= 0; i-- {
- curCtx := ctxLineage[i]
+ // from children to parent, check the global flags
+ for _, curCtx := range ctx.Lineage() {
if curCtx.IsSet("work-path") && args.WorkPath == "" {
args.WorkPath = curCtx.String("work-path")
}
CmdAdmin,
CmdMigrate,
CmdKeys,
- CmdConvert,
CmdDoctor,
CmdManager,
CmdEmbedded,
cmdHelp(), // the "help" sub-command was used to show the more information for "work path" and "custom config"
}
+ cmdConvert := util.ToPointer(*cmdDoctorConvert)
+ cmdConvert.Hidden = true // still support the legacy "./gitea doctor" by the hidden sub-command, remove it in next release
+ subCmdWithConfig = append(subCmdWithConfig, cmdConvert)
+
// these sub-commands do not need the config file, and they do not depend on any path or environment variable.
subCmdStandalone := []*cli.Command{
CmdCert,
Migrates the database. This command can be used to run other commands before starting the server for the first time.
This command is idempotent.
-### convert
+### doctor check
-Converts an existing MySQL database from utf8 to utf8mb4.
+Diagnose and potentially fix problems with the current Gitea instance.
+Several checks are run by default, but additional ones can be run:
-### doctor
+- `gitea doctor check --list` - will list all the available checks
+- `gitea doctor check --all` - will run all available checks
+- `gitea doctor check --default` - will run the default checks
+- `gitea doctor check --run [check(s),]...` - will run the named checks
-Diagnose the problems of current Gitea instance according the given configuration.
-Currently there are a check list below:
-
-- Check if OpenSSH authorized_keys file id correct
- When your Gitea instance support OpenSSH, your Gitea instance binary path will be written to `authorized_keys`
- when there is any public key added or changed on your Gitea instance.
- 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.
- This check will help you to check if it works well.
-
-For contributors, if you want to add more checks, you can write a new function like `func(ctx *cli.Context) ([]string, error)` and
-append it to `doctor.go`.
-
-```go
-var checklist = []check{
- {
- title: "Check if OpenSSH authorized_keys file id correct",
- f: runDoctorLocationMoved,
- },
- // more checks please append here
-}
-```
-
-This function will receive a command line context and return a list of details about the problems or error.
+Some problems can be automatically fixed by passing the `--fix` option.
+Extra logging can be set with `--log-file=...`.
#### doctor recreate-table
It is highly recommended to back-up your database before running these commands.
+### doctor convert
+
+Converts a MySQL database from utf8 to utf8mb4 or a MSSQL database from varchar to nvarchar.
+
### manager
Manage running server operations:
迁移数据库。该命令可用于在首次启动服务器之前运行其他命令。此命令是幂等的。
-### convert
+### doctor check
-将现有的 MySQL 数据库从 utf8 转换为 utf8mb4。
+对 Gitea 实例进行诊断,可以修复一些可修复的问题。
+默认只运行部分检查,额外的检查可以参考:
-### doctor
+- `gitea doctor check --list` - 列出所有可用的检查
+- `gitea doctor check --all` - 运行所有可用的检查
+- `gitea doctor check --default` - 运行默认的检查
+- `gitea doctor check --run [check(s),]...` - 运行指定的名字的检查
-根据给定的配置诊断当前 Gitea 实例的问题。目前有以下检查清单:
-
-- 检查 OpenSSH 的 authorized_keys 文件是否正确
- 当您的 Gitea 实例支持 OpenSSH 时,当您的 Gitea 实例添加或更改任何公钥时,Gitea 实例的二进制路径将被写入 `authorized_keys` 文件。
- 有时,如果您在升级时移动或重命名了 Gitea 二进制文件,并且您没有在管理面板上运行“使用 Gitea 的 SSH 密钥更新「.ssh/authorized_keys」文件”操作。那么通过 SSH 的所有拉取/推送操作将无法正常工作。
- 此检查将帮助您检查它是否正常工作。
-
-对于贡献者,如果您想添加更多的检查项,您可以编写一个新的函数,如 `func(ctx *cli.Context) ([]string, error)`,并将其追加到 `doctor.go` 文件中。
-
-```go
-var checklist = []check{
- {
- title: "Check if OpenSSH authorized_keys file id correct",
- f: runDoctorLocationMoved,
- },
- // more checks please append here
-}
-```
-
-此函数将接收一个命令行上下文,并返回有关问题或错误的详细信息列表。
+有些问题可以通过设置 `--fix` 选项进行自动修复。
+额外的日志可以通过 `--log-file=...` 进行设置。
#### doctor recreate-table
强烈建议在运行这些命令之前备份您的数据库。
+### doctor convert
+
+将现有的 MySQL 数据库从 utf8 转换为 utf8mb4,或者把 MSSQL 数据库从 varchar 转换为 nvarchar。
+
### manager
管理运行中的服务器操作: