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

Connecting a StringToString parameter with viper does not seem work. #778

Closed
FredrikFolkesson opened this issue Oct 29, 2018 · 5 comments
Closed

Comments

@FredrikFolkesson
Copy link

FredrikFolkesson commented Oct 29, 2018

Hi.

I am trying to connect a map[string]string parameter to Viper and it does not seem to work.

In my init function I do

cmd.PersistentFlags().StringToString("headers", nil, "Headers to use when connecting..")
viper.BindPFlag("headers", cmd.PersistentFlags().Lookup("headers"))

and then in the cmd function I do

headersMap := viper.GetStringMapString("headers")
//use the headers

The problem seems to be that what is "saved" in Cobra for the value headers is something of the type StringToString and not map[string]string as I would have thought.
(that is the value of the type I get if I do cmd.Flag("headers").Value.Type()

If I save my parameter value by directly specifying the map like this:

cmd.PersistentFlags().StringToStringVar(&headersMap, "headers", nil, "Headers to use when connecting ")

It correctly saves the value in the map. But then I would have to check that the map is not empty and if it is save the value from viper in the map.

I don't know If this is a bug or If I am missing something?

Thanks!

@FredrikFolkesson
Copy link
Author

No one?

@micahhausler
Copy link

micahhausler commented Nov 27, 2018

func ToStringMapStringE() doesn't account for pflag's stringToStringValue type.

@github-actions
Copy link

github-actions bot commented Apr 8, 2020

This issue is being marked as stale due to a long period of inactivity

@den-is
Copy link

den-is commented May 18, 2020

UPD. wiped my initial text. TLDR: I kinda got the same problem, but actually I was passing arguments the wrong way.
Plus that issue has been just fixed here spf13/viper#608, just 1 week ago!

@johnSchnake
Copy link
Collaborator

So the underlying issue was fixed? Going to go ahead and close but feel free to reopen if I misinterpreted.

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

No branches or pull requests

4 participants