Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config flags shell completions #1252

Merged
merged 3 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 42 additions & 8 deletions cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/cmd/common/st"
"github.com/wal-g/wal-g/internal"
Expand Down Expand Up @@ -39,21 +40,18 @@ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`

const hiddenConfigFlagAnnotation = "walg_annotation_hidden_config_flag"

func Init(cmd *cobra.Command, dbName string) {
internal.ConfigureSettings(dbName)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

cmd.SetUsageTemplate(usageTemplate)
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)
internal.AddConfigFlags(cmd, hiddenConfigFlagAnnotation)

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) {}
initHelp(cmd)

// Add flags subcommand
cmd.AddCommand(FlagsCmd)
Expand Down Expand Up @@ -91,9 +89,45 @@ func Init(cmd *cobra.Command, dbName string) {
// Don't run PersistentPreRun when shell autocompleting
preRun := cmd.PersistentPreRun
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if strings.Index(cmd.Use, "__complete") == 0 {
if strings.Index(cmd.Use, cobra.ShellCompRequestCmd) == 0 {
return
}
preRun(cmd, args)
}
}

// setup init and usage functionality
func initHelp(cmd *cobra.Command) {
cmd.SetUsageTemplate(usageTemplate)
defaultUsageFn := (&cobra.Command{}).UsageFunc()
defaultHelpFn := (&cobra.Command{}).HelpFunc()

// hide global config flags from usage output
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
hideGlobalConfigFlags(cmd)
return defaultUsageFn(cmd)
})

// hide global config flags from help output
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
hideGlobalConfigFlags(cmd)
defaultHelpFn(cmd, args)
})

// 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) {}
}

// hide global config flags from all subcommands except the "flags" subcommand
func hideGlobalConfigFlags(cmd *cobra.Command) {
if cmd != FlagsCmd {
cmd.Root().PersistentFlags().VisitAll(func(f *pflag.Flag) {
if _, ok := f.Annotations[hiddenConfigFlagAnnotation]; ok {
f.Hidden = true
}
})
}
}
15 changes: 0 additions & 15 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// FlagsCmd represents the flags command
Expand All @@ -16,23 +15,9 @@ var FlagsCmd = &cobra.Command{
}

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) {}
}
Expand Down
10 changes: 7 additions & 3 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func ConfigureAndRunDefaultWebServer() error {
return nil
}

func AddConfigFlags(Cmd *cobra.Command) {
func AddConfigFlags(Cmd *cobra.Command, hiddenCfgFlagAnnotation string) {
cfgFlags := &pflag.FlagSet{}
for k := range AllowedSettings {
flagName := toFlagName(k)
Expand All @@ -569,8 +569,12 @@ func AddConfigFlags(Cmd *cobra.Command) {
cfgFlags.String(flagName, "", flagUsage)
_ = viper.BindPFlag(k, cfgFlags.Lookup(flagName))
}
cfgFlags.VisitAll(func(f *pflag.Flag) { f.Hidden = true })

cfgFlags.VisitAll(func(f *pflag.Flag) {
if f.Annotations == nil {
f.Annotations = map[string][]string{}
}
f.Annotations[hiddenCfgFlagAnnotation] = []string{"true"}
})
Cmd.PersistentFlags().AddFlagSet(cfgFlags)
}

Expand Down