From 383086d39742c7cff548e8a42b0e7881676557a2 Mon Sep 17 00:00:00 2001 From: Piotr Resztak Date: Mon, 6 Sep 2021 22:08:52 +0200 Subject: [PATCH] feat(*): add --quiet flag to 'helm lint' Quiet flag reduce the verbosity, such that the only text that is written to the console is lint warnings/errors Closes #9994 Signed-off-by: Piotr Resztak --- cmd/helm/lint.go | 25 ++++++++++++++++--- cmd/helm/lint_test.go | 21 ++++++++++++++++ .../testdata/output/lint-quiet-with-error.txt | 8 ++++++ .../output/lint-quiet-with-warning.txt | 4 +++ cmd/helm/testdata/output/lint-quiet.txt | 0 pkg/action/lint.go | 11 ++++++++ 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 cmd/helm/testdata/output/lint-quiet-with-error.txt create mode 100644 cmd/helm/testdata/output/lint-quiet-with-warning.txt create mode 100644 cmd/helm/testdata/output/lint-quiet.txt diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index a7aac172a..73a37b6fe 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -29,6 +29,7 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/getter" + "helm.sh/helm/v3/pkg/lint/support" ) var longLintHelp = ` @@ -76,12 +77,23 @@ func newLintCmd(out io.Writer) *cobra.Command { var message strings.Builder failed := 0 + errorsOrWarnings := 0 for _, path := range paths { - fmt.Fprintf(&message, "==> Linting %s\n", path) - result := client.Run([]string{path}, vals) + // If there is no errors/warnings and quiet flag is set + // go to the next chart + hasWarningsOrErrors := action.HasWarningsOrErrors(result) + if hasWarningsOrErrors { + errorsOrWarnings++ + } + if client.Quiet && !hasWarningsOrErrors { + 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 @@ -93,7 +105,9 @@ func newLintCmd(out io.Writer) *cobra.Command { } for _, msg := range result.Messages { - fmt.Fprintf(&message, "%s\n", msg) + if !client.Quiet || msg.Severity > support.InfoSev { + fmt.Fprintf(&message, "%s\n", msg) + } } if len(result.Errors) != 0 { @@ -112,7 +126,9 @@ func newLintCmd(out io.Writer) *cobra.Command { if failed > 0 { return errors.New(summary) } - fmt.Fprintln(out, summary) + if !client.Quiet || errorsOrWarnings > 0 { + fmt.Fprintln(out, summary) + } return nil }, } @@ -120,6 +136,7 @@ func newLintCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.BoolVar(&client.Strict, "strict", false, "fail on lint warnings") f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts") + f.BoolVar(&client.Quiet, "quiet", false, "print only warnings and errors") addValueOptionsFlags(f, valueOpts) return cmd diff --git a/cmd/helm/lint_test.go b/cmd/helm/lint_test.go index 3501ccf87..ebea09bf0 100644 --- a/cmd/helm/lint_test.go +++ b/cmd/helm/lint_test.go @@ -37,6 +37,27 @@ func TestLintCmdWithSubchartsFlag(t *testing.T) { runTestCmd(t, tests) } +func TestLintCmdWithQuietFlag(t *testing.T) { + testChart1 := "testdata/testcharts/alpine" + testChart2 := "testdata/testcharts/chart-bad-requirements" + tests := []cmdTestCase{{ + name: "lint good chart using --quiet flag", + cmd: fmt.Sprintf("lint --quiet %s", testChart1), + golden: "output/lint-quiet.txt", + }, { + name: "lint two charts, one with error using --quiet flag", + cmd: fmt.Sprintf("lint --quiet %s %s", testChart1, testChart2), + golden: "output/lint-quiet-with-error.txt", + wantError: true, + }, { + name: "lint chart with warning using --quiet flag", + cmd: "lint --quiet testdata/testcharts/chart-with-only-crds", + golden: "output/lint-quiet-with-warning.txt", + }} + runTestCmd(t, tests) + +} + func TestLintFileCompletion(t *testing.T) { checkFileCompletion(t, "lint", true) checkFileCompletion(t, "lint mypath", true) // Multiple paths can be given diff --git a/cmd/helm/testdata/output/lint-quiet-with-error.txt b/cmd/helm/testdata/output/lint-quiet-with-error.txt new file mode 100644 index 000000000..a4e8575f8 --- /dev/null +++ b/cmd/helm/testdata/output/lint-quiet-with-error.txt @@ -0,0 +1,8 @@ +==> Linting testdata/testcharts/chart-bad-requirements +[ERROR] Chart.yaml: unable to parse YAML + error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator +[WARNING] templates/: directory not found +[ERROR] : unable to load chart + cannot load Chart.yaml: error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator + +Error: 2 chart(s) linted, 1 chart(s) failed diff --git a/cmd/helm/testdata/output/lint-quiet-with-warning.txt b/cmd/helm/testdata/output/lint-quiet-with-warning.txt new file mode 100644 index 000000000..02c6fa592 --- /dev/null +++ b/cmd/helm/testdata/output/lint-quiet-with-warning.txt @@ -0,0 +1,4 @@ +==> Linting testdata/testcharts/chart-with-only-crds +[WARNING] templates/: directory not found + +1 chart(s) linted, 0 chart(s) failed diff --git a/cmd/helm/testdata/output/lint-quiet.txt b/cmd/helm/testdata/output/lint-quiet.txt new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/action/lint.go b/pkg/action/lint.go index bdb93dcc2..5b566e9d3 100644 --- a/pkg/action/lint.go +++ b/pkg/action/lint.go @@ -36,6 +36,7 @@ type Lint struct { Strict bool Namespace string WithSubcharts bool + Quiet bool } // LintResult is the result of Lint @@ -75,6 +76,16 @@ func (l *Lint) Run(paths []string, vals map[string]interface{}) *LintResult { return result } +// HasWaringsOrErrors checks is LintResult has any warnings or errors +func HasWarningsOrErrors(result *LintResult) bool { + for _, msg := range result.Messages { + if msg.Severity > support.InfoSev { + return true + } + } + return false +} + func lintChart(path string, vals map[string]interface{}, namespace string, strict bool) (support.Linter, error) { var chartPath string linter := support.Linter{}