-
Notifications
You must be signed in to change notification settings - Fork 13
feat: config feedback #292
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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() | ||
| } | ||
|
|
@@ -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) { | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know we are deleting it from the config but |
||
| if err != nil { | ||
| return "", errors.NewError(err.Error()) | ||
| } | ||
|
|
||
| return output, nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions create JSON to pass into the output function since it expects an API response.