github.com/zsuzhengdu/helm@v3.0.0-beta.3+incompatible/cmd/helm/lint.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/spf13/cobra"
    26  
    27  	"helm.sh/helm/pkg/action"
    28  	"helm.sh/helm/pkg/cli/values"
    29  	"helm.sh/helm/pkg/getter"
    30  )
    31  
    32  var longLintHelp = `
    33  This command takes a path to a chart and runs a series of tests to verify that
    34  the chart is well-formed.
    35  
    36  If the linter encounters things that will cause the chart to fail installation,
    37  it will emit [ERROR] messages. If it encounters issues that break with convention
    38  or recommendation, it will emit [WARNING] messages.
    39  `
    40  
    41  func newLintCmd(out io.Writer) *cobra.Command {
    42  	client := action.NewLint()
    43  	valueOpts := &values.Options{}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:   "lint PATH",
    47  		Short: "examines a chart for possible issues",
    48  		Long:  longLintHelp,
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			paths := []string{"."}
    51  			if len(args) > 0 {
    52  				paths = args
    53  			}
    54  			client.Namespace = getNamespace()
    55  			vals, err := valueOpts.MergeValues(getter.All(settings))
    56  			if err != nil {
    57  				return err
    58  			}
    59  
    60  			var message strings.Builder
    61  			failed := 0
    62  
    63  			for _, path := range paths {
    64  				fmt.Fprintf(&message, "==> Linting %s\n", path)
    65  
    66  				result := client.Run([]string{path}, vals)
    67  
    68  				// All the Errors that are generated by a chart
    69  				// that failed a lint will be included in the
    70  				// results.Messages so we only need to print
    71  				// the Errors if there are no Messages.
    72  				if len(result.Messages) == 0 {
    73  					for _, err := range result.Errors {
    74  						fmt.Fprintf(&message, "Error %s\n", err)
    75  					}
    76  				}
    77  
    78  				for _, msg := range result.Messages {
    79  					fmt.Fprintf(&message, "%s\n", msg)
    80  				}
    81  
    82  				if len(result.Errors) != 0 {
    83  					failed++
    84  				}
    85  
    86  				// Adding extra new line here to break up the
    87  				// results, stops this from being a big wall of
    88  				// text and makes it easier to follow.
    89  				fmt.Fprint(&message, "\n")
    90  			}
    91  
    92  			fmt.Fprintf(out, message.String())
    93  
    94  			var summary strings.Builder
    95  			fmt.Fprintf(&summary, "%d chart(s) linted, %d chart(s) failed", len(paths), failed)
    96  			if failed > 0 {
    97  				return errors.New(summary.String())
    98  			}
    99  			fmt.Fprintf(out, "%s\n", summary.String())
   100  			return nil
   101  		},
   102  	}
   103  
   104  	f := cmd.Flags()
   105  	f.BoolVar(&client.Strict, "strict", false, "fail on lint warnings")
   106  	addValueOptionsFlags(f, valueOpts)
   107  
   108  	return cmd
   109  }