-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
200 lines (144 loc) · 4.88 KB
/
reader.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package config
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"dario.cat/mergo"
"gopkg.in/yaml.v3"
)
var ErrNoDefaults = errors.New("no default config")
var (
ErrMergingError = errors.New("merging with defaults error")
)
// Read Reads yaml files from configuration directory with sub folders
// as application stage and merges config files in one configuration per stage.
func Read(ctx context.Context, stage Stageable, opts ...Option) ([]byte, error) {
mc, err := newMerger(append(opts, withStageName(stage))...)
if err != nil {
return nil, err
}
mc.logger.Debug(ctx, "Current stage", mc.stage.Name().String())
mc.logger.Debug(ctx, "Config path", mc.cfgPath)
if err = mc.getFileList(); err != nil {
mc.logger.Error(ctx, "get file list error", err)
return nil, err
}
// check defaults config existence. Fall down if not
if !mc.defaultConfigExists() {
mc.logger.Error(ctx, "defaults config is not found in file list! Fall down.", mc.fileList)
return nil, ErrNoDefaults
}
mc.logger.Debug(ctx, "Existing config list", mc.fileList)
fileListResult := make(map[StageName][]string)
for stageName, files := range mc.fileList {
for _, file := range files {
var configBytes []byte
configBytes, err = os.ReadFile(file)
if err != nil {
mc.logger.Error(ctx, "Config read fail! Fall down.", stageName, file)
return nil, fmt.Errorf("config file `%s` read fail: %w", file, err)
}
var configFromFile map[StageName]map[string]any
mc.logger.Debug(ctx, "file content", file, string(configBytes))
if err = yaml.Unmarshal(configBytes, &configFromFile); err != nil {
mc.logger.Error(ctx, "config read fail! Fall down.", stageName, file)
return nil, fmt.Errorf("config file `%s` unmarshal fail: %w", file, err)
}
if _, ok := configFromFile[stageName]; !ok {
mc.logger.Warn(ctx, "File excluded from current stage (it is not for this stage)!", file, stageName)
continue
}
cc, ok := mc.getConfigForStage(stageName)
if !ok {
mc.setConfigForStage(stageName, configFromFile[stageName])
cc, _ = mc.getConfigForStage(stageName)
}
if err = mergo.Merge(
&cc,
configFromFile[stageName],
); err != nil {
mc.logger.Error(ctx, "config merge fail! Fall down.", stageName, file)
return nil, fmt.Errorf("merging configs[%s] with configFromFile[%s] config fail: %w", stageName, stageName, err)
}
mc.setConfigForStage(stageName, cc)
fileListResult[stageName] = append(fileListResult[stageName], file)
}
}
mc.logger.Debug(ctx, "Parsed config list", fileListResult)
return mc.getResultConfigForStage(ctx)
}
func (mc *merger) getResultConfigForStage(ctx context.Context) ([]byte, error) {
resultConfig, ok := mc.getConfigForStage(StageNameDefaults)
if !ok {
return nil, ErrNoDefaults
}
if cfg, ok := mc.getConfigForStage(mc.stage.Name()); ok {
if err := mergo.Merge(&resultConfig, cfg, mergo.WithOverride); err != nil {
mc.logger.Error(ctx, "merging with defaults error: %s", err)
return nil, fmt.Errorf("%w: %w", ErrMergingError, err)
}
mc.logger.Debug(ctx, "Stage config is loaded and merged with `defaults`", mc.stage.Name().String())
}
mc.logger.Debug(ctx, "final config", resultConfig)
return yaml.Marshal(resultConfig)
}
func (mc *merger) getConfigForStage(stage StageName) (map[string]any, bool) {
mc.mu.RLock()
defer mc.mu.RUnlock()
if len(mc.configs[stage]) == 0 {
return nil, false
}
cfg := make(map[string]any, len(mc.configs[stage]))
for key, val := range mc.configs[stage] {
cfg[key] = val
}
return cfg, true
}
func (mc *merger) getFileList() error {
var stageDir StageName
return filepath.Walk(mc.cfgPath, func(path string, f os.FileInfo, err error) error {
if mc.cfgPath == path {
return nil
}
if f.IsDir() {
if stageDir.String() == "" || f.Name() == StageNameDefaults.String() || mc.stage.Name().String() == f.Name() {
stageDir = NewStageNameUnsafe(f.Name())
return nil
}
return filepath.SkipDir
}
if fileIsYaml(f.Name()) && (stageDir.isDefault() || mc.isSpecifiedStage(stageDir)) {
mc.addFileToStage(stageDir, f.Name())
}
return nil
})
}
func fileIsYaml(name string) bool {
return filepath.Ext(name) == ".yaml" || filepath.Ext(name) == ".yml"
}
func (mc *merger) isSpecifiedStage(stage StageName) bool {
return mc.stage.Name() == stage
}
func (mc *merger) addFileToStage(stage StageName, file string) {
fileList, ok := mc.fileList[stage]
if !ok || len(fileList) == 0 {
fileList = make([]string, 0, 1)
}
file = mc.cfgPath + "/" + stage.String() + "/" + file
fileList = append(fileList, file)
mc.fileList[stage] = fileList
}
func (mc *merger) defaultConfigExists() bool {
defaultConfig, ok := mc.fileList[StageNameDefaults]
if !ok || len(defaultConfig) == 0 {
return false
}
return true
}
func (mc *merger) setConfigForStage(stageName StageName, cfg map[string]any) {
mc.mu.Lock()
defer mc.mu.Unlock()
mc.configs[stageName] = cfg
}