Skip to content

Commit

Permalink
feat: config feedback (#292)
Browse files Browse the repository at this point in the history
Show success or error with config values
  • Loading branch information
dbolson authored May 15, 2024
1 parent ef0ed13 commit 0268ab6
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,39 @@ func run() func(*cobra.Command, []string) error {

rawConfig, v, err := getRawConfig()
if err != nil {
return err
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

// add arg pairs to config
// where each argument is --set arg1 val1 --set arg2 val2
// add arg pairs to config where each argument is --set arg1 val1 --set arg2 val2
newFields := make([]string, 0)
for i, a := range args {
if i%2 == 0 {
rawConfig[a] = struct{}{}
newFields = append(newFields, a)
} else {
rawConfig[args[i-1]] = a
}
}

configFile, err := config.NewConfig(rawConfig)
if err != nil {
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

setKeyFn := func(key string, value interface{}, v *viper.Viper) {
v.Set(key, value)
}
err = writeConfig(configFile, v, setKeyFn)
if err != nil {
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

configFile, err := config.NewConfig(rawConfig)
output, err := outputSetAction(newFields)
if err != nil {
return errors.NewError(err.Error())
return err
}

return writeConfig(configFile, v, setKeyFn)
fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
case viper.IsSet(UnsetFlag):
_, ok := cliflags.AllFlagsHelp()[viper.GetString(UnsetFlag)]
if !ok {
Expand All @@ -163,21 +173,25 @@ func run() func(*cobra.Command, []string) error {

config, v, err := getConfig()
if err != nil {
return err
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

unsetKeyFn := func(key string, value interface{}, v *viper.Viper) {
if key != viper.GetString("unset") {
v.Set(key, value)
}
}

// TODO: show successful output

err = writeConfig(config, v, unsetKeyFn)
if err != nil {
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

output, err := outputUnsetAction(viper.GetString(UnsetFlag))
if err != nil {
return err
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
default:
return cmd.Help()
}
Expand Down Expand Up @@ -292,7 +306,7 @@ func newErr(flag string) error {
),
)

return errors.NewError(output.CmdOutputError(flag, err))
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

func writeAlphabetizedFlags(sb *strings.Builder) {
Expand All @@ -305,3 +319,33 @@ func writeAlphabetizedFlags(sb *strings.Builder) {
sb.WriteString(fmt.Sprintf("- `%s`: %s\n", flag, cliflags.AllFlagsHelp()[flag]))
}
}

func outputSetAction(newFields []string) (string, error) {
fields := struct {
Items []string `json:"items"`
}{
Items: newFields,
}
fieldsJSON, _ := json.Marshal(fields)
output, err := output.CmdOutput("update", viper.GetString(cliflags.OutputFlag), fieldsJSON)
if err != nil {
return "", errors.NewError(err.Error())
}

return output, nil
}

func outputUnsetAction(newField string) (string, error) {
field := struct {
Key string `json:"key"`
}{
Key: newField,
}
fieldJSON, _ := json.Marshal(field)
output, err := output.CmdOutput("delete", viper.GetString(cliflags.OutputFlag), fieldJSON)
if err != nil {
return "", errors.NewError(err.Error())
}

return output, nil
}

0 comments on commit 0268ab6

Please sign in to comment.