-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a 'flags' subcommand to list global flags
- Loading branch information
1 parent
e49e3bf
commit fcad638
Showing
16 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/wal-g/wal-g/cmd/common/st" | ||
"github.com/wal-g/wal-g/internal" | ||
) | ||
|
||
const usageTemplate = `Usage:{{if .Runnable}} | ||
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} | ||
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} | ||
Aliases: | ||
{{.NameAndAliases}}{{end}}{{if .HasExample}} | ||
Examples: | ||
{{.Example}}{{end}}{{if .HasAvailableSubCommands}} | ||
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} | ||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} | ||
Flags: | ||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} | ||
Global Flags: | ||
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}` + | ||
// additional custom message : cli flags introduced by 'internal.AddConfigFlags()' are hidden by default | ||
` | ||
To get the complete list of all global flags, run: 'wal-g flags'` + | ||
`{{if .HasHelpSubCommands}} | ||
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} | ||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}} | ||
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} | ||
` | ||
|
||
func Init(cmd *cobra.Command, dbName string) { | ||
internal.ConfigureSettings(dbName) | ||
cobra.OnInitialize(internal.InitConfig, internal.Configure) | ||
|
||
cmd.SetUsageTemplate(usageTemplate) | ||
cmd.InitDefaultVersionFlag() | ||
internal.AddConfigFlags(cmd) | ||
|
||
cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)") | ||
|
||
// Init help subcommand | ||
cmd.InitDefaultHelpCmd() | ||
helpCmd, _, _ := cmd.Find([]string{"help"}) | ||
// fix to disable the required settings check for the help subcommand | ||
helpCmd.PersistentPreRun = func(*cobra.Command, []string) {} | ||
|
||
// Add flags subcommand | ||
cmd.AddCommand(FlagsCmd) | ||
|
||
// Add storage tools | ||
cmd.AddCommand(st.StorageToolsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// FlagsCmd represents the flags command | ||
var FlagsCmd = &cobra.Command{ | ||
Use: "flags", | ||
Short: "Display the list of available global flags for all wal-g commands", | ||
DisableFlagsInUseLine: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_ = cmd.Usage() | ||
}, | ||
} | ||
|
||
func init() { | ||
defaultUsageFn := (&cobra.Command{}).UsageFunc() | ||
defaultHelpFn := (&cobra.Command{}).HelpFunc() | ||
|
||
FlagsCmd.SetUsageTemplate(flagsUsageTemplate) | ||
FlagsCmd.SetHelpTemplate(flagsHelpTemplate) | ||
|
||
FlagsCmd.SetUsageFunc(func(cmd *cobra.Command) error { | ||
cmd.Parent().PersistentFlags().VisitAll(func(f *pflag.Flag) { f.Hidden = false }) | ||
|
||
return defaultUsageFn(cmd) | ||
}) | ||
FlagsCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { | ||
cmd.Parent().PersistentFlags().VisitAll(func(f *pflag.Flag) { f.Hidden = false }) | ||
|
||
defaultHelpFn(cmd, args) | ||
}) | ||
|
||
// fix to disable the required settings check for the help subcommand | ||
FlagsCmd.PersistentPreRun = func(*cobra.Command, []string) {} | ||
} | ||
|
||
const flagsHelpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}{{end}} | ||
Usage: | ||
{{.UseLine}} | ||
Flags: | ||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}} | ||
{{.Usage}}` | ||
const flagsUsageTemplate = `Global Flags: | ||
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}} | ||
` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters