fixed teh filtering function--need to be moved

Signed-off-by: Danilo Patrucco <danilo.patrucco@gmail.com>
pull/13205/head
Danilo Patrucco 1 year ago
parent 00809584d4
commit 35874a3d8d

@ -85,7 +85,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
return err
print("this is the error\n")
}
var ignorePatterns map[string]string
var ignorePatterns map[string][]string
if lintIgnoreFile == "" {
// Uncomment to debug:
// print("empty")
@ -112,13 +112,10 @@ func newLintCmd(out io.Writer) *cobra.Command {
errorsOrWarnings := 0
for _, path := range paths {
if rules.IsIgnored(path, ignorePatterns) {
continue
}
result := client.Run([]string{path}, vals)
hasWarningsOrErrors := action.HasWarningsOrErrors(result)
filteredResult := FilterIgnoredMessages(result, ignorePatterns)
hasWarningsOrErrors := len(filteredResult.Messages) > 0
if hasWarningsOrErrors {
errorsOrWarnings++
}
@ -126,6 +123,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
continue
}
fmt.Fprintf(&message, "==> Linting %s\n", path)
if len(result.Messages) == 0 {
@ -171,3 +169,31 @@ func newLintCmd(out io.Writer) *cobra.Command {
return cmd
}
// Need to figure out how to pull this function out of here
func FilterIgnoredMessages(result *action.LintResult, patterns map[string][]string) *action.LintResult {
filteredMessages := make([]support.Message, 0)
for _, msg := range result.Messages {
ignore := false
for path, pathPatterns := range patterns {
cleanedPath := filepath.Clean(path)
if strings.Contains(msg.Path, cleanedPath) { // Check if the message path matches the ignored path
for _, pattern := range pathPatterns {
if strings.Contains(msg.Err.Error(), pattern) { // Assuming we are matching patterns against the error message
fmt.Printf("Ignoring message: [%s] %s\n", msg.Path, msg.Err.Error())
ignore = true
break
}
}
}
if ignore {
break
}
}
if !ignore {
filteredMessages = append(filteredMessages, msg)
}
}
return &action.LintResult{Messages: filteredMessages}
}

@ -30,7 +30,7 @@ func All(basedir string, values map[string]interface{}, namespace string, _ bool
}
func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, lintIgnoreFile string) support.Linter {
chartDir, _ := filepath.Abs(basedir)
var ignorePatterns map[string]string
var ignorePatterns map[string][]string
var err error
if lintIgnoreFile != "" {
ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile)

@ -6,8 +6,8 @@ import (
"strings"
)
func ParseIgnoreFile(filePath string) (map[string]string, error) {
patterns := make(map[string]string)
func ParseIgnoreFile(filePath string) (map[string][]string, error) {
patterns := make(map[string][]string)
file, err := os.Open(filePath)
if err != nil {
return nil, err
@ -20,9 +20,11 @@ func ParseIgnoreFile(filePath string) (map[string]string, error) {
if line != "" && !strings.HasPrefix(line, "#") {
parts := strings.SplitN(line, " ", 2)
if len(parts) > 1 {
patterns[parts[0]] = parts[1]
// Check if the key already exists and append to its slice
patterns[parts[0]] = append(patterns[parts[0]], parts[1])
} else if len(parts) == 1 {
patterns[parts[0]] = ""
// Add an empty pattern if only the path is present
patterns[parts[0]] = append(patterns[parts[0]], "")
}
}
}

@ -6,20 +6,22 @@ import (
"fmt"
)
func IsIgnored(path string, patterns map[string]string) bool {
for pattern, _ := range patterns {
type LintResult struct {
Messages []string
}
func IsIgnored(errorMessage string, patterns map[string][]string) bool {
for path, pathPatterns := range patterns {
cleanedPath := filepath.Clean(path)
cleanedPattern := filepath.Clean(pattern)
if match, err := filepath.Match(cleanedPattern, cleanedPath); err == nil && match {
fmt.Printf("Ignoring path: %s due to pattern: %s\n", path, pattern)
if strings.Contains(errorMessage, cleanedPath) {
for _, pattern := range pathPatterns {
if strings.Contains(errorMessage, pattern) {
fmt.Printf("Ignoring error related to path: %s with pattern: %s\n", path, pattern)
return true
}
if strings.HasSuffix(cleanedPattern, "/") || strings.HasSuffix(cleanedPattern, "\\") {
patternDir := strings.TrimRight(cleanedPattern, "/\\")
if strings.HasPrefix(cleanedPath, patternDir) {
return true
}
}
}
return false
}

@ -33,6 +33,7 @@ const (
// sev matches the *Sev states.
var sev = []string{"UNKNOWN", "INFO", "WARNING", "ERROR"}
// Linter encapsulates a linting run of a particular chart.
type Linter struct {
Messages []Message

Loading…
Cancel
Save