diff options
author | Valery Piashchynski <[email protected]> | 2020-12-21 14:24:45 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-21 14:24:45 +0300 |
commit | 8543980775e5f8b12e5e200a0764052cdb4350a5 (patch) | |
tree | c1c6dff8e6bd81bcf51d608c5ed935702911ae81 /plugins/config/plugin.go | |
parent | fd6e9cc403fc0c3857dcf29768429a374bd85636 (diff) | |
parent | 7b32b6b93576ec72b4b7fdf2068e655f869e9cf8 (diff) |
Merge pull request #453 from spiral/plugin/redis
Plugin/redis
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 -} |