diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index fb3a47b16..6417d0c11 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "helm.sh/helm/v3/pkg/lint" "helm.sh/helm/v3/pkg/lint/support" "io" "os" @@ -31,9 +32,10 @@ import ( "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/getter" - "helm.sh/helm/v3/pkg/lint/rules" ) +const defaultIgnoreFileName = ".helmlintignore" + var longLintHelp = ` This command takes a path to a chart and runs a series of tests to verify that the chart is well-formed. @@ -50,7 +52,7 @@ func newLintCmd(out io.Writer) *cobra.Command { client.Debug = settings.Debug valueOpts := &values.Options{} var kubeVersion string - var lintIgnoreFile string + var lintIgnoreFilePath string cmd := &cobra.Command{ Use: "lint PATH", @@ -91,7 +93,6 @@ func newLintCmd(out io.Writer) *cobra.Command { return err } - var ignorePatterns map[string][]string var message strings.Builder failed := 0 @@ -99,22 +100,26 @@ func newLintCmd(out io.Writer) *cobra.Command { for _, path := range paths { useTempFile = false - if lintIgnoreFile != "" { - debug("\nUsing ignore file: %s\n", lintIgnoreFile) + if lintIgnoreFilePath != "" { + debug("\nUsing ignore file: %s\n", lintIgnoreFilePath) } else { - lintIgnoreFile = filepath.Join(path, ".helmlintignore") - debug("\nNo HelmLintIgnore file specified, will try and use the following: %s\n", lintIgnoreFile) + lintIgnoreFilePath = filepath.Join(path, defaultIgnoreFileName) + debug("\nNo HelmLintIgnore file specified, will try and use the following: %s\n", lintIgnoreFilePath) useTempFile = true // Mark that a temporary file was used } - ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile) + ignorer, err := lint.NewIgnorer(lintIgnoreFilePath) + if err != nil { + debug("Unable to load lint ignore rules: %s", err.Error()) + ignorer = &lint.Ignorer{Patterns: map[string][]string{} } + } if useTempFile { - lintIgnoreFile = "" + lintIgnoreFilePath = "" } result := client.Run([]string{path}, vals) - result.Messages = rules.FilterIgnoredMessages(result.Messages, ignorePatterns) - result.Errors = rules.FilterIgnoredErrors(result.Errors, ignorePatterns) + result.Messages = ignorer.FilterIgnoredMessages(result.Messages) + result.Errors = ignorer.FilterIgnoredErrors(result.Errors) - // If there is no errors/warnings and quiet flag is set + // If there are no errors/warnings and quiet flag is set // go to the next chart hasWarningsOrErrors := action.HasWarningsOrErrors(result) if hasWarningsOrErrors { @@ -170,7 +175,7 @@ func newLintCmd(out io.Writer) *cobra.Command { f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts") f.BoolVar(&client.Quiet, "quiet", false, "print only warnings and errors") f.StringVar(&kubeVersion, "kube-version", "", "Kubernetes version used for capabilities and deprecation checks") - f.StringVar(&lintIgnoreFile, "lint-ignore-file", "", "path to .helmlintignore file to specify ignore patterns") + f.StringVar(&lintIgnoreFilePath, "lint-ignore-file", "", "path to .helmlintignore file to specify ignore patterns") addValueOptionsFlags(f, valueOpts) return cmd diff --git a/cmd/helm/lint_test.go b/cmd/helm/lint_test.go index 08d449f23..694f91fd0 100644 --- a/cmd/helm/lint_test.go +++ b/cmd/helm/lint_test.go @@ -90,8 +90,3 @@ func TestLintCmdWithKubeVersionFlag(t *testing.T) { }} runTestCmd(t, tests) } - -func TestLintFileCompletion(t *testing.T) { - checkFileCompletion(t, "lint", true) - checkFileCompletion(t, "lint mypath", true) // Multiple paths can be given -} \ No newline at end of file diff --git a/pkg/lint/ignore.go b/pkg/lint/ignore.go new file mode 100644 index 000000000..650d48210 --- /dev/null +++ b/pkg/lint/ignore.go @@ -0,0 +1,155 @@ +package lint + +import ( + "bufio" + "fmt" + "helm.sh/helm/v3/pkg/lint/support" + "log" + "os" + "path/filepath" + "strings" +) + +var Debug bool + +type Ignorer struct { + Patterns map[string][]string +} + +func NewIgnorer(ignoreFilePath string) (*Ignorer, error) { + patterns, err := parseFromFilePath(ignoreFilePath) + if err != nil { + return nil, fmt.Errorf("failed to parse ignore file: %w", err) + } + + return &Ignorer{ Patterns: patterns }, nil +} + +func (i *Ignorer) MatchMessage(msg support.Message) bool { + errText := msg.Err.Error() + errorFullPath := extractFullPathFromError(errText) + if len(errorFullPath) == 0 { + debug("Unable to find a path for message, guess we'll keep it: %s", errText) + return false + } + debug("Extracted full path: %s\n", errorFullPath) + for ignorablePath, pathPatterns := range i.Patterns { + cleanIgnorablePath := filepath.Clean(ignorablePath) + if strings.Contains(errorFullPath, cleanIgnorablePath) { + for _, pattern := range pathPatterns { + if strings.Contains(msg.Err.Error(), pattern) { + debug("Ignoring message: [%s] %s\n\n", errorFullPath, errText) + return true + } + } + } + } + debug("keeping unignored message: [%s]", errText) + return false +} + +func (i *Ignorer) MatchError(err error) bool { + errText := err.Error() + errorFullPath := extractFullPathFromError(errText) + if len(errorFullPath) == 0 { + debug("Unable to find a path for error, guess we'll keep it: %s", errText) + return false + } + debug("Extracted full path: %s\n", errorFullPath) + for ignorablePath, pathPatterns := range i.Patterns { + cleanIgnorablePath := filepath.Clean(ignorablePath) + if strings.Contains(errorFullPath, cleanIgnorablePath) { + for _, pattern := range pathPatterns { + if strings.Contains(err.Error(), pattern) { + debug("Ignoring error: [%s] %s\n\n", errorFullPath, errText) + return true + } + } + } + } + debug("keeping unignored error: [%s]", errText) + return false +} + +func (i *Ignorer) FilterIgnoredErrors(errors []error) []error { + filteredErrors := make([]error, 0) + for _, err := range errors { + if !i.MatchError(err) { + filteredErrors = append(filteredErrors, err) + } + } + return filteredErrors +} + +func (i *Ignorer) FilterIgnoredMessages(messages []support.Message) []support.Message { + filteredMessages := make([]support.Message, 0) + for _, msg := range messages { + if !i.MatchMessage(msg) { + filteredMessages = append(filteredMessages, msg) + } + } + return filteredMessages +} + +// TODO: figure out & fix or remove +func extractFullPathFromError(errorString string) string { + parts := strings.Split(errorString, ":") + if len(parts) > 2 { + return strings.TrimSpace(parts[1]) + } + return "" +} + +func parseFromFilePath(filePath string) (map[string][]string, error) { + patterns := make(map[string][]string) + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer func() { + err := file.Close() + if err != nil { + log.Printf("Failed to close ignore file at [%s]: %v", filePath, err) + } + }() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line != "" && !strings.HasPrefix(line, "#") { + parts := strings.SplitN(line, " ", 2) + if len(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 { + // Add an empty pattern if only the path is present + patterns[parts[0]] = append(patterns[parts[0]], "") + } + } + } + + return patterns, scanner.Err() +} + +// TODO: DELETE +var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile) +func debug(format string, v ...interface{}) { + if Debug { + format = fmt.Sprintf("[debug] %s\n", format) + logger.Output(2, fmt.Sprintf(format, v...)) + } +} +// END TODO: DELETE + + +/* TODO HIP-0019 +- find ignore file path for a subchart +- add a chart or two for the end to end tests via testdata like in pkg/lint/lint_test.go +- review debug / output patterns across the helm project + +Later/never +- XDG support +- helm config file support +- ignore file validation +- +*/ diff --git a/pkg/lint/rules/ignore_test.go b/pkg/lint/ignore_test.go similarity index 92% rename from pkg/lint/rules/ignore_test.go rename to pkg/lint/ignore_test.go index 5967344ab..550c70dc6 100644 --- a/pkg/lint/rules/ignore_test.go +++ b/pkg/lint/ignore_test.go @@ -1,4 +1,4 @@ -package rules +package lint import ( "fmt" @@ -64,7 +64,8 @@ func TestFilterIgnoredMessages(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := FilterIgnoredMessages(tt.args.messages, tt.args.ignorePatterns) + ignorer := Ignorer{Patterns: tt.args.ignorePatterns} + got := ignorer.FilterIgnoredMessages(tt.args.messages) assert.Equalf(t, tt.want, got, "FilterIgnoredMessages(%v, %v)", tt.args.messages, tt.args.ignorePatterns) }) } diff --git a/pkg/lint/rules/ignore.go b/pkg/lint/rules/ignore.go deleted file mode 100644 index a17a2bffc..000000000 --- a/pkg/lint/rules/ignore.go +++ /dev/null @@ -1,147 +0,0 @@ -package rules - -import ( - "bufio" - "fmt" - "helm.sh/helm/v3/pkg/lint/support" - "log" - "os" - "path/filepath" - "strings" -) - -var Debug bool - -func ParseIgnoreFile(filePath string) (map[string][]string, error) { - patterns := make(map[string][]string) - file, err := os.Open(filePath) - if err != nil { - return nil, err - } - defer func() { - err := file.Close() - if err != nil { - log.Printf("Failed to close ignore file at [%s]: %v", filePath, err) - } - }() - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line != "" && !strings.HasPrefix(line, "#") { - parts := strings.SplitN(line, " ", 2) - if len(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 { - // Add an empty pattern if only the path is present - patterns[parts[0]] = append(patterns[parts[0]], "") - } - } - } - - return patterns, scanner.Err() -} - -func FilterIgnoredErrors(errors []error, patterns map[string][]string) []error { - filteredErrors := make([]error, 0) - for _, err := range errors { - errText := err.Error() - errorFullPath := extractFullPathFromError(errText) - if len(errorFullPath) == 0 { - debug("Unable to find a path for error, guess we'll keep it: %s", errText) - filteredErrors = append(filteredErrors, err) - continue - } - ignore := false - debug("Extracted full path: %s\n", errorFullPath) - for ignorablePath, pathPatterns := range patterns { - cleanIgnorablePath := filepath.Clean(ignorablePath) - if strings.Contains(errorFullPath, cleanIgnorablePath) { - for _, pattern := range pathPatterns { - if strings.Contains(err.Error(), pattern) { - debug("Ignoring error: [%s] %s\n\n", errorFullPath, errText) - ignore = true - break - } - } - } - if ignore { - break - } - } - if !ignore { - debug("keeping unignored error: [%s]", errText) - filteredErrors = append(filteredErrors, err) - } - } - - return filteredErrors -} -func FilterIgnoredMessages(messages []support.Message, patterns map[string][]string) []support.Message { - filteredMessages := make([]support.Message, 0) - for _, msg := range messages { - errText := msg.Err.Error() - errorFullPath := extractFullPathFromError(errText) - if len(errorFullPath) == 0 { - debug("Unable to find a path for message, guess we'll keep it: %s", errText) - filteredMessages = append(filteredMessages, msg) - continue - } - ignore := false - debug("Extracted full path: %s\n", errorFullPath) - for ignorablePath, pathPatterns := range patterns { - cleanIgnorablePath := filepath.Clean(ignorablePath) - if strings.Contains(errorFullPath, cleanIgnorablePath) { - for _, pattern := range pathPatterns { - if strings.Contains(msg.Err.Error(), pattern) { - debug("Ignoring message: [%s] %s\n\n", errorFullPath, errText) - ignore = true - break - } - } - } - if ignore { - break - } - } - if !ignore { - debug("keeping unignored message: [%s]", errText) - filteredMessages = append(filteredMessages, msg) - } - } - - return filteredMessages -} - -// TODO: figure out & fix or remove -func extractFullPathFromError(errorString string) string { - parts := strings.Split(errorString, ":") - if len(parts) > 2 { - return strings.TrimSpace(parts[1]) - } - return "" -} - -// TODO: DELETE -var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile) -func debug(format string, v ...interface{}) { - if Debug { - format = fmt.Sprintf("[debug] %s\n", format) - logger.Output(2, fmt.Sprintf(format, v...)) - } -} -// END TODO: DELETE - - -/* TODO HIP-0019 -- find ignore file path for a subchart -- add a chart or two for the end to end tests via testdata like in pkg/lint/lint_test.go -- review debug / output patterns across the helm project - -Later/never -- XDG support -- helm config file support -- ignore file validation -- -*/