diff options
Diffstat (limited to 'plugins/config/plugin.go')
-rwxr-xr-x | plugins/config/plugin.go | 41 |
1 files changed, 10 insertions, 31 deletions
diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go index 2555d28a..4cde314d 100755 --- a/plugins/config/plugin.go +++ b/plugins/config/plugin.go @@ -1,17 +1,18 @@ package config import ( + "bytes" "errors" - "fmt" "strings" "github.com/spf13/viper" ) type Viper struct { - viper *viper.Viper - Path string - Prefix string + viper *viper.Viper + Path string + Prefix string + ReadInCfg []byte } // Inits config provider. @@ -32,17 +33,16 @@ func (v *Viper) Init() error { v.viper.SetConfigFile(v.Path) v.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + if v.ReadInCfg != nil { + return v.viper.ReadConfig(bytes.NewBuffer(v.ReadInCfg)) + } return v.viper.ReadInConfig() } // Overwrite overwrites existing config with provided values -func (v *Viper) Overwrite(values map[string]string) error { +func (v *Viper) Overwrite(values map[string]interface{}) error { if len(values) != 0 { - for _, flag := range values { - key, value, err := parseFlag(flag) - if err != nil { - return err - } + for key, value := range values { v.viper.Set(key, value) } } @@ -68,24 +68,3 @@ func (v *Viper) Get(name string) interface{} { func (v *Viper) Has(name string) bool { return v.viper.IsSet(name) } - -func parseFlag(flag string) (string, string, error) { - if !strings.Contains(flag, "=") { - return "", "", fmt.Errorf("invalid flag `%s`", flag) - } - - parts := strings.SplitN(strings.TrimLeft(flag, " \"'`"), "=", 2) - - return strings.Trim(parts[0], " \n\t"), parseValue(strings.Trim(parts[1], " \n\t")), nil -} - -func parseValue(value string) string { - escape := []rune(value)[0] - - if escape == '"' || escape == '\'' || escape == '`' { - value = strings.Trim(value, string(escape)) - value = strings.ReplaceAll(value, fmt.Sprintf("\\%s", string(escape)), string(escape)) - } - - return value -} |