|
| 1 | +package ignore |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "log/slog" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// RuleLoader provides a means of suppressing unwanted helm lint errors and messages |
| 15 | +// by comparing them to an ignore list provided in a plaintext helm lint ignore file. |
| 16 | +type RuleLoader struct { |
| 17 | + Matchers []MatchesErrors |
| 18 | + debugFnOverride func(string, ...interface{}) |
| 19 | +} |
| 20 | + |
| 21 | +func (i *RuleLoader) LogAttrs() slog.Attr { |
| 22 | + return slog.Group("RuleLoader", |
| 23 | + slog.String("Matchers", fmt.Sprintf("%v", i.Matchers)), |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +// DefaultIgnoreFileName is the name of the lint ignore file |
| 28 | +// an RuleLoader will seek out at load/parse time. |
| 29 | +const DefaultIgnoreFileName = ".helmlintignore" |
| 30 | + |
| 31 | +const NoMessageText = "" |
| 32 | + |
| 33 | +// NewRuleLoader builds an RuleLoader object that enables helm to discard specific lint result Messages |
| 34 | +// and Errors should they match the ignore rules in the specified .helmlintignore file. |
| 35 | +func NewRuleLoader(chartPath, ignoreFilePath string, debugLogFn func(string, ...interface{})) (*RuleLoader, error) { |
| 36 | + out := &RuleLoader{ |
| 37 | + debugFnOverride: debugLogFn, |
| 38 | + } |
| 39 | + |
| 40 | + if ignoreFilePath == "" { |
| 41 | + ignoreFilePath = filepath.Join(chartPath, DefaultIgnoreFileName) |
| 42 | + out.Debug("\nNo HelmLintIgnore file specified, will try and use the following: %s\n", ignoreFilePath) |
| 43 | + } |
| 44 | + |
| 45 | + // attempt to load ignore patterns from ignoreFilePath. |
| 46 | + // if none are found, return an empty ignorer so the program can keep running. |
| 47 | + out.Debug("\nUsing ignore file: %s\n", ignoreFilePath) |
| 48 | + file, err := os.Open(ignoreFilePath) |
| 49 | + if err != nil { |
| 50 | + out.Debug("failed to open lint ignore file: %s", ignoreFilePath) |
| 51 | + return out, nil |
| 52 | + } |
| 53 | + defer file.Close() |
| 54 | + |
| 55 | + out.LoadFromReader(file) |
| 56 | + out.Debug("RuleLoader loaded.", out.LogAttrs()) |
| 57 | + return out, nil |
| 58 | +} |
| 59 | + |
| 60 | +// Debug provides an RuleLoader with a caller-overridable logging function |
| 61 | +// intended to match the behavior of the top level debug() method from package main. |
| 62 | +// |
| 63 | +// When no i.debugFnOverride is present Debug will fall back to a naive |
| 64 | +// implementation that assumes all debug output should be logged and not swallowed. |
| 65 | +func (i *RuleLoader) Debug(format string, args ...interface{}) { |
| 66 | + if i.debugFnOverride == nil { |
| 67 | + i.debugFnOverride = func(format string, v ...interface{}) { |
| 68 | + format = fmt.Sprintf("[debug] %s\n", format) |
| 69 | + log.Output(2, fmt.Sprintf(format, v...)) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + i.debugFnOverride(format, args...) |
| 74 | +} |
| 75 | + |
| 76 | +// TODO: figure out & fix or remove |
| 77 | +func extractFullPathFromError(errText string) (string, error) { |
| 78 | + delimiter := ":" |
| 79 | + // splits into N parts delimited by colons |
| 80 | + parts := strings.Split(errText, delimiter) |
| 81 | + // if 3 or more parts, return the second part, after trimming its spaces |
| 82 | + if len(parts) > 2 { |
| 83 | + return strings.TrimSpace(parts[1]), nil |
| 84 | + } |
| 85 | + // if fewer than 3 parts, return empty string |
| 86 | + return "", fmt.Errorf("fewer than three [%s]-delimited parts found, no path here: %s", delimiter, errText) |
| 87 | +} |
| 88 | + |
| 89 | +func (i *RuleLoader) LoadFromReader(rdr io.Reader) { |
| 90 | + const pathlessPatternPrefix = "error_lint_ignore=" |
| 91 | + scanner := bufio.NewScanner(rdr) |
| 92 | + for scanner.Scan() { |
| 93 | + line := strings.TrimSpace(scanner.Text()) |
| 94 | + if line == "" || strings.HasPrefix(line, "#") { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + isPathlessPattern := strings.HasPrefix(line, pathlessPatternPrefix) |
| 99 | + |
| 100 | + if isPathlessPattern { |
| 101 | + i.storePathlessPattern(line, pathlessPatternPrefix) |
| 102 | + } else { |
| 103 | + i.storePathfulPattern(line) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (i *RuleLoader) storePathlessPattern(line string, pathlessPatternPrefix string) { |
| 109 | + // handle chart-level errors |
| 110 | + // Drop 'error_lint_ignore=' prefix from rule before saving it |
| 111 | + const numSplits = 2 |
| 112 | + tokens := strings.SplitN(line[len(pathlessPatternPrefix):], pathlessPatternPrefix, numSplits) |
| 113 | + if len(tokens) == numSplits { |
| 114 | + // TODO: find an example for this one - not sure we still use it |
| 115 | + messageText, _ := tokens[0], tokens[1] |
| 116 | + i.Matchers = append(i.Matchers, PathlessRule{RuleText: line, MessageText: messageText}) |
| 117 | + } else { |
| 118 | + messageText := tokens[0] |
| 119 | + i.Matchers = append(i.Matchers, PathlessRule{RuleText: line, MessageText: messageText}) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (i *RuleLoader) storePathfulPattern(line string) { |
| 124 | + const separator = " " |
| 125 | + const numSplits = 2 |
| 126 | + |
| 127 | + // handle chart yaml file errors in specific template files |
| 128 | + parts := strings.SplitN(line, separator, numSplits) |
| 129 | + if len(parts) == numSplits { |
| 130 | + messagePath, messageText := parts[0], parts[1] |
| 131 | + i.Matchers = append(i.Matchers, Rule{RuleText: line, MessagePath: messagePath, MessageText: messageText}) |
| 132 | + } else { |
| 133 | + messagePath := parts[0] |
| 134 | + i.Matchers = append(i.Matchers, Rule{RuleText: line, MessagePath: messagePath, MessageText: NoMessageText}) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (i *RuleLoader) loadFromFilePath(filePath string) { |
| 139 | + file, err := os.Open(filePath) |
| 140 | + if err != nil { |
| 141 | + i.Debug("failed to open lint ignore file: %s", filePath) |
| 142 | + return |
| 143 | + } |
| 144 | + defer file.Close() |
| 145 | + i.LoadFromReader(file) |
| 146 | +} |
0 commit comments