@@ -6,6 +6,7 @@ package cmd
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "io"
9
10
"os"
10
11
"strings"
11
12
@@ -15,61 +16,63 @@ import (
15
16
"github.com/urfave/cli/v3"
16
17
)
17
18
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 ( "-c" can apper in any position):
26
+ // * ./gitea -c /dev/null -h
27
+ // * ./gitea -c help /dev/null help
28
+ // * ./gitea help -c /dev/null
29
+ // * ./gitea help -c /dev/null web
30
+ // * ./gitea help web -c /dev/null
31
+ // * ./gitea web help -c /dev/null
32
+ // * ./gitea web -h -c /dev/null
33
+ func cliHelpPrinterNew (out io.Writer , templ string , data interface {}) {
34
+ cmd , _ := data .(* cli.Command )
35
+ if cmd != nil {
36
+ prepareWorkPathAndCustomConf (cmd )
37
+ }
38
+ cliHelpPrinterOld (out , templ , data )
39
+ if setting .CustomConf != "" {
40
+ _ , _ = fmt .Fprintf (out , `
34
41
DEFAULT CONFIGURATION:
35
42
AppPath: %s
36
43
WorkPath: %s
37
44
CustomPath: %s
38
45
ConfigFile: %s
39
46
40
47
` , setting .AppPath , setting .AppWorkPath , setting .CustomPath , setting .CustomConf )
41
- }
42
- return err
43
- },
44
48
}
45
- return c
46
49
}
47
50
48
- func prepareSubcommandWithGlobalFlags (command * cli.Command ) {
49
- command .Before = prepareWorkPathAndCustomConf (command .Before )
51
+ func prepareSubcommandWithGlobalFlags (originCmd * cli.Command ) {
52
+ originBefore := originCmd .Before
53
+ originCmd .Before = func (ctx context.Context , cmd * cli.Command ) (context.Context , error ) {
54
+ prepareWorkPathAndCustomConf (cmd )
55
+ if originBefore != nil {
56
+ return originBefore (ctx , cmd )
57
+ }
58
+ return ctx , nil
59
+ }
50
60
}
51
61
52
- // 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
- }
62
+ // prepareWorkPathAndCustomConf tries to prepare the work path, custom path and custom config from various inputs:
63
+ // command line flags, environment variables, config file
64
+ func prepareWorkPathAndCustomConf ( cmd * cli. Command ) {
65
+ var args setting. ArgWorkPathAndCustomConf
66
+ if cmd . IsSet ( "work-path" ) {
67
+ args . WorkPath = cmd . String ( "work-path" )
58
68
}
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 )
69
+ if cmd .IsSet ("custom-path" ) {
70
+ args .CustomPath = cmd .String ("custom-path" )
71
+ }
72
+ if cmd .IsSet ("config" ) {
73
+ args .CustomConf = cmd .String ("config" )
72
74
}
75
+ setting .InitWorkPathAndCommonConfig (os .Getenv , args )
73
76
}
74
77
75
78
type AppVersion struct {
@@ -105,9 +108,8 @@ func NewMainApp(appVer AppVersion) *cli.Command {
105
108
Usage : "Set custom path (defaults to '{WorkPath}/custom')" ,
106
109
},
107
110
}
108
- // these sub-commands need to use config file
111
+ // these sub-commands need to use a config file
109
112
subCmdWithConfig := []* cli.Command {
110
- cmdHelp (), // the "help" sub-command was used to show the more information for "work path" and "custom config"
111
113
CmdWeb ,
112
114
CmdServ ,
113
115
CmdHook ,
0 commit comments