-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add config set flag #177
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
dbolson
merged 2 commits into
sc-240202/add-config-list-flag
from
sc-240202/add-config-set-flag
Apr 16, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package config | |
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
|
|
@@ -54,9 +55,44 @@ func run() func(*cobra.Command, []string) error { | |
|
|
||
| fmt.Fprint(cmd.OutOrStdout(), string(configJSON)+"\n") | ||
| case viper.GetBool(SetFlag): | ||
| fmt.Fprintln(cmd.OutOrStdout(), "called --set flag") | ||
| case viper.GetBool(UnsetFlag): | ||
| fmt.Fprintln(cmd.OutOrStdout(), "called --unset flag") | ||
| // flag needs two arguments: a key and value | ||
| if len(args)%2 != 0 { | ||
| return errors.New("flag needs an argument: --set") | ||
| } | ||
|
|
||
| rawConfig, v, err := getRawConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // add arg pairs to config | ||
| // where each argument is --set arg1 val1 --set arg2 val2 | ||
| for i, a := range args { | ||
| if i%2 == 0 { | ||
| rawConfig[a] = struct{}{} | ||
| } else { | ||
| rawConfig[args[i-1]] = a | ||
| } | ||
| } | ||
|
|
||
| setKeyFn := func(key string, value interface{}, v *viper.Viper) { | ||
|
Contributor
Author
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. Using a filter function here will be more obvious in the next PR that supports |
||
| v.Set(key, value) | ||
| } | ||
|
|
||
| return writeConfig(config.NewConfig(rawConfig), v, setKeyFn) | ||
| case viper.IsSet(UnsetFlag): | ||
| config, v, err := getConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| unsetKeyFn := func(key string, value interface{}, v *viper.Viper) { | ||
| if key != viper.GetString("unset") { | ||
| v.Set(key, value) | ||
| } | ||
| } | ||
|
|
||
| return writeConfig(config, v, unsetKeyFn) | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -84,6 +120,27 @@ func getConfig() (config.ConfigFile, *viper.Viper, error) { | |
| return c, v, nil | ||
| } | ||
|
|
||
| // getRawConfig builds a map of the values in the config file. | ||
| func getRawConfig() (map[string]interface{}, *viper.Viper, error) { | ||
| v, err := getViperWithConfigFile() | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| data, err := os.ReadFile(v.ConfigFileUsed()) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| config := make(map[string]interface{}, 0) | ||
| err = yaml.Unmarshal([]byte(data), &config) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| return config, v, nil | ||
| } | ||
|
|
||
| // getViperWithConfigFile ensures the viper instance has a config file written to the filesystem. | ||
| // We want to write the file when someone runs a config command. | ||
| func getViperWithConfigFile() (*viper.Viper, error) { | ||
|
|
@@ -105,3 +162,36 @@ func getViperWithConfigFile() (*viper.Viper, error) { | |
|
|
||
| return v, nil | ||
| } | ||
|
|
||
| // writeConfig writes the values in config to the config file based on the filter function. | ||
| func writeConfig( | ||
| conf config.ConfigFile, | ||
| v *viper.Viper, | ||
| filterFn func(key string, value interface{}, v *viper.Viper), | ||
| ) error { | ||
| // create a new viper instance since the existing one has the command name and flags already set, | ||
| // and these will get written to the config file. | ||
| newViper := viper.New() | ||
| newViper.SetConfigFile(v.ConfigFileUsed()) | ||
|
|
||
| configYAML, err := yaml.Marshal(conf) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| rawConfig := make(map[string]interface{}) | ||
| err = yaml.Unmarshal(configYAML, &rawConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for key, value := range rawConfig { | ||
| filterFn(key, value, newViper) | ||
| } | ||
|
|
||
| err = newViper.WriteConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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
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.
This is the same error that cobra shows for a flag that requires a single value.