/
linter.go
513 lines (442 loc) · 16.3 KB
/
linter.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// MIT License
//
// Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// AINC-NOTE-0815
package yaclint
import (
"fmt"
"reflect"
"strings"
"github.com/kylelemons/godebug/diff"
"github.com/kylelemons/godebug/pretty"
"github.com/swaros/contxt/module/yacl"
"github.com/swaros/contxt/module/yamc"
)
const (
IssueLevelInfo = 2
IssueLevelWarn = 5
IssueLevelError = 10
)
type Linter struct {
config *yacl.ConfigModel // the config model that we need to verify
lMap LintMap // contains the diff chunks
diffFound bool // true if we found a diff. that is just a sign, that an SOME diff is found, not that the config is invalid
highestIssueLevel int // the highest issue level found
structhandler yamc.StructDef // the struct handler for the config file. keeps the struct definition
ldlogger DirtyLoggerDef // quick and dirty logger for the linter
}
// Create a new Linter. The config is the config model that we need to verify
// it must be loaded before, and it must be injected as pointer
func NewLinter(config yacl.ConfigModel) *Linter {
return &Linter{
config: &config,
highestIssueLevel: 0,
}
}
// getUnstructMap loads the config file as generic map and returns it
// as map[string]interface{} and as string (the yaml/json representation)
func (l *Linter) getUnstructMap(loader yamc.DataReader) (map[string]interface{}, error) {
// right now we use the last used file only. support fpor chainloading comes later
fileName := l.config.GetLoadedFile() // the file name of the config file
if fileName == "" {
return nil, fmt.Errorf("no file name found. the config needs to be loaded first. also chainload fom yacl not supported right now")
}
l.Trace("(re)Loading file:", fileName)
// generic map to load the file for comparison
m := make(map[string]interface{})
// first we need to make sure to get the right way to load the file.
// so if yacl is using a costum loader, we need to get the content by the loader.
// but this is possible only, if the tracking of the loaded file is enabled.
if l.config.GetConfig(yacl.ConfigHaveCustomLoader).(bool) {
// okay there is a custom loader. we are f... up if the tracking is not enabled
if !l.config.GetConfig(yacl.ConfigTrackFiles).(bool) {
return nil, fmt.Errorf("custom loader is used, but tracking of loaded file is not enabled")
}
// okay, we have a custom loader and the tracking is enabled. so we can get the content
// by the loader
content, err := l.config.GetFileContent(fileName)
if err != nil {
return nil, err
}
if mErr := loader.Unmarshal(content, &m); mErr != nil {
return nil, mErr
}
} else {
err := loader.FileDecode(fileName, &m)
if err != nil {
return nil, err
}
}
// now we need to marshal the map to get the yaml/json representation
_, lerr := loader.Marshal(m)
if lerr != nil {
return nil, lerr
}
return m, nil
}
// getStructSource creates the yaml/json representation of the config file
func (l *Linter) getStructSource(loader yamc.DataReader) (map[string]interface{}, error) {
cYamc, cerr := l.config.GetAsYmac() // get the configuration as yamc object
if cerr != nil {
return nil, cerr
}
structData := cYamc.GetData() // get the source as map[string]interface{}
return structData, nil
}
// init4read is a helper function that initializes the linter for reading the config file.
func (l *Linter) init4read() (string, string, error) {
yamcLoader := l.config.GetLastUsedReader() // the last used reader from the config
if yamcLoader == nil {
return "", "", fmt.Errorf("no reader found. the config needs to be loaded first")
}
l.Trace("found used data reader: ", reflect.TypeOf(yamcLoader))
l.structhandler = *yamcLoader.GetFields() // get the struct handler from the reader. must be done before the unstructed map is loaded
l.Trace("init4read: structhandler Init: ", l.structhandler.Init)
unStructData, err1 := l.getUnstructMap(yamcLoader)
if err1 != nil {
return "", "", err1
}
structData, err2 := l.getStructSource(yamcLoader)
if err2 != nil {
return "", "", err2
}
niceUnstructed := pretty.CompareConfig.Sprint(unStructData)
niceStructed := pretty.CompareConfig.Sprint(structData)
return niceUnstructed, niceStructed, nil
}
// GetDiff returns the diff between the config file and the structed config file.
// The diff is returned as string.
func (l *Linter) GetDiff() (string, error) {
unstructedSrc, structedSrc, err := l.init4read()
if err != nil {
return "", err
}
return diff.Diff(unstructedSrc, structedSrc), nil
}
// Verify is the main function of the linter. It will verify the config file
// against the structed config file. It will return an error if the config file
// is not valid.
func (l *Linter) Verify() error {
unstructSource, structSource, err := l.init4read()
l.Trace("Verify: unstructSource:\n", unstructSource)
l.Trace("Verify: structSource:\n", structSource)
if err != nil {
return err
}
freeChnk := strings.Split(unstructSource, "\n")
orgiChnk := strings.Split(structSource, "\n")
chunk := diff.DiffChunks(freeChnk, orgiChnk)
l.Trace("Verify: chunk count:", len(chunk))
l.chunkWorker(chunk)
l.Trace("Verify: diff found:", l.diffFound)
if !l.diffFound { // no diff found, so no need to go further
return nil
}
l.findPairs()
l.valueVerify()
return nil
}
// GetHighestIssueLevel returns the highest issue level found.
func (l *Linter) GetHighestIssueLevel() int {
return l.highestIssueLevel
}
// report if we found an issue with the config file, that should not be ignored.
func (l *Linter) HasError() bool {
return l.highestIssueLevel >= IssueLevelError
}
// report if we found an issue with the config file, that is not so important, but be warned.
func (l *Linter) HasWarning() bool {
return l.highestIssueLevel >= IssueLevelWarn
}
// report if we found an issue with the config file, that is most usual, like type conversion. (what is difficult to avoid)
func (l *Linter) HasInfo() bool {
return l.highestIssueLevel >= IssueLevelInfo
}
// if the lint fails and do not report any error, the config could be just invalid for structure parsing.
// this can happen while the config file is tryed to be loaded, but it is not readable.
// this can be the case if the config is injected with an reference of an pointer,
// or it is an array, a map[string]string or an interface{}.
// if this was happens, this function will return the reason why the parsing failed.
func (l *Linter) HaveParsingError() (string, bool) {
if l.structhandler.IgnoredBecauseOf != "" {
return l.structhandler.IgnoredBecauseOf, true
}
return "", false
}
// chunkWorker is a worker that is called for each chunk that is found.
// in the diff. It will create a LintMap that contains the chunks
// for later investigation, if needed.
// if no diff found at all, that is all what we need to do.
func (l *Linter) chunkWorker(chunks []diff.Chunk) {
l.Trace("chunkWorker: chunk count:", len(chunks))
// we need to remember all the keys that are found in the config file.
// so we can build a ordered list of keys.
// this is needed to get the full path of the key. including the parent keys.
var keysAdded []string
var keysRemoved []string
lintResult := LintMap{}
foundDiff := false
// the diff package reports any change as an added and a removed line.
// thats fine for printing a diff, but not for our needs.
// we need to get the context what add and remove is just a change.
// for this we need to count the changes over all chunks.
// in the end, we have an safe match, if we have the same sequence number and the same change number.
sequenceNr := 0
changeNr4Add := 0 // track the index of added lines
changeNr4Rm := 0 // track the index of removed lines
l.structhandler.SetAllowedTagSearch(true)
// iterate over all chunks.
for chunkIndex, c := range chunks {
temporaryChunk := LintChunk{}
needToBeAdded := false
if len(keysAdded) > 0 {
l.Trace("chunkWorker: -- index -- add - (", len(keysAdded), ")", keysAdded)
}
if len(keysRemoved) > 0 {
l.Trace("chunkWorker: -- index -- rm - (", len(keysRemoved), ")", keysRemoved)
}
for _, line := range c.Added {
// skip anything that do not need to be compared like { [ ] }
if isJustAToken(line) {
continue
}
if isObjectOrArray(line) {
keyStr, _, _ := getTokenParts(line)
keysAdded = append(keysAdded, keyStr)
}
l.structhandler.SetIndexSlice(keysAdded)
changeNr4Add++
addToken := NewMatchToken(l.structhandler, l.Trace, &l.lMap, line, changeNr4Add, sequenceNr, true)
temporaryChunk.Added = append(temporaryChunk.Added, &addToken)
needToBeAdded = true
}
for _, line := range c.Deleted {
// ignore non object of array lines
if isJustAToken(line) {
continue
}
if isObjectOrArray(line) {
keyStr, _, _ := getTokenParts(line)
keysRemoved = append(keysRemoved, keyStr)
}
l.structhandler.SetIndexSlice(keysRemoved)
changeNr4Rm++
rmToken := NewMatchToken(l.structhandler, l.Trace, &l.lMap, line, changeNr4Rm, sequenceNr, false)
rmToken.TraceFunc = l.Trace
temporaryChunk.Removed = append(temporaryChunk.Removed, &rmToken)
needToBeAdded = true
}
// on equal we need to reset the indices for removed and added changes, so we can track the
// changes in the next chunk, and get the context what of these removas and adds are just changes
if len(c.Equal) > 0 {
changeNr4Add = 0
changeNr4Rm = 0
sequenceNr += len(c.Equal)
// we need to add the equal lines to the keys, so we can track the context of the keys
// this needs to be done for booth. added and removed keys.
// ignore any single open or close bracket
for _, line := range c.Equal {
// ignore non object of array lines
if isJustAToken(line) {
continue
}
if isObjectOrArray(line) {
keyStr, _, _ := getTokenParts(line)
keysAdded = append(keysAdded, keyStr)
keysRemoved = append(keysRemoved, keyStr)
}
}
}
if needToBeAdded {
temporaryChunk.ChunkNr = chunkIndex
lintResult.Chunks = append(lintResult.Chunks, &LintChunk{
ChunkNr: chunkIndex,
Added: temporaryChunk.Added,
Removed: temporaryChunk.Removed,
})
foundDiff = true
}
}
l.diffFound = foundDiff
// only if we found a diff, we need to store the chunks
// so we can verify the if the diffs matter
if foundDiff {
l.lMap = lintResult
}
}
// find the token that is the pair of the current token
// so it must be deleted in the previous chunk with the same sequence number
func (l *Linter) findPairsHelper(tkn *MatchToken) {
// first we try to find the pair in the best case. that means, we do not have a match depending the keyword.
// we also are in the same chunk, so we be sure, this keyword is the pair.
bestmatchTokens := l.lMap.GetTokensFromSequenceAndIndex(tkn.SequenceNr, tkn.IndexNr)
l.findPairInPairMap(tkn, bestmatchTokens, "findPairsHelper: try optimal case. compare ")
if tkn.PairToken == nil {
// did not found a pair in the best case, so we do not loger stick to the sequence and index
// and just compare the path of the token.
pathToken := l.lMap.GetTokensByTokenPath(tkn)
l.findPairInPairMap(tkn, pathToken, "findPairsHelper: try path only. compare ")
}
}
// findPairInPairMap is a helper to find the pair in a map of tokens.
func (l *Linter) findPairInPairMap(tkn *MatchToken, tkns []*MatchToken, traceMsg string) {
if len(tkns) > 0 {
for _, bestmatch := range tkns {
if bestmatch.Added != tkn.Added {
match := tkn.IsPair(bestmatch) // {bestmatch, tkn}
if traceMsg != "" {
l.Trace(traceMsg, tkn, " and ", bestmatch, " (", match, ")")
}
}
}
}
}
// findPairs is a worker that is called if a diff is found.
// it will find the pairs of the diff chunks.
// a pair is a removed and an added line that are the same.
func (l *Linter) findPairs() {
if l.diffFound {
// depends on the reult of the diff, we need to find the pairs
// in the diff chunks. and they have the matching part in the previous diff chunk.
// so we need to find the matching part in the previous chunk.
for _, chunk := range l.lMap.Chunks {
for _, add := range chunk.Added {
l.findPairsHelper(add)
}
// the deletes
for _, rm := range chunk.Removed {
l.findPairsHelper(rm)
}
}
}
}
// verify the values of the tokens.
// if the values are not the same, we have an issue.
// this is done after all pairs are found.
// so we can verify the values of the pairs.
// also we detect the highest issue level.
func (l *Linter) valueVerify() {
if l.diffFound {
l.walkAll(func(token *MatchToken, added bool) {
token.VerifyValue()
if added {
if l.highestIssueLevel < token.Status {
l.highestIssueLevel = token.Status
}
} else {
// no pair on the removed side.
// so we have an entry that is not defined in the struct.
if token.PairToken == nil {
token.Status = UnknownEntry
}
if l.highestIssueLevel < token.Status {
l.highestIssueLevel = token.Status
}
}
})
}
}
// GetIssue will execute the reportFn for all tokens that have the same or higher level as the given level.
func (l *Linter) GetIssue(level int, reportFn func(token *MatchToken)) {
if l.diffFound {
l.walkAll(func(token *MatchToken, added bool) {
if token.Status >= level {
reportFn(token)
}
})
}
}
// Errors will return all errors found in the diff as a string array
func (l *Linter) Errors() []string {
return l.filterIssueBylevel(IssueLevelError)
}
// Warnings will return all warnings found in the diff as a string array
func (l *Linter) Warnings() []string {
return l.filterIssueBylevel(IssueLevelWarn)
}
// Infos will return all infos found in the diff as a string array
func (l *Linter) Infos() []string {
return l.filterIssueBylevel(IssueLevelInfo)
}
// filterIssueBylevel will filter the issues by the given level.
// the level is ment to equal or higher.
func (l *Linter) filterIssueBylevel(equalOrHigherLevel int) []string {
var out []string
if l.diffFound {
l.walkAll(func(token *MatchToken, added bool) {
if token.Status >= equalOrHigherLevel {
add := "[-]"
if added {
add = "[+]"
}
out = append(out, add+token.ToIssueString())
}
})
}
return out
}
// PrintIssues will print the issues found in the diff.
// This is an report function and show any issue greater than 0.
func (l *Linter) PrintIssues() string {
outPut := ""
if l.diffFound {
l.walkAll(func(token *MatchToken, added bool) {
if token.Status > 0 {
add := "[-]"
if added {
add = "[+]"
}
outPut += add + token.ToIssueString() + "\n"
}
})
}
return outPut
}
// WalkIssues will execute the reportFn for all tokens that have the same or higher level as the given level.
func (l *Linter) WalkIssues(hndlFn func(token *MatchToken, added bool)) {
if l.diffFound {
l.walkAll(func(token *MatchToken, added bool) {
if token.Status > 0 {
hndlFn(token, added)
}
})
}
}
// proxy to walk all
func (l *Linter) walkAll(hndl func(token *MatchToken, added bool)) {
l.lMap.walkAll(hndl)
}
func (l *Linter) SetDirtyLogger(logger DirtyLoggerDef) {
l.ldlogger = logger
}
// Trace is a helper function to trace the linter workflow.
// this might help to debug the linter.
func (l *Linter) Trace(arg ...interface{}) {
if l.ldlogger != nil {
l.ldlogger.Trace(arg...)
}
}
func (l *Linter) GetTrace(orFind ...string) string {
if l.ldlogger != nil {
return strings.Join(l.ldlogger.GetTrace(orFind...), "\n")
}
return ""
}