mirror of
https://github.com/helm/helm.git
synced 2026-04-21 22:27:03 -04:00
Merge pull request #10106 from presztak/lint_quiet_flag
feat(*): add --quiet flag to 'helm lint'
This commit is contained in:
commit
2cd709c4a6
6 changed files with 65 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
8
cmd/helm/testdata/output/lint-quiet-with-error.txt
vendored
Normal file
8
cmd/helm/testdata/output/lint-quiet-with-error.txt
vendored
Normal file
|
|
@ -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
cmd/helm/testdata/output/lint-quiet-with-warning.txt
vendored
Normal file
4
cmd/helm/testdata/output/lint-quiet-with-warning.txt
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
==> Linting testdata/testcharts/chart-with-only-crds
|
||||
[WARNING] templates/: directory not found
|
||||
|
||||
1 chart(s) linted, 0 chart(s) failed
|
||||
0
cmd/helm/testdata/output/lint-quiet.txt
vendored
Normal file
0
cmd/helm/testdata/output/lint-quiet.txt
vendored
Normal file
|
|
@ -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{}
|
||||
|
|
|
|||
Loading…
Reference in a new issue