diff options
author | Wolfy-J <[email protected]> | 2019-05-02 22:34:08 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-05-02 22:34:08 +0300 |
commit | 788fd35e2e186eff1c7a7ac71cdbb3ac557ddfd6 (patch) | |
tree | cbe9e697fcacb658bf1f53fdbba3b1268ae5bd51 /cmd | |
parent | 658254359597aa85fa4679abd821dd06855b61e9 (diff) |
embedding env variables to config
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/util/config.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/cmd/util/config.go b/cmd/util/config.go index a354b132..d5b1020c 100644 --- a/cmd/util/config.go +++ b/cmd/util/config.go @@ -63,6 +63,8 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*co // read in environment variables that match cfg.AutomaticEnv() + cfg.SetEnvPrefix("rr") + cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) // If a cfg file is found, read it in. if err := cfg.ReadInConfig(); err != nil { @@ -71,6 +73,13 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*co } } + // automatically inject ENV variables using ${ENV} pattern + for _, key := range cfg.AllKeys() { + val := cfg.Get(key) + cfg.Set(key, parseEnv(val)) + } + + // merge with console flags if len(flags) != 0 { for _, f := range flags { k, v, err := parseFlag(f) @@ -114,3 +123,18 @@ func parseValue(value string) string { return value } + +func parseEnv(value interface{}) interface{} { + str, ok := value.(string) + if !ok || len(str) <= 3 { + return value + } + + if str[0:2] == "${" && str[len(str)-1:] == "}" { + if v, ok := os.LookupEnv(str[2 : len(str)-1]); ok { + return v + } + } + + return str +} |