Skip to content

Commit

Permalink
handle global in flag-prefix for nested structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Jun 20, 2023
1 parent 21c27d1 commit b15ac8d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 6 additions & 5 deletions x/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newCLI(pn string, o *options) *CLI {
v := viper.New()
v.AutomaticEnv()

_ = bindFlags(rc, rc, reflect.ValueOf(o.cfgObj).Elem(), v, "", "", rc.Name())
_ = bindFlags(rc, rc, reflect.ValueOf(o.cfgObj).Elem(), v, "", "", rc.Name(), false)

rc.AddCommand(&cobra.Command{
Use: "generate-config",
Expand Down Expand Up @@ -98,7 +98,7 @@ func addSubCommandsMap(rc *cobra.Command, v *viper.Viper, o *options, cfg interf
}
rc.AddCommand(newCmd)

if err := bindFlags(rc, newCmd, reflect.ValueOf(o.cfgObj).Elem(), v, "", "", sc.Name); err != nil {
if err := bindFlags(rc, newCmd, reflect.ValueOf(o.cfgObj).Elem(), v, "", "", sc.Name, false); err != nil {
panic(err)
}

Expand All @@ -113,6 +113,7 @@ func bindFlags(
val reflect.Value,
v *viper.Viper,
flagPrefix, envPrefix, cmdName string,
global bool,
) error {
typ := val.Type()

Expand All @@ -123,7 +124,7 @@ func bindFlags(
env := envPrefix + field.Tag.Get("env")
def := field.Tag.Get("default")
usage := field.Tag.Get("flag-usage")
global := len(flagDetails) > 1 && flagDetails[1] == "global"
localGlobal := global || (len(flagDetails) > 1 && flagDetails[1] == "global")
subCommands := strings.Split(field.Tag.Get("sub-commands"), ",")
bindToSubCommand := len(subCommands) > 1 || (len(subCommands) == 1 && subCommands[0] != "")

Expand All @@ -134,7 +135,7 @@ func bindFlags(
targetCmd := cmd
flagFunc := targetCmd.Flags()

if global {
if localGlobal {
targetCmd = rootCmd
flagFunc = targetCmd.PersistentFlags()
}
Expand All @@ -155,7 +156,7 @@ func bindFlags(
nestedEnvPrefix = envPrefix + nestedEnvPrefix
}

if err := bindFlags(rootCmd, targetCmd, val.Field(i), v, nestedFlagPrefix, nestedEnvPrefix, cmdName); err != nil {
if err := bindFlags(rootCmd, targetCmd, val.Field(i), v, nestedFlagPrefix, nestedEnvPrefix, cmdName, localGlobal); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion x/cli/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
type Config struct {
Debug bool `flag:"debug,global" env:"DEBUG" default:"false" flag-usage:"enable debug mode"`
Log struct {
Level string `flag:"level" env:"LEVEL" default:"info"`
Enabled bool `flag:"enabled" env:"ENABLED" default:"true" flag-usage:"enable logging"`
Level string `flag:"level" env:"LEVEL" default:"info"`
} `flag-prefix:"log,global" env-prefix:"LOG"`
Host string `flag:"host" env:"HOST" default:"localhost" sub-commands:"server"`
Port int `flag:"port" env:"PORT" default:"8080" sub-commands:"server"`
Expand Down

0 comments on commit b15ac8d

Please sign in to comment.