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 return err
print("this is the error\n") print("this is the error\n")
} }
var ignorePatterns map[string]string var ignorePatterns map[string][]string
if lintIgnoreFile == "" { if lintIgnoreFile == "" {
// Uncomment to debug: // Uncomment to debug:
// print("empty") // print("empty")
@ -112,19 +112,17 @@ func newLintCmd(out io.Writer) *cobra.Command {
errorsOrWarnings := 0 errorsOrWarnings := 0
for _, path := range paths { for _, path := range paths {
if rules.IsIgnored(path, ignorePatterns) {
continue
}
result := client.Run([]string{path}, vals) result := client.Run([]string{path}, vals)
filteredResult := FilterIgnoredMessages(result, ignorePatterns)
hasWarningsOrErrors := action.HasWarningsOrErrors(result) hasWarningsOrErrors := len(filteredResult.Messages) > 0
if hasWarningsOrErrors { if hasWarningsOrErrors {
errorsOrWarnings++ errorsOrWarnings++
} }
if client.Quiet && !hasWarningsOrErrors { if client.Quiet && !hasWarningsOrErrors {
continue continue
} }
fmt.Fprintf(&message, "==> Linting %s\n", path) fmt.Fprintf(&message, "==> Linting %s\n", path)
@ -171,3 +169,31 @@ func newLintCmd(out io.Writer) *cobra.Command {
return cmd 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 { func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, lintIgnoreFile string) support.Linter {
chartDir, _ := filepath.Abs(basedir) chartDir, _ := filepath.Abs(basedir)
var ignorePatterns map[string]string var ignorePatterns map[string][]string
var err error var err error
if lintIgnoreFile != "" { if lintIgnoreFile != "" {
ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile) ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile)

@ -6,8 +6,8 @@ import (
"strings" "strings"
) )
func ParseIgnoreFile(filePath string) (map[string]string, error) { func ParseIgnoreFile(filePath string) (map[string][]string, error) {
patterns := make(map[string]string) patterns := make(map[string][]string)
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -20,12 +20,14 @@ func ParseIgnoreFile(filePath string) (map[string]string, error) {
if line != "" && !strings.HasPrefix(line, "#") { if line != "" && !strings.HasPrefix(line, "#") {
parts := strings.SplitN(line, " ", 2) parts := strings.SplitN(line, " ", 2)
if len(parts) > 1 { 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 { } 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]], "")
} }
} }
} }
return patterns, scanner.Err() return patterns, scanner.Err()
} }

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

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

Loading…
Cancel
Save