github.com/autonomy/conform@v0.1.0-alpha.16/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
     6  
     7  // Report summarizes the compliance of a policy.
     8  type Report struct {
     9  	checks []Check
    10  }
    11  
    12  // Check defines a policy check.
    13  type Check interface {
    14  	Name() string
    15  	Message() string
    16  	Errors() []error
    17  }
    18  
    19  // Policy is an interface that policies must implement.
    20  type Policy interface {
    21  	Compliance(*Options) (*Report, error)
    22  }
    23  
    24  // Valid checks if a report is valid.
    25  func (r *Report) Valid() bool {
    26  	for _, check := range r.checks {
    27  		if len(check.Errors()) != 0 {
    28  			return false
    29  		}
    30  	}
    31  	return true
    32  }
    33  
    34  // Checks returns the checks executed by a policy.
    35  func (r *Report) Checks() []Check {
    36  	return r.checks
    37  }
    38  
    39  // AddCheck adds a check to the policy report.
    40  func (r *Report) AddCheck(c Check) {
    41  	r.checks = append(r.checks, c)
    42  }