Skip to content

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

Closed
@marcofranssen

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions