summaryrefslogtreecommitdiff
path: root/cmd/util
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-24 13:52:08 +0300
committerWolfy-J <[email protected]>2019-06-24 13:52:08 +0300
commit4182f60c833b644a785828e784386a95f690d5c8 (patch)
treeb53f7c12131cb7b1f53c0e4dd42f113daf674bc3 /cmd/util
parentdbd44f6e68f91daa56a983559ed1d4a9359f1d69 (diff)
added ability to include external configs
Diffstat (limited to 'cmd/util')
-rw-r--r--cmd/util/config.go40
1 files changed, 32 insertions, 8 deletions
diff --git a/cmd/util/config.go b/cmd/util/config.go
index d5b1020c..a1b3d25d 100644
--- a/cmd/util/config.go
+++ b/cmd/util/config.go
@@ -73,6 +73,32 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*co
}
}
+ // merge included configs
+ if include, ok := cfg.Get("include").([]interface{}); ok {
+
+ for _, file := range include {
+ filename, ok := file.(string)
+ if !ok {
+ continue
+ }
+
+ partial := viper.New()
+ partial.AutomaticEnv()
+ partial.SetEnvPrefix("rr")
+ partial.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
+ partial.SetConfigFile(filename)
+
+ if err := partial.ReadInConfig(); err != nil {
+ return nil, err
+ }
+
+ // merging
+ if err := cfg.MergeConfigMap(partial.AllSettings()); err != nil {
+ return nil, err
+ }
+ }
+ }
+
// automatically inject ENV variables using ${ENV} pattern
for _, key := range cfg.AllKeys() {
val := cfg.Get(key)
@@ -89,18 +115,16 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*co
cfg.Set(k, v)
}
+ }
- merged := viper.New()
-
- // we have to copy all the merged values into new config in order normalize it (viper bug?)
- if err := merged.MergeConfigMap(cfg.AllSettings()); err != nil {
- return nil, err
- }
+ merged := viper.New()
- return &configWrapper{merged}, nil
+ // we have to copy all the merged values into new config in order normalize it (viper bug?)
+ if err := merged.MergeConfigMap(cfg.AllSettings()); err != nil {
+ return nil, err
}
- return &configWrapper{cfg}, nil
+ return &configWrapper{merged}, nil
}
func parseFlag(flag string) (string, string, error) {