Rename the main module to gotea.dev and the binary to gotea, and update the application name and English locale. Keep the external gitea.dev/sdk and gitea.dev/actionslib module imports unchanged. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
194 lines
5.6 KiB
Go
194 lines
5.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"gotea.dev/modules/log"
|
|
"gotea.dev/modules/setting"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
var cliHelpPrinterOld = cli.HelpPrinter
|
|
|
|
func init() {
|
|
cli.HelpPrinter = cliHelpPrinterNew
|
|
}
|
|
|
|
// cliHelpPrinterNew helps to print "DEFAULT CONFIGURATION" for the following cases ( "-c" can apper in any position):
|
|
// * ./gotea -c /dev/null -h
|
|
// * ./gotea -c help /dev/null help
|
|
// * ./gotea help -c /dev/null
|
|
// * ./gotea help -c /dev/null web
|
|
// * ./gotea help web -c /dev/null
|
|
// * ./gotea web help -c /dev/null
|
|
// * ./gotea web -h -c /dev/null
|
|
func cliHelpPrinterNew(out io.Writer, templ string, data any) {
|
|
cmd, _ := data.(*cli.Command)
|
|
if cmd != nil {
|
|
prepareWorkPathAndCustomConf(cmd)
|
|
}
|
|
cliHelpPrinterOld(out, templ, data)
|
|
if setting.CustomConf != "" {
|
|
_, _ = fmt.Fprintf(out, `
|
|
DEFAULT CONFIGURATION:
|
|
AppPath: %s
|
|
WorkPath: %s
|
|
CustomPath: %s
|
|
ConfigFile: %s
|
|
|
|
`, setting.AppPath, setting.AppWorkPath, setting.CustomPath, setting.CustomConf)
|
|
}
|
|
}
|
|
|
|
func PrepareSubcommandWithGlobalFlags(originCmd *cli.Command) {
|
|
originBefore := originCmd.Before
|
|
originCmd.Before = func(ctxOrig context.Context, cmd *cli.Command) (ctx context.Context, err error) {
|
|
ctx = ctxOrig
|
|
if originBefore != nil {
|
|
ctx, err = originBefore(ctx, cmd)
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
}
|
|
prepareWorkPathAndCustomConf(cmd)
|
|
return ctx, nil
|
|
}
|
|
}
|
|
|
|
// prepareWorkPathAndCustomConf tries to prepare the work path, custom path and custom config from various inputs:
|
|
// command line flags, environment variables, config file
|
|
func prepareWorkPathAndCustomConf(cmd *cli.Command) {
|
|
var args setting.ArgWorkPathAndCustomConf
|
|
if cmd.IsSet("work-path") {
|
|
args.WorkPath = cmd.String("work-path")
|
|
}
|
|
if cmd.IsSet("custom-path") {
|
|
args.CustomPath = cmd.String("custom-path")
|
|
}
|
|
if cmd.IsSet("config") {
|
|
args.CustomConf = cmd.String("config")
|
|
}
|
|
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
|
|
}
|
|
|
|
type AppVersion struct {
|
|
Version string
|
|
Extra string
|
|
}
|
|
|
|
func NewMainApp(appVer AppVersion) *cli.Command {
|
|
app := &cli.Command{}
|
|
app.Name = "gotea" // must be lower-cased because it appears in the "USAGE" section like "gotea doctor [command [command options]]"
|
|
app.Usage = "A painless self-hosted Git service"
|
|
app.Description = `Gitea program contains "web" and other subcommands. If no subcommand is given, it starts the web server by default. Use "web" subcommand for more web server arguments, use other subcommands for other purposes.`
|
|
app.Version = appVer.Version + appVer.Extra
|
|
app.EnableShellCompletion = true
|
|
app.Flags = []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "work-path",
|
|
Aliases: []string{"w"},
|
|
TakesFile: true,
|
|
Usage: "Set Gitea's working path (defaults to the Gitea's binary directory)",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "config",
|
|
Aliases: []string{"c"},
|
|
TakesFile: true,
|
|
Value: setting.CustomConf,
|
|
Usage: "Set custom config file (defaults to '{WorkPath}/custom/conf/app.ini')",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "custom-path",
|
|
Aliases: []string{"C"},
|
|
TakesFile: true,
|
|
Usage: "Set custom path (defaults to '{WorkPath}/custom')",
|
|
},
|
|
}
|
|
webCmd := newWebCommand()
|
|
// these sub-commands need to use a config file
|
|
subCmdWithConfig := []*cli.Command{
|
|
webCmd,
|
|
newServCommand(),
|
|
newHookCommand(),
|
|
NewKeysCommand(),
|
|
newDumpCommand(),
|
|
newAdminCommand(),
|
|
newMigrateCommand(),
|
|
newDoctorCommand(),
|
|
newManagerCommand(),
|
|
newEmbeddedCommand(),
|
|
newMigrateStorageCommand(),
|
|
newDumpRepositoryCommand(),
|
|
newRestoreRepositoryCommand(),
|
|
newActionsCommand(),
|
|
}
|
|
|
|
// these sub-commands do not need the config file, and they do not depend on any path or environment variable.
|
|
subCmdStandalone := []*cli.Command{
|
|
cmdConfig(),
|
|
cmdCert(),
|
|
newGenerateCommand(),
|
|
newDocsCommand(),
|
|
}
|
|
|
|
// TODO: we should eventually drop the default command,
|
|
// but not sure whether it would break Windows users who used to double-click the EXE to run.
|
|
app.DefaultCommand = webCmd.Name
|
|
|
|
app.Before = PrepareConsoleLoggerLevel(log.INFO)
|
|
for i := range subCmdWithConfig {
|
|
PrepareSubcommandWithGlobalFlags(subCmdWithConfig[i])
|
|
}
|
|
app.Commands = append(app.Commands, subCmdWithConfig...)
|
|
app.Commands = append(app.Commands, subCmdStandalone...)
|
|
|
|
setting.UnsetUnnecessaryEnvVars()
|
|
return app
|
|
}
|
|
|
|
// usageErr marks a usage error already reported by cliOnUsageError, so RunMainApp does not print it again.
|
|
type usageErr struct{ err error }
|
|
|
|
func (e usageErr) Error() string { return e.err.Error() }
|
|
func (e usageErr) Unwrap() error { return e.err }
|
|
|
|
// cliOnUsageError reports usage errors itself instead of letting urfave/cli dump the full help to stdout (since urfave/cli v3.10).
|
|
func cliOnUsageError(_ context.Context, cmd *cli.Command, err error, _ bool) error {
|
|
_, _ = fmt.Fprintf(cmd.Root().ErrWriter, "Incorrect Usage: %s\n", err.Error())
|
|
return usageErr{err}
|
|
}
|
|
|
|
func setCLIOnUsageError(cmd *cli.Command) {
|
|
_ = cmd.Walk(func(c *cli.Command) error {
|
|
c.OnUsageError = cliOnUsageError
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func RunMainApp(app *cli.Command, args ...string) error {
|
|
ctx, cancel := installSignals()
|
|
defer cancel()
|
|
setCLIOnUsageError(app)
|
|
// the completion subcommands are built during app.Run, after the Walk above, so cover them via this hook
|
|
app.ConfigureShellCompletionCommand = setCLIOnUsageError
|
|
err := app.Run(ctx, args)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if _, ok := errors.AsType[usageErr](err); ok {
|
|
cli.OsExiter(1) // cliOnUsageError already reported it
|
|
return err
|
|
}
|
|
_, _ = fmt.Fprintf(app.ErrWriter, "Command error: %v\n", err)
|
|
cli.OsExiter(1)
|
|
return err
|
|
}
|