diff options
Diffstat (limited to 'cmd/util/config.go')
-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 +} |