github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/lint/support/message.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 support
    18  
    19  import "fmt"
    20  
    21  // Severity indicatest the severity of a Message.
    22  const (
    23  	// UnknownSev indicates that the severity of the error is unknown, and should not stop processing.
    24  	UnknownSev = iota
    25  	// InfoSev indicates information, for example missing values.yaml file
    26  	InfoSev
    27  	// WarningSev indicates that something does not meet code standards, but will likely function.
    28  	WarningSev
    29  	// ErrorSev indicates that something will not likely function.
    30  	ErrorSev
    31  )
    32  
    33  // sev matches the *Sev states.
    34  var sev = []string{"UNKNOWN", "INFO", "WARNING", "ERROR"}
    35  
    36  // Linter encapsulates a linting run of a particular chart.
    37  type Linter struct {
    38  	Messages []Message
    39  	// The highest severity of all the failing lint rules
    40  	HighestSeverity int
    41  	ChartDir        string
    42  }
    43  
    44  // Message describes an error encountered while linting.
    45  type Message struct {
    46  	// Severity is one of the *Sev constants
    47  	Severity int
    48  	Path     string
    49  	Err      error
    50  }
    51  
    52  func (m Message) Error() string {
    53  	return fmt.Sprintf("[%s] %s: %s", sev[m.Severity], m.Path, m.Err.Error())
    54  }
    55  
    56  // NewMessage creates a new Message struct
    57  func NewMessage(severity int, path string, err error) Message {
    58  	return Message{Severity: severity, Path: path, Err: err}
    59  }
    60  
    61  // RunLinterRule returns true if the validation passed
    62  func (l *Linter) RunLinterRule(severity int, path string, err error) bool {
    63  	// severity is out of bound
    64  	if severity < 0 || severity >= len(sev) {
    65  		return false
    66  	}
    67  
    68  	if err != nil {
    69  		l.Messages = append(l.Messages, NewMessage(severity, path, err))
    70  
    71  		if severity > l.HighestSeverity {
    72  			l.HighestSeverity = severity
    73  		}
    74  	}
    75  	return err == nil
    76  }