github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/rule/file_contents.go (about)

     1  package rule
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"regexp"
     7  )
     8  
     9  // FileContentMatches is a rule that verifies that the contents of a file
    10  // match the regular expression provided
    11  type FileContentMatches struct {
    12  	Meta
    13  	File         string
    14  	ContentRegex string
    15  }
    16  
    17  // Name is the name of the rule
    18  func (f FileContentMatches) Name() string {
    19  	return fmt.Sprintf("Contents of %q match: %s", f.File, f.ContentRegex)
    20  }
    21  
    22  // IsRemoteRule returns true if the rule is to be run from outside of the node
    23  func (f FileContentMatches) IsRemoteRule() bool { return false }
    24  
    25  // Validate the rule
    26  func (f FileContentMatches) Validate() []error {
    27  	errs := []error{}
    28  	if f.File == "" {
    29  		errs = append(errs, errors.New("File cannot be empty"))
    30  	}
    31  	if f.ContentRegex == "" {
    32  		errs = append(errs, errors.New("ContentRegex cannot be empty"))
    33  	}
    34  	if f.ContentRegex != "" {
    35  		if _, err := regexp.Compile(f.ContentRegex); err != nil {
    36  			errs = append(errs, fmt.Errorf("ContentRegex contains an invalid regular expression: %v", err))
    37  		}
    38  	}
    39  	if len(errs) > 0 {
    40  		return errs
    41  	}
    42  	return nil
    43  }