summaryrefslogtreecommitdiff
path: root/plugins/config
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-12-21 13:14:42 +0300
committerValery Piashchynski <[email protected]>2020-12-21 13:14:42 +0300
commite7a62f19155bbba9ac17527e2abb30d31c632655 (patch)
treef060dbd61f0dc0d1093937eced738bd6aead1373 /plugins/config
parentb403dd170bf3dc3ce451ba4ada40dd55773b032a (diff)
Finish redis plugin
Diffstat (limited to 'plugins/config')
-rwxr-xr-xplugins/config/plugin.go29
1 files changed, 10 insertions, 19 deletions
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]