duanjiati1755 2016-02-08 20:05
浏览 317

使用Viper在Go中将字符串映射作为环境变量传递

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.

  • 写回答

1条回答 默认 最新

  • dongquelu1239 2016-02-08 22:46
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能