Skip to content

Commit

Permalink
feat(*): add --quiet flag to 'helm lint'
Browse files Browse the repository at this point in the history
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 <piotr.resztak@gmail.com>
  • Loading branch information
presztak committed Jan 20, 2022
1 parent 86a94f2 commit 383086d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cmd/helm/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -112,14 +126,17 @@ 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
},
}

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
Expand Down
21 changes: 21 additions & 0 deletions cmd/helm/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cmd/helm/testdata/output/lint-quiet-with-error.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions cmd/helm/testdata/output/lint-quiet-with-warning.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
==> Linting testdata/testcharts/chart-with-only-crds
[WARNING] templates/: directory not found

1 chart(s) linted, 0 chart(s) failed
Empty file.
11 changes: 11 additions & 0 deletions pkg/action/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Lint struct {
Strict bool
Namespace string
WithSubcharts bool
Quiet bool
}

// LintResult is the result of Lint
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 383086d

Please sign in to comment.