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

BUG: Sub Viper is not reflecting config value updates via cli-option or ENV #745

Closed
marcofranssen opened this issue Aug 8, 2019 · 0 comments · Fixed by #1056
Closed
Milestone

Comments

@marcofranssen
Copy link

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.

@sagikazarmark sagikazarmark added this to the v1.16.0 milestone Feb 25, 2023
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 a pull request may close this issue.

2 participants