Closed
Description
I have following cobra command
func init() {
rootCmd.AddCommand(serveCmd)
serveCmd.Flags().String("listenAddr", ":5000", "server listen address")
viper.BindPFlag("server.listenaddr", serveCmd.Flags().Lookup("listenAddr"))
}
Now when I run the command I try to get the config value.
listenAddr := viper.Sub("server").GetString("listenaddr")
When running this command I see that the config value is not updated based on Env variable or commandline option.
$ ./my-app serve
listenAddr = :5000
$ ./my-app serve --listenAddr :6000
listenAddr = :5000
$ SERVER_LISTENADDR=:6000 ./my-app serve
listenAddr = :5000
However when changing the code to read the value as following it does work as expected.
listenAddr := viper.GetString("server.listenaddr")
$ ./my-app serve
listenAddr = :5000
$ ./my-app serve --listenAddr :6000
listenAddr = :6000
$ SERVER_LISTENADDR=:8888./my-app serve
listenAddr = :8888
It would be excepted that the Sub viper will also have the correct values for the config. So I can pass the subviper to the package that depends on those configs.
Metadata
Assignees
Labels
No labels
Activity