Passing a string map as an environment variable in Go using Viper
Asked Answered
I

1

6

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.

Inseminate answered 8/2, 2016 at 20:5 Comment(3)
It seems that your environment variable name is not the same as the key name that you are using to retrieve its value, try with that: fmt.Print(viper.GetString("options.values.root"))Westonwestover
That works, but I that is not the effect I'm looking for. In that case I need to know the keys of the values map in advance. I want to process all the values as a map. I will add to the question to make the use case more clear.Inseminate
I added an answer, maybe it could help you.Westonwestover
W
3

I have been investigating a bit and it seems that you are not set the value of your environment variable properly, as well as how you are calling it with viper. Find below an example of that and feel free to comment any thought that you have:

package main

import (
    "bytes"
    "fmt"
    "github.com/spf13/viper"
    "strings"
)

func main() {
    //Configure the type of the configuration as JSON
    viper.SetConfigType("json")
    //Set the environment prefix as CONFIG
    viper.SetEnvPrefix("CONFIG")
    viper.AutomaticEnv()
    //Substitute the _ to .
    replacer := strings.NewReplacer(".", "_")
    viper.SetEnvKeyReplacer(replacer)

    //Get the string that is set in the CONFIG_OPTIONS_VALUES environment variable
    var jsonExample = []byte(viper.GetString("options.values"))
    viper.ReadConfig(bytes.NewBuffer(jsonExample))

    //Convert the sub-json string of the options field in map[string]string
    fmt.Println(viper.GetStringMapString("options"))
}

And how it would be call:

CONFIG_OPTIONS_VALUES="{\"options\": {\"root\": \".\", \"cmd\": \"exec\", \"logging\": \"on\"}}" go run main.go
Westonwestover answered 8/2, 2016 at 22:46 Comment(3)
Thanks! that works, but the only caveat with that is that I'm already using a yaml configuration file, and the env variable will be an alternative way to pass the values, so I cannot switch to a JSON config type in viper.Inseminate
You can add a condition that checks if the environment variable is set, if so change the config type to JSON, do whatever you want, and once it is finished change again the config type to YML.Westonwestover
Great, I will give it a try and let you know, thanks!Inseminate

© 2022 - 2024 — McMap. All rights reserved.