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 | |
parent | 658254359597aa85fa4679abd821dd06855b61e9 (diff) |
embedding env variables to config
-rw-r--r-- | cmd/util/config.go | 24 | ||||
-rw-r--r-- | service/http/service.go | 1 |
2 files changed, 25 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 +} diff --git a/service/http/service.go b/service/http/service.go index ad59f887..6e4542bf 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -53,6 +53,7 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) { func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) { s.cfg = cfg s.env = e + if r != nil { if err := r.Register(ID, &rpcServer{s}); err != nil { return false, err |