@@ -6,6 +6,7 @@ package cmd
66import (
77 "context"
88 "fmt"
9+ "io"
910 "os"
1011 "strings"
1112
@@ -15,61 +16,63 @@ 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 ( "-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 , `
3441DEFAULT CONFIGURATION:
3542 AppPath: %s
3643 WorkPath: %s
3744 CustomPath: %s
3845 ConfigFile: %s
3946
4047` , setting .AppPath , setting .AppWorkPath , setting .CustomPath , setting .CustomConf )
41- }
42- return err
43- },
4448 }
45- return c
4649}
4750
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+ }
5060}
5161
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" )
5868 }
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" )
7274 }
75+ setting .InitWorkPathAndCommonConfig (os .Getenv , args )
7376}
7477
7578type AppVersion struct {
@@ -105,9 +108,8 @@ func NewMainApp(appVer AppVersion) *cli.Command {
105108 Usage : "Set custom path (defaults to '{WorkPath}/custom')" ,
106109 },
107110 }
108- // these sub-commands need to use config file
111+ // these sub-commands need to use a config file
109112 subCmdWithConfig := []* cli.Command {
110- cmdHelp (), // the "help" sub-command was used to show the more information for "work path" and "custom config"
111113 CmdWeb ,
112114 CmdServ ,
113115 CmdHook ,
0 commit comments