Diego February 2016

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

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.

Answers


JesusRT February 2016

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

Post Status

Asked in February 2016
Viewed 1,408 times
Voted 6
Answered 1 times

Search




Leave an answer