Skip to content

Commit 061ab90

Browse files
committed
improve
1 parent bf7265c commit 061ab90

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

cmd/main.go

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmd
66
import (
77
"context"
88
"fmt"
9+
"io"
910
"os"
1011
"strings"
1112

@@ -15,61 +16,60 @@ import (
1516
"github.com/urfave/cli/v3"
1617
)
1718

18-
// cmdHelp is our own help subcommand with more information
19-
// Keep in mind that the "./gitea help"(subcommand) is different from "./gitea --help"(flag), the flag doesn't parse the config or output "DEFAULT CONFIGURATION:" information
20-
func cmdHelp() *cli.Command {
21-
c := &cli.Command{
22-
Name: "help",
23-
Aliases: []string{"h"},
24-
Usage: "Shows a list of commands or help for one command",
25-
ArgsUsage: "[command]",
26-
Action: func(ctx context.Context, c *cli.Command) (err error) {
27-
if !c.Args().Present() {
28-
err = cli.ShowAppHelp(c.Root())
29-
} else {
30-
err = cli.ShowCommandHelp(ctx, c.Root(), c.Args().First())
31-
}
32-
if err == nil {
33-
_, _ = fmt.Fprintf(c.Root().Writer, `
19+
var cliHelpPrinterOld = cli.HelpPrinter
20+
21+
func init() {
22+
cli.HelpPrinter = cliHelpPrinterNew
23+
}
24+
25+
// cliHelpPrinterNew helps to print "DEFAULT CONFIGURATION" for the following cases:
26+
// * ./gitea -c /dev/null -h
27+
// * ./gitea help -c /dev/null
28+
// * ./gitea help web -c /dev/null
29+
// * ./gitea web help -c /dev/null
30+
// * ./gitea web -h -c /dev/null
31+
func cliHelpPrinterNew(out io.Writer, templ string, data interface{}) {
32+
cmd, _ := data.(*cli.Command)
33+
if cmd != nil {
34+
prepareWorkPathAndCustomConf(cmd)
35+
}
36+
cliHelpPrinterOld(out, templ, data)
37+
if setting.CustomConf != "" {
38+
_, _ = fmt.Fprintf(out, `
3439
DEFAULT CONFIGURATION:
3540
AppPath: %s
3641
WorkPath: %s
3742
CustomPath: %s
3843
ConfigFile: %s
3944
4045
`, setting.AppPath, setting.AppWorkPath, setting.CustomPath, setting.CustomConf)
41-
}
42-
return err
43-
},
4446
}
45-
return c
4647
}
4748

48-
func prepareSubcommandWithGlobalFlags(command *cli.Command) {
49-
command.Before = prepareWorkPathAndCustomConf(command.Before)
49+
func prepareSubcommandWithGlobalFlags(originCmd *cli.Command) {
50+
originBefore := originCmd.Before
51+
originCmd.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
52+
prepareWorkPathAndCustomConf(cmd)
53+
if originBefore != nil {
54+
return originBefore(ctx, cmd)
55+
}
56+
return ctx, nil
57+
}
5058
}
5159

5260
// prepareWorkPathAndCustomConf wraps the Action to prepare the work path and custom config
53-
func prepareWorkPathAndCustomConf(original cli.BeforeFunc) cli.BeforeFunc {
54-
if original == nil {
55-
original = func(ctx context.Context, c *cli.Command) (context.Context, error) {
56-
return ctx, nil
57-
}
61+
func prepareWorkPathAndCustomConf(cmd *cli.Command) {
62+
var args setting.ArgWorkPathAndCustomConf
63+
if cmd.IsSet("work-path") {
64+
args.WorkPath = cmd.String("work-path")
5865
}
59-
return func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
60-
var args setting.ArgWorkPathAndCustomConf
61-
if cmd.IsSet("work-path") {
62-
args.WorkPath = cmd.String("work-path")
63-
}
64-
if cmd.IsSet("custom-path") {
65-
args.CustomPath = cmd.String("custom-path")
66-
}
67-
if cmd.IsSet("config") {
68-
args.CustomConf = cmd.String("config")
69-
}
70-
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
71-
return original(ctx, cmd)
66+
if cmd.IsSet("custom-path") {
67+
args.CustomPath = cmd.String("custom-path")
68+
}
69+
if cmd.IsSet("config") {
70+
args.CustomConf = cmd.String("config")
7271
}
72+
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
7373
}
7474

7575
type AppVersion struct {
@@ -105,9 +105,8 @@ func NewMainApp(appVer AppVersion) *cli.Command {
105105
Usage: "Set custom path (defaults to '{WorkPath}/custom')",
106106
},
107107
}
108-
// these sub-commands need to use config file
108+
// these sub-commands need to use a config file
109109
subCmdWithConfig := []*cli.Command{
110-
cmdHelp(), // the "help" sub-command was used to show the more information for "work path" and "custom config"
111110
CmdWeb,
112111
CmdServ,
113112
CmdHook,

0 commit comments

Comments
 (0)