summaryrefslogtreecommitdiff
path: root/plugins/config/plugin.go
blob: ddb7fe8896ee00ae9227aea39debf9536ab84b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package config

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/spf13/viper"
	"github.com/spiral/errors"
)

type Viper struct {
	viper     *viper.Viper
	Path      string
	Prefix    string
	Type      string
	ReadInCfg []byte
	// user defined Flags in the form of <option>.<key> = <value>
	// which overwrites initial config key
	Flags []string
}

// Inits config provider.
func (v *Viper) Init() error {
	const op = errors.Op("config_plugin_init")
	v.viper = viper.New()
	// If user provided []byte data with config, read it and ignore Path and Prefix
	if v.ReadInCfg != nil && v.Type != "" {
		v.viper.SetConfigType("yaml")
		return v.viper.ReadConfig(bytes.NewBuffer(v.ReadInCfg))
	}

	// read in environment variables that match
	v.viper.AutomaticEnv()
	if v.Prefix == "" {
		return errors.E(op, errors.Str("prefix should be set"))
	}

	v.viper.SetEnvPrefix(v.Prefix)
	if v.Path == "" {
		return errors.E(op, errors.Str("path should be set"))
	}

	v.viper.SetConfigFile(v.Path)
	v.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

	err := v.viper.ReadInConfig()
	if err != nil {
		return errors.E(op, err)
	}

	// override config Flags
	if len(v.Flags) > 0 {
		for _, f := range v.Flags {
			key, val, err := parseFlag(f)
			if err != nil {
				return errors.E(op, err)
			}

			v.viper.Set(key, val)
		}
	}

	return v.viper.ReadInConfig()
}

// Overwrite overwrites existing config with provided values
func (v *Viper) Overwrite(values map[string]interface{}) error {
	if len(values) != 0 {
		for key, value := range values {
			v.viper.Set(key, value)
		}
	}

	return nil
}

// UnmarshalKey reads configuration section into configuration object.
func (v *Viper) UnmarshalKey(name string, out interface{}) error {
	const op = errors.Op("config_plugin_unmarshal_key")
	err := v.viper.UnmarshalKey(name, &out)
	if err != nil {
		return errors.E(op, err)
	}
	return nil
}

func (v *Viper) Unmarshal(out interface{}) error {
	const op = errors.Op("config_plugin_unmarshal")
	err := v.viper.Unmarshal(&out)
	if err != nil {
		return errors.E(op, err)
	}
	return nil
}

// Get raw config in a form of config section.
func (v *Viper) Get(name string) interface{} {
	return v.viper.Get(name)
}

// Has checks if config section exists.
func (v *Viper) Has(name string) bool {
	return v.viper.IsSet(name)
}

func parseFlag(flag string) (string, string, error) {
	const op = errors.Op("parse_flag")
	if !strings.Contains(flag, "=") {
		return "", "", errors.E(op, errors.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
}