Skip to content
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

utils/cobrautil: support pasring lists from env vars and config file #381

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

Choraden
Copy link
Contributor

@Choraden Choraden commented Sep 1, 2023

This trims and reformats string values set from viper. It allows reading yaml and json format lists e.g.

dns-server:
 - 1.2.3.4
 - 4.3.2.1
{
  "dns-server": ["8.8.8.8", "5.5.5.5"]
}

and env vars:

FORWARDER_DNS_SERVER="1.2.3.4 4.3.2.1" forwarder run

It does not affect reading and parsing flags from command line.

This trims and reformats string values set from viper.
It allows reading yaml and json format lists e.g.
```
dns-server:
 - 1.2.3.4
 - 4.3.2.1
```
```
{
  "dns-server": ["8.8.8.8", "5.5.5.5"]
}
```
and env vars:
```
FORWARDER_DNS_SERVER="1.2.3.4 4.3.2.1" forwarder run
```

It does not affect reading and parsing flags from command line.
@@ -55,7 +55,11 @@ func BindAll(cmd *cobra.Command, envPrefix, configFileFlagName string) error {
ok = true
fs.VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
if err := fs.Set(f.Name, fmt.Sprintf("%v", v.Get(f.Name))); err != nil {
s := fmt.Sprintf("%v", v.Get(f.Name))
s = strings.TrimPrefix(s, "[")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because we don't have access to the anyflag repo right now? I think it could wait a few days (unless it's problematic to fix in anyflag)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After investigation, it turns out that anyflag follows parsing rules of cobra, so we probably don't want to mess with it.
More advanced parsing is usually implemented in a higher layer and later passed to cobra like I've done here.

@Choraden Choraden merged commit 138fae7 into main Sep 1, 2023
@Choraden Choraden deleted the hg/bind-hack branch September 1, 2023 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants