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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package util
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"github.com/spiral/roadrunner/service"
"os"
"path/filepath"
"strings"
)
// ConfigWrapper provides interface bridge between v configs and service.Config.
type ConfigWrapper struct {
v *viper.Viper
}
// Get nested config section (sub-map), returns nil if section not found.
func (w *ConfigWrapper) Get(key string) service.Config {
sub := w.v.Sub(key)
if sub == nil {
return nil
}
return &ConfigWrapper{sub}
}
// Unmarshal unmarshal config data into given struct.
func (w *ConfigWrapper) Unmarshal(out interface{}) error {
return w.v.Unmarshal(out)
}
// LoadConfig config and merge it's values with set of flags.
func LoadConfig(cfgFile string, path []string, name string, flags []string, jsonConfig string) (*ConfigWrapper, error) {
cfg := viper.New()
if cfgFile != "" {
if absPath, err := filepath.Abs(cfgFile); err == nil {
cfgFile = absPath
// force working absPath related to config file
if err := os.Chdir(filepath.Dir(absPath)); err != nil {
return nil, err
}
}
// Use cfg file from the flag.
cfg.SetConfigFile(cfgFile)
if dir, err := filepath.Abs(cfgFile); err == nil {
// force working absPath related to config file
if err := os.Chdir(filepath.Dir(dir)); err != nil {
return nil, err
}
}
} else {
// automatic location
for _, p := range path {
cfg.AddConfigPath(p)
}
cfg.SetConfigName(name)
}
// read in environment variables that match
cfg.AutomaticEnv()
cfg.SetEnvPrefix("rr")
cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// If a cfg file is found, read it in.
if err := cfg.ReadInConfig(); err != nil {
if len(flags) == 0 && jsonConfig == "" {
return nil, err
}
}
// merge included configs
if include, ok := cfg.Get("include").([]interface{}); ok {
for _, file := range include {
filename, ok := file.(string)
if !ok {
continue
}
partial := viper.New()
partial.AutomaticEnv()
partial.SetEnvPrefix("rr")
partial.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
partial.SetConfigFile(filename)
if err := partial.ReadInConfig(); err != nil {
return nil, err
}
// merging
if err := cfg.MergeConfigMap(partial.AllSettings()); err != nil {
return nil, err
}
}
}
// automatically inject ENV variables using ${ENV} pattern
for _, key := range cfg.AllKeys() {
val := cfg.Get(key)
cfg.Set(key, parseEnv(val))
}
// merge with console flags
if len(flags) != 0 {
for _, f := range flags {
k, v, err := parseFlag(f)
if err != nil {
return nil, err
}
cfg.Set(k, v)
}
}
if jsonConfig != "" {
jConfig := viper.New()
jConfig.AutomaticEnv()
jConfig.SetEnvPrefix("rr")
jConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
jConfig.SetConfigType("json")
if err := jConfig.ReadConfig(bytes.NewBufferString(jsonConfig)); err != nil {
return nil, err
}
// merging
if err := cfg.MergeConfigMap(jConfig.AllSettings()); err != nil {
return nil, err
}
}
merged := viper.New()
// we have to copy all the merged values into new config in order normalize it (viper bug?)
if err := merged.MergeConfigMap(cfg.AllSettings()); err != nil {
return nil, err
}
return &ConfigWrapper{merged}, nil
}
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.Replace(value, fmt.Sprintf("\\%s", string(escape)), string(escape), -1)
}
return value
}
func parseEnv(value interface{}) interface{} {
str, ok := value.(string)
if !ok || len(str) <= 3 {
return value
}
if str[0:2] == "${" && str[len(str)-1:] == "}" {
if v, ok := os.LookupEnv(str[2 : len(str)-1]); ok {
return v
}
}
return str
}
|