For a project I'm working on I'm trying to pass a map of stings as an environment variable using Viper. I tried several approaches to achieve this but with no success. When I read the env variable from code it is empty. This is the code I'm using:
// To configure viper
viper.SetEnvPrefix("CONFIG")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// To read the configuration value I tried all this variants:
fmt.Print(viper.GetString("options.values"))
fmt.Print(viper.GetStringMapString("options.values"))
fmt.Print(viper.GetStringMap("options.values"))
And this is how I'm passing the value:
CONFIG_OPTIONS_VALUES_ROOT="."
I've also tried:
CONFIG_OPTIONS_VALUES="{\"root\": \".\",\"cmd\": \"exec\", \"logging\": \"on\"}"
The way I want to process the value passes in the env variable is:
values := viper.GetStringMapString("options.values")
for key, val := range values {
fmt.Printf("Key: %s, Value: %s", key, val)
}
Which I can perfectly do if I write this configuration in a config file and I read it using viper:
options:
values:
root: .
cmd: exec
logging: on
#more values can be added here
Hope someone can point me in the right direction here.
fmt.Print(viper.GetString("options.values.root"))
– Westonwestover