Merge pull request #3 from danilo-patrucco/HIP-0019-fixup-cmd

Restores some of the bits of `cmd/helm/lint.go` we lost along the way
pull/13207/head
danilo patrucco 1 year ago committed by GitHub
commit bb28ce45fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -18,6 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"helm.sh/helm/v3/pkg/lint/support"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -58,6 +59,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
if len(args) > 0 { if len(args) > 0 {
paths = args paths = args
} }
if kubeVersion != "" { if kubeVersion != "" {
parsedKubeVersion, err := chartutil.ParseKubeVersion(kubeVersion) parsedKubeVersion, err := chartutil.ParseKubeVersion(kubeVersion)
if err != nil { if err != nil {
@ -65,6 +67,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
} }
client.KubeVersion = parsedKubeVersion client.KubeVersion = parsedKubeVersion
} }
if client.WithSubcharts { if client.WithSubcharts {
for _, p := range paths { for _, p := range paths {
filepath.Walk(filepath.Join(p, "charts"), func(path string, info os.FileInfo, _ error) error { filepath.Walk(filepath.Join(p, "charts"), func(path string, info os.FileInfo, _ error) error {
@ -79,40 +82,75 @@ func newLintCmd(out io.Writer) *cobra.Command {
}) })
} }
} }
client.Namespace = settings.Namespace() client.Namespace = settings.Namespace()
vals, err := valueOpts.MergeValues(getter.All(settings)) vals, err := valueOpts.MergeValues(getter.All(settings))
if err != nil { if err != nil {
return err return err
} }
var ignorePatterns map[string][]string var ignorePatterns map[string][]string
if lintIgnoreFile != "" { if lintIgnoreFile != "" {
debug("\nUsing ignore file: %s\n", lintIgnoreFile) debug("\nUsing ignore file: %s\n", lintIgnoreFile)
ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile) ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile)
} }
var message strings.Builder var message strings.Builder
failed := 0 failed := 0
errorsOrWarnings := 0
for _, path := range paths { for _, path := range paths {
fmt.Fprintf(&message, "==> Linting %s\n", path)
result := client.Run([]string{path}, vals) result := client.Run([]string{path}, vals)
result.Messages = rules.FilterIgnoredMessages(result.Messages, ignorePatterns) result.Messages = rules.FilterIgnoredMessages(result.Messages, ignorePatterns)
result.Errors = rules.FilterIgnoredErrors(result.Errors, ignorePatterns) result.Errors = rules.FilterIgnoredErrors(result.Errors, ignorePatterns)
for _, msg := range result.Messages {
fmt.Fprintf(&message, "%s\n", msg) // If there is no errors/warnings and quiet flag is set
// go to the next chart
hasWarningsOrErrors := action.HasWarningsOrErrors(result)
if hasWarningsOrErrors {
errorsOrWarnings++
} }
if len(result.Messages) != 0 { if client.Quiet && !hasWarningsOrErrors {
failed++ continue
}
fmt.Fprintf(&message, "==> Linting %s\n", path)
// All the Errors that are generated by a chart
// that failed a lint will be included in the
// results.Messages so we only need to print
// the Errors if there are no Messages.
if len(result.Messages) == 0 {
for _, err := range result.Errors { for _, err := range result.Errors {
fmt.Fprintf(&message, "Error: %s\n", err) fmt.Fprintf(&message, "Error %s\n", err)
}
}
for _, msg := range result.Messages {
if !client.Quiet || msg.Severity > support.InfoSev {
fmt.Fprintf(&message, "%s\n", msg)
} }
} }
if len(result.Errors) != 0 {
failed++
}
// Adding extra new line here to break up the
// results, stops this from being a big wall of
// text and makes it easier to follow.
fmt.Fprint(&message, "\n")
} }
fmt.Fprint(out, message.String()) fmt.Fprint(out, message.String())
summary := fmt.Sprintf("%d chart(s) linted, %d chart(s) failed", len(paths), failed) summary := fmt.Sprintf("%d chart(s) linted, %d chart(s) failed", len(paths), failed)
if failed > 0 { if failed > 0 {
return errors.New(summary) return errors.New(summary)
} }
fmt.Fprintln(out, summary) if !client.Quiet || errorsOrWarnings > 0 {
fmt.Fprintln(out, summary)
}
return nil return nil
}, },
} }
@ -123,6 +161,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
f.BoolVar(&client.Quiet, "quiet", false, "print only warnings and errors") 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(&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(&lintIgnoreFile, "lint-ignore-file", "", "path to .helmlintignore file to specify ignore patterns")
addValueOptionsFlags(f, valueOpts)
return cmd return cmd
} }

Loading…
Cancel
Save