From e7a62f19155bbba9ac17527e2abb30d31c632655 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 21 Dec 2020 13:14:42 +0300 Subject: Finish redis plugin --- plugins/config/plugin.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'plugins/config/plugin.go') diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go index 2555d28a..b438a185 100755 --- a/plugins/config/plugin.go +++ b/plugins/config/plugin.go @@ -1,6 +1,7 @@ package config import ( + "bytes" "errors" "fmt" "strings" @@ -9,9 +10,10 @@ import ( ) type Viper struct { - viper *viper.Viper - Path string - Prefix string + viper *viper.Viper + Path string + Prefix string + ReadInCfg []byte } // Inits config provider. @@ -32,17 +34,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) } } @@ -69,16 +70,6 @@ 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] -- cgit v1.2.3 From 7b32b6b93576ec72b4b7fdf2068e655f869e9cf8 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 21 Dec 2020 13:15:24 +0300 Subject: Removed unused function from config --- plugins/config/plugin.go | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'plugins/config/plugin.go') diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go index b438a185..4cde314d 100755 --- a/plugins/config/plugin.go +++ b/plugins/config/plugin.go @@ -3,7 +3,6 @@ package config import ( "bytes" "errors" - "fmt" "strings" "github.com/spf13/viper" @@ -69,14 +68,3 @@ func (v *Viper) Get(name string) interface{} { func (v *Viper) Has(name string) bool { return v.viper.IsSet(name) } - -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 -} -- cgit v1.2.3