Refactor to use urfave/cli/v2 (#25959)

Replace #10912

And there are many new tests to cover the CLI behavior

There were some concerns about the "option order in hook scripts"
(https://github.com/go-gitea/gitea/pull/10912#issuecomment-1137543314),
it's not a problem now. Because the hook script uses `/gitea hook
--config=/app.ini pre-receive` format. The "config" is a global option,
it can appear anywhere.

----

## ⚠️ BREAKING ⚠️

This PR does it best to avoid breaking anything. The major changes are:

* `gitea` itself won't accept web's options: `--install-port` / `--pid`
/ `--port` / `--quiet` / `--verbose` .... They are `web` sub-command's
options.
    * Use `./gitea web --pid ....` instead
* `./gitea` can still run the `web` sub-command as shorthand, with
default options
* The sub-command's options must follow the sub-command
* Before: `./gitea --sub-opt subcmd` might equal to `./gitea subcmd
--sub-opt` (well, might not ...)
    * After: only `./gitea subcmd --sub-opt` could be used
    * The global options like `--config` are not affected
This commit is contained in:
wxiaoguang 2023-07-21 17:28:19 +08:00 committed by GitHub
parent 840830b655
commit d0dbe52e76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 886 additions and 643 deletions

View file

@ -32,7 +32,7 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/kballard/go-shellquote"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)
const (
@ -40,17 +40,17 @@ const (
)
// CmdServ represents the available serv sub-command.
var CmdServ = cli.Command{
var CmdServ = &cli.Command{
Name: "serv",
Usage: "This command should only be called by SSH shell",
Description: "Serv provides access auth for repositories",
Before: PrepareConsoleLoggerLevel(log.FATAL),
Action: runServ,
Flags: []cli.Flag{
cli.BoolFlag{
&cli.BoolFlag{
Name: "enable-pprof",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "debug",
},
},
@ -119,7 +119,7 @@ func fail(ctx context.Context, userMessage, logMsgFmt string, args ...any) error
}
_ = private.SSHLog(ctx, true, logMsg)
}
return cli.NewExitError("", 1)
return cli.Exit("", 1)
}
// handleCliResponseExtra handles the extra response from the cli sub-commands
@ -130,7 +130,7 @@ func handleCliResponseExtra(extra private.ResponseExtra) error {
_, _ = fmt.Fprintln(os.Stdout, extra.UserMsg)
}
if extra.HasError() {
return cli.NewExitError(extra.Error, 1)
return cli.Exit(extra.Error, 1)
}
return nil
}
@ -147,20 +147,20 @@ func runServ(c *cli.Context) error {
return nil
}
if len(c.Args()) < 1 {
if c.NArg() < 1 {
if err := cli.ShowSubcommandHelp(c); err != nil {
fmt.Printf("error showing subcommand help: %v\n", err)
}
return nil
}
keys := strings.Split(c.Args()[0], "-")
keys := strings.Split(c.Args().First(), "-")
if len(keys) != 2 || keys[0] != "key" {
return fail(ctx, "Key ID format error", "Invalid key argument: %s", c.Args()[0])
return fail(ctx, "Key ID format error", "Invalid key argument: %s", c.Args().First())
}
keyID, err := strconv.ParseInt(keys[1], 10, 64)
if err != nil {
return fail(ctx, "Key ID parsing error", "Invalid key argument: %s", c.Args()[1])
return fail(ctx, "Key ID parsing error", "Invalid key argument: %s", c.Args().Get(1))
}
cmd := os.Getenv("SSH_ORIGINAL_COMMAND")