add changes -- prepare for testing

Signed-off-by: Danilo Patrucco <danilo.patrucco@gmail.com>
pull/13205/head
Danilo Patrucco 1 year ago
parent b3b6479e4c
commit 1eaae6de39

@ -31,6 +31,7 @@ import (
"helm.sh/helm/v3/pkg/cli/values" "helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/lint/support" "helm.sh/helm/v3/pkg/lint/support"
"helm.sh/helm/v3/pkg/lint/rules"
) )
var longLintHelp = ` var longLintHelp = `
@ -46,6 +47,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
client := action.NewLint() client := action.NewLint()
valueOpts := &values.Options{} valueOpts := &values.Options{}
var kubeVersion string var kubeVersion string
var lintIgnoreFile string
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "lint PATH", Use: "lint PATH",
@ -86,15 +88,25 @@ func newLintCmd(out io.Writer) *cobra.Command {
return err return err
} }
var ignorePatterns []string
if lintIgnoreFile != "" {
ignorePatterns, err = rules.ParseIgnoreFile(lintIgnoreFile)
if err != nil {
return fmt.Errorf("failed to parse .helmlintignore file: %v", err)
}
}
var message strings.Builder var message strings.Builder
failed := 0 failed := 0
errorsOrWarnings := 0 errorsOrWarnings := 0
for _, path := range paths { for _, path := range paths {
if rules.IsIgnored(path, ignorePatterns) {
continue
}
result := client.Run([]string{path}, vals) 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) hasWarningsOrErrors := action.HasWarningsOrErrors(result)
if hasWarningsOrErrors { if hasWarningsOrErrors {
errorsOrWarnings++ errorsOrWarnings++
@ -105,10 +117,6 @@ func newLintCmd(out io.Writer) *cobra.Command {
fmt.Fprintf(&message, "==> Linting %s\n", path) 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 { 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)
@ -125,9 +133,6 @@ func newLintCmd(out io.Writer) *cobra.Command {
failed++ 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(&message, "\n")
} }
@ -149,7 +154,10 @@ func newLintCmd(out io.Writer) *cobra.Command {
f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts") f.BoolVar(&client.WithSubcharts, "with-subcharts", false, "lint dependent charts")
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") // Add the flag for .helmlintignore file
addValueOptionsFlags(f, valueOpts) addValueOptionsFlags(f, valueOpts)
return cmd return cmd
} }

@ -14,27 +14,29 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package lint // import "helm.sh/helm/v3/pkg/lint" package lint
import ( import (
"path/filepath" "path/filepath"
"os"
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/lint/rules" "helm.sh/helm/v3/pkg/lint/rules"
"helm.sh/helm/v3/pkg/lint/support" "helm.sh/helm/v3/pkg/lint/support"
) )
// All runs all of the available linters on the given base directory.
func All(basedir string, values map[string]interface{}, namespace string, _ bool) support.Linter { func All(basedir string, values map[string]interface{}, namespace string, _ bool) support.Linter {
return AllWithKubeVersion(basedir, values, namespace, nil) return AllWithKubeVersion(basedir, values, namespace, nil, "")
} }
func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion, lintIgnoreFile string) support.Linter {
// AllWithKubeVersion runs all the available linters on the given base directory, allowing to specify the kubernetes version.
func AllWithKubeVersion(basedir string, values map[string]interface{}, namespace string, kubeVersion *chartutil.KubeVersion) support.Linter {
// Using abs path to get directory context
chartDir, _ := filepath.Abs(basedir) chartDir, _ := filepath.Abs(basedir)
var ignorePatterns []string
if lintIgnoreFile != "" {
ignorePatterns, _ = rules.ParseIgnoreFile(lintIgnoreFile) // Simplified error handling for the example
}
linter := support.Linter{ChartDir: chartDir} linter := support.Linter{ChartDir: chartDir}
if rules.IsIgnored(chartDir, ignorePatterns) {
return linter
}
rules.Chartfile(&linter) rules.Chartfile(&linter)
rules.ValuesWithOverrides(&linter, values) rules.ValuesWithOverrides(&linter, values)
rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion) rules.TemplatesWithKubeVersion(&linter, values, namespace, kubeVersion)

@ -0,0 +1,27 @@
package rules
import (
"bufio"
"os"
"strings"
)
// ParseIgnoreFile reads and parses the .helmlintignore file, returning a list of patterns
func ParseIgnoreFile(filePath string) ([]string, error) {
var patterns []string
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" && !strings.HasPrefix(line, "#") { // Ignore comments and empty lines
patterns = append(patterns, line)
}
}
return patterns, scanner.Err()
}
Loading…
Cancel
Save