I wrote a short example to illustrate the problem.
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Config struct {
Foo struct {
Bar string
}
}
func main() {
config := &Config{}
cmdRoot := &cobra.Command{
Run: func(*cobra.Command, []string) {
viper.Unmarshal(config)
},
}
flags := cmdRoot.PersistentFlags()
flags.String("foo.bar", "hello-world", "test default value fo foo.bar")
viper.BindPFlags(flags)
cmdRoot.Execute()
fmt.Printf("foo.bar: [%v]\n", config.Foo.Bar)
}
The above code I expected to get the output is:
This has worked very well in the past. But since PR #673, it can't work properly, it will output:
I checked the contents of the PR #673 , I can't fully understand the changes. But I believe the changes blocked my previous usage.
How should I modify my code to fit the new version of viper?