Skip to content

Commit

Permalink
Merge pull request #178 from kongfei605/config_collapse
Browse files Browse the repository at this point in the history
fix config collapse when instances were written in more than one file…
  • Loading branch information
kongfei605 authored Sep 7, 2022
2 parents 2497bff + 3d4b23d commit d4c8d7e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
9 changes: 7 additions & 2 deletions inputs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ type Provider interface {

func NewProvider(c *config.ConfigType, reloadFunc func()) (Provider, error) {
log.Println("I! use input provider:", c.Global.Providers)

// 不添加provider配置 则默认使用local
// 兼容老版本
if len(c.Global.Providers) == 0 {
c.Global.Providers = append(c.Global.Providers, "local")
}
providers := make([]Provider, 0, len(c.Global.Providers))
for _, p := range c.Global.Providers {
name := strings.ToLower(p)
Expand All @@ -80,6 +84,7 @@ func NewProvider(c *config.ConfigType, reloadFunc func()) (Provider, error) {
providers = append(providers, provider)
}
}

return &ProviderManager{
providers: providers,
}, nil
Expand Down Expand Up @@ -224,7 +229,7 @@ func (lp *LocalProvider) GetInputConfig(inputKey string) ([]cfg.ConfigWithFormat
return nil, fmt.Errorf("failed to list files under: %s : %v", lp.configDir, err)
}

cwf := make([]cfg.ConfigWithFormat, 0, 1)
cwf := make([]cfg.ConfigWithFormat, 0, len(files))
for _, f := range files {
c, err := file.ReadBytes(path.Join(lp.configDir, inputFilePrefix+inputKey, f))
if err != nil {
Expand Down
53 changes: 42 additions & 11 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cfg

import (
"bytes"
"fmt"
"io/ioutil"
"path"
"strings"

Expand Down Expand Up @@ -33,6 +35,10 @@ func GuessFormat(fpath string) ConfigFormat {
}

func LoadConfigByDir(configDir string, configPtr interface{}) error {
var (
tBuf, yBuf, jBuf []byte
)

loaders := []multiconfig.Loader{
&multiconfig.TagLoader{},
&multiconfig.EnvironmentLoader{},
Expand All @@ -44,41 +50,66 @@ func LoadConfigByDir(configDir string, configPtr interface{}) error {
}

for _, fpath := range files {
if strings.HasSuffix(fpath, ".toml") {
loaders = append(loaders, &multiconfig.TOMLLoader{Path: path.Join(configDir, fpath)})
}
if strings.HasSuffix(fpath, ".json") {
loaders = append(loaders, &multiconfig.JSONLoader{Path: path.Join(configDir, fpath)})
buf, err := ioutil.ReadFile(path.Join(configDir, fpath))
if err != nil {
return err
}
if strings.HasSuffix(fpath, ".yaml") || strings.HasSuffix(fpath, ".yml") {
loaders = append(loaders, &multiconfig.YAMLLoader{Path: path.Join(configDir, fpath)})
switch {
case strings.HasSuffix(fpath, "toml"):
tBuf = append(tBuf, buf...)
case strings.HasSuffix(fpath, "json"):
jBuf = append(jBuf, buf...)
case strings.HasSuffix(fpath, "yaml") || strings.HasSuffix(fpath, "yml"):
yBuf = append(yBuf, buf...)
}
}

if len(tBuf) != 0 {
loaders = append(loaders, &multiconfig.TOMLLoader{Reader: bytes.NewReader(tBuf)})
}
if len(yBuf) != 0 {
loaders = append(loaders, &multiconfig.YAMLLoader{Reader: bytes.NewReader(yBuf)})
}
if len(jBuf) != 0 {
loaders = append(loaders, &multiconfig.JSONLoader{Reader: bytes.NewReader(jBuf)})
}

m := multiconfig.DefaultLoader{
Loader: multiconfig.MultiLoader(loaders...),
Validator: multiconfig.MultiValidator(&multiconfig.RequiredValidator{}),
}

return m.Load(configPtr)
}

func LoadConfigs(configs []ConfigWithFormat, configPtr interface{}) error {
var (
tBuf, yBuf, jBuf []byte
)
loaders := []multiconfig.Loader{
&multiconfig.TagLoader{},
&multiconfig.EnvironmentLoader{},
}
for _, c := range configs {
switch c.Format {
case TomlFormat:
loaders = append(loaders, &multiconfig.TOMLLoader{Reader: strings.NewReader(c.Config)})
tBuf = append(tBuf, []byte(c.Config)...)
case YamlFormat:
loaders = append(loaders, &multiconfig.YAMLLoader{Reader: strings.NewReader(c.Config)})
yBuf = append(yBuf, []byte(c.Config)...)
case JsonFormat:
loaders = append(loaders, &multiconfig.JSONLoader{Reader: strings.NewReader(c.Config)})
jBuf = append(jBuf, []byte(c.Config)...)
}
}

if len(tBuf) != 0 {
loaders = append(loaders, &multiconfig.TOMLLoader{Reader: bytes.NewReader(tBuf)})
}
if len(yBuf) != 0 {
loaders = append(loaders, &multiconfig.YAMLLoader{Reader: bytes.NewReader(yBuf)})
}
if len(jBuf) != 0 {
loaders = append(loaders, &multiconfig.JSONLoader{Reader: bytes.NewReader(jBuf)})
}

m := multiconfig.DefaultLoader{
Loader: multiconfig.MultiLoader(loaders...),
Validator: multiconfig.MultiValidator(&multiconfig.RequiredValidator{}),
Expand Down

0 comments on commit d4c8d7e

Please sign in to comment.