Skip to content

Commit

Permalink
fix: config action output (#225)
Browse files Browse the repository at this point in the history
Show output for invalid config set and unset actions
  • Loading branch information
dbolson authored Apr 30, 2024
1 parent b74053c commit 889d8a2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func run() func(*cobra.Command, []string) error {
return errors.NewError("flag needs an argument: --set")
}

for i := 0; i < len(args)-1; i += 2 {
_, ok := cliflags.AllFlagsHelp()[args[i]]
if !ok {
return newErr(args[i])
}
}

rawConfig, v, err := getRawConfig()
if err != nil {
return err
Expand All @@ -122,6 +129,11 @@ func run() func(*cobra.Command, []string) error {

return writeConfig(configFile, v, setKeyFn)
case viper.IsSet(UnsetFlag):
_, ok := cliflags.AllFlagsHelp()[viper.GetString(UnsetFlag)]
if !ok {
return newErr(viper.GetString(UnsetFlag))
}

config, v, err := getConfig()
if err != nil {
return err
Expand All @@ -133,7 +145,12 @@ func run() func(*cobra.Command, []string) error {
}
}

return writeConfig(config, v, unsetKeyFn)
// TODO: show successful output

err = writeConfig(config, v, unsetKeyFn)
if err != nil {
return err
}
default:
return cmd.Help()
}
Expand Down Expand Up @@ -234,3 +251,16 @@ func writeConfig(

return nil
}

func newErr(flag string) error {
err := errors.NewError(
fmt.Sprintf(
`{
"message": "%s is not a valid configuration option"
}`,
flag,
),
)

return errors.NewError(output.CmdOutputError(flag, err))
}

0 comments on commit 889d8a2

Please sign in to comment.