Skip to content

Commit

Permalink
Config flags shell completions (#1252)
Browse files Browse the repository at this point in the history
* All flags are unhidden for autocompletions to work

Default usage/help funcs are overridden to hide persistent
config flags from usage/help messages

* string constant is removed

* Minor polishing

Co-authored-by: usernamedt <usernamedt@yandex-team.com>
  • Loading branch information
hexlify and usernamedt committed May 8, 2022
1 parent efe802d commit dc0618f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
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 @@ -94,9 +92,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 @@ -562,7 +562,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 @@ -575,8 +575,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

0 comments on commit dc0618f

Please sign in to comment.