-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathconfig.go
450 lines (420 loc) · 13.3 KB
/
config.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package qaengine
import (
"bytes"
"fmt"
"os"
"reflect"
"regexp"
"strings"
"github.com/konveyor/move2kube/common"
"github.com/mikefarah/yq/v4/pkg/yqlib"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
logging "gopkg.in/op/go-logging.v1"
"gopkg.in/yaml.v3"
)
type mapT = map[string]interface{}
// nullLogBackend is a used to prevent yq from logging to stdout
type nullLogBackend struct{}
// Config stores the answers in a yaml file
type Config struct {
configFiles []string
configStrings []string
yamlMap mapT
writeYamlMap mapT
OutputPath string
persistPasswords bool
}
var arrayIndexRegex = regexp.MustCompile(`^\[(\d+)\]$`)
// Implement the Store interface
// Load loads and merges config files and strings
func (c *Config) Load() (err error) {
logrus.Debugf("Config.Load")
yamlDatas := []string{}
// config files specified later override earlier config files
for _, configFile := range c.configFiles {
yamlData, err := os.ReadFile(configFile)
if err != nil {
logrus.Errorf("Failed to read the config file %s Error: %q", configFile, err)
continue
}
yamlDatas = append(yamlDatas, string(yamlData))
}
// config strings override config files
// config strings specified later override earlier config strings
for i, configString := range c.configStrings {
if len(configString) == 0 {
logrus.Errorf("The %dth config string is empty", i)
continue
}
if !strings.HasPrefix(configString, common.Delim) {
// given move2kube.services change to .move2kube.services for yq parser.
configString = common.Delim + configString
}
logrus.Debugf("config store load configString: [%s]", configString)
yamlData, err := GenerateYAMLFromExpression(configString)
if err != nil {
logrus.Errorf("Unable to parse the config string %s Error: %q", configString, err)
continue
}
logrus.Debugf("after parsing the yamlData is:\n%s", yamlData)
yamlDatas = append(yamlDatas, yamlData)
}
c.yamlMap, err = MergeYAMLDatasIntoMap(yamlDatas)
c.writeYamlMap = mapT{}
return err
}
func (c *Config) convertAnswer(p Problem, value interface{}) (Problem, error) {
p.Answer = value
return p, nil
}
func (c *Config) normalGetSolution(p Problem) (Problem, error) {
key := p.ID
value, ok := c.Get(key)
if ok {
return c.convertAnswer(p, value)
}
// starting from 2nd last subkey replace with match all selector *
// Example: Given a.b.c.d.e this matches a.b.c.*.e, then a.b.*.d.e, then a.*.c.d.e
subKeys := getSubKeys(key)
for idx := len(subKeys) - 2; idx > 0; idx-- {
baseKey := strings.Join(subKeys[:idx], common.Delim)
lastKeySegment := strings.Join(subKeys[idx+1:], common.Delim)
newKey := baseKey + common.Delim + common.MatchAll + common.Delim + lastKeySegment
v, ok := c.Get(newKey)
if ok {
return c.convertAnswer(p, v)
}
}
return p, fmt.Errorf("no answer found in the config for the problem:%+v", p)
}
func (c *Config) specialGetSolution(p Problem) (Problem, error) {
noAns := fmt.Errorf("no answer found in the config for the problem:%+v", p)
key := p.ID
idx := strings.LastIndex(key, common.Special)
baseKey := key[:idx-len(common.Delim)]
lastKeySegment := key[idx+len(common.Special)+len(common.Delim):]
if baseKey == "" {
return p, noAns
}
baseValue, ok := c.Get(baseKey)
if !ok {
return p, noAns
}
baseValueMap, ok := baseValue.(mapT)
if !ok {
return p, noAns
}
// Given 'a.b.[].d' there should be at least one key 'c' such that 'a.b.c.d' exists.
// Note that 'c' in 'a.b.c.d' does not have to be a valid option for the question.
// We take the mere existence of 'a.b.c.d' to indicate that the user wants to skip the question.
atleastOneKeyHasLastSegment := false
for k := range baseValueMap {
newK := baseKey + common.Delim + `"` + k + `"` + common.Delim + lastKeySegment
lastKeySegmentValue, ok := c.Get(newK)
if !ok {
continue
}
if _, ok := lastKeySegmentValue.(bool); !ok {
logrus.Debugf("Found key %s in the config but the corresponding value is not a boolean. Actual value %v is of type %T", newK, lastKeySegmentValue, lastKeySegmentValue)
return p, noAns
}
atleastOneKeyHasLastSegment = true
break
}
if !atleastOneKeyHasLastSegment {
return p, noAns
}
// found at least one key, so we will try to answer using the defaults
selectedOptions := []string{}
for _, option := range p.Options {
isOptionSelected := true
newKey := baseKey + common.Delim + `"` + option + `"` + common.Delim + lastKeySegment
if newValue, ok := c.Get(newKey); ok {
isOptionSelected, ok = newValue.(bool)
if !ok {
return p, fmt.Errorf("error occurred in special case for multiselect problems. Expected key %s to have boolean value. Actual value is %v of type %T", newKey, newValue, newValue)
}
}
if isOptionSelected {
selectedOptions = append(selectedOptions, option)
}
}
p.Answer = selectedOptions
return p, nil
}
// GetSolution reads a solution from the config
func (c *Config) GetSolution(p Problem) (Problem, error) {
if strings.Contains(p.ID, common.Special) {
if p.Type != MultiSelectSolutionFormType {
return p, fmt.Errorf("cannot use the '%s' selector with non multi-select problems: %+v", common.Special, p)
}
p, err := c.specialGetSolution(p)
if err != nil {
return p, fmt.Errorf("failed to get the solution for the problem using special logic. Error: %w", err)
}
problem, err := Deserialize(p)
if err != nil {
return problem, fmt.Errorf("failed to deserialize the problem. Error: %w", err)
}
return problem, nil
}
p, err := c.normalGetSolution(p)
if err != nil {
return p, fmt.Errorf("failed to get the solution for the problem using normal logic. Error: %w", err)
}
problem, err := Deserialize(p)
if err != nil {
return problem, fmt.Errorf("failed to deserialize the problem. Error: %w", err)
}
return problem, nil
}
// Write writes the config to disk
func (c *Config) Write() error {
logrus.Debugf("Config.Write write the file out")
return common.WriteYaml(c.OutputPath, c.writeYamlMap)
}
// AddSolution adds a problem to the config
func (c *Config) AddSolution(problem Problem) error {
logrus.Trace("Config.AddSolution start")
defer logrus.Trace("Config.AddSolution end")
logrus.Debugf("Config.AddSolution the problem is: %+v", problem)
if problem.Answer == nil {
return fmt.Errorf("unresolved problem. Not going to be added to config")
}
p, err := Serialize(problem)
if err != nil {
return fmt.Errorf("failed to serialize the problem. Error: %w", err)
}
if p.Type != MultiSelectSolutionFormType {
set(p.ID, p.Answer, c.yamlMap)
if p.Type != PasswordSolutionFormType || c.persistPasswords {
set(p.ID, p.Answer, c.writeYamlMap)
if err := c.Write(); err != nil {
return fmt.Errorf("failed to write to the config file. Error: %w", err)
}
return nil
} else if p.Type == PasswordSolutionFormType {
logrus.Debug("passwords won't be added to the config")
}
return nil
}
selectedAnswers, ok := p.Answer.([]string)
if !ok {
iArr, ok := p.Answer.([]interface{})
if !ok {
logrus.Errorf("Unable to convert to []interface{}")
selectedAnswers = []string{}
} else {
for _, iV := range iArr {
selectedAnswers = append(selectedAnswers, iV.(string))
}
}
}
// multi-select problem has 2 cases
key := p.ID
idx := strings.LastIndex(key, common.Special)
if idx < 0 {
// normal case key1 = [val1, val2, val3, ...]
set(key, p.Answer, c.yamlMap)
set(key, p.Answer, c.writeYamlMap)
return nil
}
// special case
baseKey, lastKeySegment := key[:idx-len(common.Delim)], key[idx+len(common.Special)+len(common.Delim):]
if baseKey == "" {
return fmt.Errorf("failed to add the problem\n%+v\nto the config. The base key is empty", p)
}
for _, option := range p.Options {
isOptionSelected := common.IsPresent(selectedAnswers, option)
newKey := baseKey + common.Delim + `"` + option + `"` + common.Delim + lastKeySegment
set(newKey, isOptionSelected, c.yamlMap)
set(newKey, isOptionSelected, c.writeYamlMap)
}
if err := c.Write(); err != nil {
return fmt.Errorf("failed to write to the config file. Error: %w", err)
}
return nil
}
// Get returns the value at the position given by the key in the config
func (c *Config) Get(key string) (value interface{}, ok bool) {
return get(key, c.yamlMap)
}
// NewConfig creates a new config instance given config strings and paths to config files
func NewConfig(outputPath string, configStrings, configFiles []string, persistPasswords bool) (config *Config) {
logrus.Debug("NewConfig create a new config")
return &Config{
configFiles: configFiles,
configStrings: configStrings,
OutputPath: outputPath,
persistPasswords: persistPasswords,
}
}
func (*nullLogBackend) Log(logging.Level, int, *logging.Record) error {
return nil
}
func getPrinterAndEvaluator(buffer *bytes.Buffer) (yqlib.Printer, yqlib.StreamEvaluator) {
logging.SetBackend(new(nullLogBackend))
indentLevel := 2
printer := yqlib.NewPrinterWithSingleWriter(buffer, yqlib.YamlOutputFormat, false, false, indentLevel, false)
evaluator := yqlib.NewStreamEvaluator()
return printer, evaluator
}
// GenerateYAMLFromExpression generates yaml string from yq syntax expression
// Example: The expression .foo.bar="abc" gives:
// foo:
//
// bar: abc
func GenerateYAMLFromExpression(expr string) (string, error) {
logrus.Debugf("GenerateYAMLFromExpression parsing the string [%s]", expr)
logging.SetBackend(new(nullLogBackend))
b := bytes.Buffer{}
printer, evaluator := getPrinterAndEvaluator(&b)
if err := evaluator.EvaluateNew(expr, printer, ""); err != nil {
return "", err
}
return b.String(), nil
}
func isMap(x reflect.Value) bool {
return x.IsValid() && !x.IsZero() && (x.Kind() == reflect.Map || (x.Kind() == reflect.Interface && x.Elem().Kind() == reflect.Map))
}
func mergeRecursively(baseV, overrideV reflect.Value) {
if !isMap(baseV) || !isMap(overrideV) {
return
}
if baseV.Kind() == reflect.Interface {
baseV = baseV.Elem()
}
if overrideV.Kind() == reflect.Interface {
overrideV = overrideV.Elem()
}
for _, k := range overrideV.MapKeys() {
v := overrideV.MapIndex(k)
oldv := baseV.MapIndex(k)
if !isMap(v) || !isMap(oldv) {
baseV.SetMapIndex(k, v)
continue
}
mergeRecursively(oldv, v)
}
}
// merge takes 2 mapTs and merges them together recursively.
func merge(baseI, overrideI interface{}) {
if baseI == nil || overrideI == nil {
return
}
mergeRecursively(reflect.ValueOf(baseI), reflect.ValueOf(overrideI))
}
// MergeYAMLDatasIntoMap merges multiple yamls together into a map.
// Later yamls will override earlier yamls.
func MergeYAMLDatasIntoMap(yamlDatas []string) (mapT, error) {
if len(yamlDatas) == 0 {
return mapT{}, nil
}
if len(yamlDatas) == 1 {
var v mapT
err := yaml.Unmarshal([]byte(yamlDatas[0]), &v)
return v, err
}
vs := make([]interface{}, len(yamlDatas))
for i, yamlData := range yamlDatas {
if err := yaml.Unmarshal([]byte(yamlData), &vs[i]); err != nil {
logrus.Errorf("Error on unmarshalling the %dth yaml:\n%v\nError: %q", i, yamlData, err)
return nil, err
}
}
basev := vs[0]
for _, v := range vs[1:] {
merge(basev, v)
}
return basev.(mapT), nil
}
func getIndex(key string) (int, bool) {
matches := arrayIndexRegex.FindSubmatch([]byte(key))
if matches == nil {
return 0, false
}
idx, err := cast.ToIntE(string(matches[1]))
if err != nil || idx < 0 {
return 0, false
}
return idx, true
}
func get(key string, config interface{}) (value interface{}, ok bool) {
subKeys := getSubKeys(key)
value = config
for _, subKey := range subKeys {
valueMap, ok := value.(mapT)
if ok {
value, ok = valueMap[subKey]
if ok {
continue
}
return value, false
}
valueArr, ok := value.([]interface{})
if ok {
idx, ok := getIndex(subKey)
if ok && idx < len(valueArr) {
value = valueArr[idx]
continue
}
}
return value, false
}
return value, true
}
func set(key string, newValue interface{}, config mapT) {
subKeys := getSubKeys(key)
if len(subKeys) == 1 {
config[key] = newValue
}
// at least 2 sub keys. example: move2kube.key1 = val1
lastIdx := len(subKeys) - 1
var value interface{}
var ok bool
for _, subKey := range subKeys[:lastIdx] {
value, ok = config[subKey]
if !ok {
// sub key doesn't exist
newMap := mapT{}
config[subKey] = newMap
config = newMap
continue
}
valueMap, ok := value.(mapT)
if ok {
config = valueMap
continue
}
// sub key exists but corresponding value is not a map
newMap := mapT{}
config[subKey] = newMap
config = newMap
}
lastSubKey := subKeys[lastIdx]
config[lastSubKey] = newValue
}
func getSubKeys(key string) []string {
unStrippedSubKeys := common.SplitOnDotExpectInsideQuotes(key) // assuming delimiter is dot
subKeys := []string{}
for _, unStrippedSubKey := range unStrippedSubKeys {
subKeys = append(subKeys, common.StripQuotes(unStrippedSubKey))
}
return subKeys
}