github.com/twelho/conform@v0.0.0-20231016230407-c25e9238598a/internal/policy/policy.go (about)

     1  // This Source Code Form is subject to the terms of the Mozilla Public
     2  // License, v. 2.0. If a copy of the MPL was not distributed with this
     3  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  
     5  // Package policy provides base policy definitions.
     6  package policy
     7  
     8  // Report reports the compliance of a policy.
     9  type Report struct {
    10  	checks []Check
    11  }
    12  
    13  // Check defines a policy check.
    14  type Check interface {
    15  	Name() string
    16  	Message() string
    17  	Errors() []error
    18  }
    19  
    20  // Policy is an interface that policies must implement.
    21  type Policy interface {
    22  	Compliance(*Options) (*Report, error)
    23  }
    24  
    25  // Valid checks if a report is valid.
    26  func (r *Report) Valid() bool {
    27  	for _, check := range r.checks {
    28  		if len(check.Errors()) != 0 {
    29  			return false
    30  		}
    31  	}
    32  
    33  	return true
    34  }
    35  
    36  // Checks returns the checks executed by a policy.
    37  func (r *Report) Checks() []Check {
    38  	return r.checks
    39  }
    40  
    41  // AddCheck adds a check to the policy report.
    42  func (r *Report) AddCheck(c Check) {
    43  	r.checks = append(r.checks, c)
    44  }