【问题标题】:How to bind config items in an array to environment variables如何将数组中的配置项绑定到环境变量
【发布时间】:2019-10-02 04:07:39
【问题描述】:

下面是我的 toml 格式的配置文件。

[[hosts]]
name = "host1"
username = "user1"
password = "password1"

[[hosts]]
name = "host2"
username = "user2"
password = "password2"

...这是我加载它的代码:

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

type Config struct {
    Hosts []Host
}

type Host struct {
    Name      string `mapstructure:"name"`
    Username  string `mapstructure:"username"`
    Password  string `mapstructure:"password"`
}

func main() {
    viper.AddConfigPath(".")
    viper.AddConfigPath("./config")
    viper.SetConfigName("app")

    if err := viper.ReadInConfig(); err != nil {
        return nil, fmt.Errorf("error reading config file, %v", err)
    }

    config := new(Config)
    if err := viper.Unmarshal(config); err != nil {
        return nil, fmt.Errorf("error parsing config file, %v", err)
    }

    var username, password string

    for i, h := range config.Hosts {
        if len(h.Name) == 0 {
            return nil, fmt.Errorf("name not defined for host %d", i)
        }

        if username = os.Getenv(strings.ToUpper(h.Name) + "_" + "USERNAME"); len(username) > 0 {
            config.Hosts[i].Username = username
        } else if len(h.Username) == 0 {
            return nil, fmt.Errorf("username not defined for %s", e.Name)
        }

        if password = os.Getenv(strings.ToUpper(e.Name) + "_" + "PASSWORD"); len(password) > 0 {
            config.Hosts[i].Password = password
        } else if len(h.Password) == 0 {
            return nil, fmt.Errorf("password not defined for %s", e.Name)
        }

        fmt.Printf("Hostname: %s", h.name)
        fmt.Printf("Username: %s", h.Username)
        fmt.Printf("Password: %s", h.Password)
    }
}

例如,我首先检查环境变量HOST1_USERNAME1HOST1_PASSWORD1HOST2_USERNAME2HOST2_PASSWORD2是否存在...如果存在,那么我将配置项设置为它们的值,否则我尝试从配置文件中获取值。

我知道 viper 提供了方法 AutomaticEnv 来做类似的事情......但它是否适用于像我这样的集合(AutomaticEnv 应该在环境变量绑定之后调用)?

鉴于我上面的代码,是否可以使用viper提供的机制并删除os.GetEnv

谢谢。

更新

下面是我更新的代码。在我已经定义了环境变量HOST1_USERNAMEHOST1_PASSWORD 并将我的配置文件中的相应设置设置为一个空字符串。

这是我的新配置文件:

[host1]
username = ""
password = ""

[host2]
username = "user2"
password = "password2"

这是我的代码:

package config

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

type Config struct {
    Hosts []Host
}

type Host struct {
    Name     string
    Username string
    Password string
}

var config *Config

func (c *Config) Load() (*Config, error) {
    if config == nil {
        viper.AddConfigPath(".")
        viper.AddConfigPath("./config")
        viper.SetConfigName("myapp")
        viper.AutomaticEnv()
        viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

        if err := viper.ReadInConfig(); err != nil {
            return nil, fmt.Errorf("error reading config file, %v", err)
        }

        allSettings := viper.AllSettings()
        hosts := make([]Host, 0, len(allSettings))

        for key, value := range allSettings {
            val := value.(map[string]interface{})

            if val["username"] == nil {
                return nil, fmt.Errorf("username not defined for host %s", key)
            }

            if val["password"] == nil {
                return nil, fmt.Errorf("password not defined for host %s", key)
            }

            hosts = append(hosts, Host{
                Name:      key,
                Unsername: val["username"].(string),
                Password: val["password"].(string),
            })
        }

        config = &Config{hosts}
    }

    return config, nil
}

我现在工作(感谢 Chrono Kitsune),希望对您有所帮助, j3d

【问题讨论】:

    标签: go viper-go


    【解决方案1】:

    来自viper.Viper

    来源的优先级如下:

    1. 覆盖
    2. 标志
    3. 环境。变量
    4. 配置文件
    5. 键/值存储
    6. 默认值

    您在确定环境变量的名称时可能会遇到问题。您基本上需要一种将hosts[0].Username 绑定到环境变量HOST1_USERNAME 的方法。但是,目前在 viper 中没有办法做到这一点。事实上,viper.Get("hosts[0].username") 返回nil,这意味着数组索引显然不能与viper.BindEnv 一起使用。您还需要对尽可能多的主机使用此函数,这意味着如果您列出了 20 个主机,则需要调用 viper.BindEnv 40 或 60 次,具体取决于主机名称是否可以被覆盖通过环境变量。要解决此限制,您需要将主机作为独立表而不是表数组动态使用:

    [host1]
    username = "user1"
    password = "password1"
    
    [host2]
    username = "user2"
    password = "password2"
    

    然后您可以使用viper.SetEnvKeyReplacerstrings.Replacer 来处理环境变量问题:

    // host1.username => HOST1_USERNAME
    // host2.password => HOST2_PASSWORD
    // etc.
    viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
    

    请注意,在撰写本文时some bugs exist when it comes to the resolution order。此问题影响viper.Unmarshalviper.Get:环境变量应覆盖配置文件值,但目前仍使用配置文件值。奇怪的是,viper.AllSettings 工作正常。如果没有,您将无法执行以下操作来使用上述格式的主机:

    // Manually collect hosts for storing in config.
    func collectHosts() []Host {
        hosts := make([]Host, 0, 10)
        for key, value := range viper.AllSettings() {
            // viper.GetStringMapString(key)
            // won't work properly with environment vars, etc. until
            //   https://github.com/spf13/viper/issues/309
            // is resolved.
            v := value.(map[string]interface{})
            hosts = append(hosts, Host{
                Name:     key,
                Username: v["username"].(string),
                Password: v["password"].(string),
            })
        }
        return hosts
    }
    

    总结一下:

    1. 值应该取自第一个提供的:覆盖、标志、环境变量、配置文件、键/值存储、默认值。不幸的是,这并不总是遵循的顺序(因为存在错误)。
    2. 您需要更改配置格式并使用字符串替换器来利用viper.AutomaticEnv 的便利性。

    【讨论】:

    • 如果配置文件中没有定义主机1的用户名和密码,AllSettings是否返回环境变量HOST1_USERNAMEHOST1_PASSWORD的值?我问是因为它似乎不起作用 - 请参阅我原来问题中的更新。
    • @j3d viper.AutomaticEnv 仅适用于已定义的键。如果它们没有被定义,任何环境变量都不会被神奇地绑定到一个不存在的键上。可悲的是,viper.AllKeys()viper.AllSettings() 不会返回 host1 键,尽管您仍然可以使用 viper.Get("host1") 检索空/nil map[string]interface{} 而不是未键入的 nil,尽管它不会这样做你有什么好处(一个空的host238471 TOML 表作为唯一的主机呢?)如果你知道一个特定的设置,你可以为它设置一个默认值,环境变量就会起作用。否则,你就不走运了。
    • 好的,我刚刚在我的配置文件中将HOST1_USERNAMEHOST1_PASSWORD 设置为一个空字符串,现在它可以工作了。非常感谢 Kitsune 的支持 :-)
    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 2017-06-14
    • 2017-05-24
    • 2015-02-11
    • 1970-01-01
    • 2019-08-03
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多