diff options
-rw-r--r-- | cmd/util/config.go | 40 |
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) { |