-
Notifications
You must be signed in to change notification settings - Fork 2k
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
StringToString pflag values are not handled correctly by viper #608
Comments
This issue seems related to what was seen in #200 |
It looks like viper is fixed, can someone update viper? |
Created a PR, sensu/sensu-go#2867 |
I made a demo of this issue: https://github.com/kd7lxl/viper-demo |
it seems that in v1.6.2 this issue is fixed |
I tried it @danmx and still doesn't work. Using |
@terev here you have how the input works: @amir20 did you add this to your code: serverCmd.Flags().Int("filter", map[string]string{}, "desc filter")
if err := viper.BindPFlag("filter", serverCmd.Flags().Lookup("filter")); err != nil {
return err
} |
The issue still persists in v1.6.2. This: f := pflag.NewFlagSet("", pflag.ContinueOnError)
f.StringToString("filter", map[string]string{}, "desc filter")
_ = viper.BindPFlags(f)
...
filters := viper.GetStringMapString("filter")
fmt.Println(filters) prints While using just filters := viper.GetString("filter") prints |
@amir20 that was a poor copy-paste of code snippets from my tool. I wrote a simple cli just to test it and you're right also now I know that I have a bug in my tool 🤦♂ here's the code if someone what's to quickly reproduce it: package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfg *viper.Viper
rootCmd = &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cfg.BindPFlag("test1", cmd.Flags().Lookup("test1")); err != nil {
return err
}
if err := cfg.BindPFlag("test2", cmd.Flags().Lookup("test2")); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
a := cfg.GetStringMapString("test1")
fmt.Printf("GetStringMapString - T: %T, v: %v, s: %s\n", a, a, a)
b := cfg.GetStringMap("test1")
fmt.Printf("GetStringMap - T: %T, v: %v, s: %s\n", b, b, b)
c := cfg.Get("test1")
fmt.Printf("Get - T: %T, v: %v, s: %s\n", c, c, c)
arg2 := cfg.GetString("test2")
fmt.Printf("GetString - T: %T, v: %v, s: %s\n", arg2, arg2, arg2)
return nil
},
}
)
func init() {
cfg = viper.GetViper()
rootCmd.Flags().StringToString("test1", map[string]string{"default_key": "default_value"}, "test description")
rootCmd.Flags().String("test2", "default_string", "test description")
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1) //nolint:gomnd
}
} $ go build -o main
$ ./main --test1 a=b,c=d
GetStringMapString - T: map[string]string, v: map[], s: map[]
GetStringMap - T: map[string]interface {}, v: map[], s: map[]
Get - T: string, v: [c=d,a=b], s: [c=d,a=b]
GetString - T: string, v: default_string, s: default_string |
$ ./main --test1 a=b,c=d
GetStringMapString - T: map[string]string, v: map[a:b c:d], s: map[a:b c:d]
GetStringMap - T: map[string]interface {}, v: map[], s: map[]
Get - T: map[string]string, v: map[a:b c:d], s: map[a:b c:d]
GetString - T: string, v: default_string, s: default_string @terev it seems to work only for |
@danmx to me that output looks like Get also worked |
@terev you're right 😂 I only glimpsed over |
@amir20 @danmx pushed some changes #874. I was able to get GetStringMap working,
|
This still doesn't work. :( @sagikazarmark I tried testing with After reading and using debugger, I see that the code eventually gets to https://github.com/spf13/cast/blob/master/cast.go#L108-L110.
I can create a new bug or reopen this. |
@amir20 This issue (and the fix) is for pflag values, not env vars. So I think it should definitely be a new feature request. |
Interesting. That's confusing. This whole time I thought Regardless, not a big deal. I'll create a new bug. |
I bind a StringToString pflag like so
Then retrieve the value from my viper with
The string map returned is an empty map, which seems to be the default value returned on error by spf13/cast.
I expected to get a populated string map back.
The text was updated successfully, but these errors were encountered: