rules

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package rules applies rules and combines the results.

Index

Constants

This section is empty.

Variables

View Source
var ValidateName = regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]*[a-z0-9]$`).MatchString

ValidateName checks if a rule or ruleset name is valid.

Functions

This section is empty.

Types

type Analyzer

type Analyzer struct {
	// contains filtered or unexported fields
}

func NewAnalyzer

func NewAnalyzer(rulesets []RulesetSpec, cnf *AnalyzerConfig) (*Analyzer, error)

func (*Analyzer) Analyze

func (a *Analyzer) Analyze(ctx context.Context, fsys fs.FS, root string) ([]Report, error)

type AnalyzerConfig

type AnalyzerConfig struct {
	CELEnvOptions      []cel.EnvOption // Optional custom CEL environment options, replacing the default.
	CELExpressionCache eval.Cache      // Optional expression cache: ideally it should cover the expected expressions.

	// DisableGitIgnore disables handling of .gitignore and .git/info/exclude files.
	//
	// The IgnoreDirs setting will still be respected, and certain directories will
	// always be ignored (namely .git and node_modules). Rules that implement the
	// Ignorer interface will also still be respected.
	DisableGitIgnore bool

	IgnoreDirs []string // Additional directory ignore rules, using git's exclude syntax.

	DisableMetadata bool // Skip calculating or reporting rule metadata.
}

type Ignorer

type Ignorer interface {
	GetIgnores() []string
}

type Match

type Match struct {
	Result string
	Maybe  bool
	Err    error
	Rules  []RuleSpec
}

func FindMatches

func FindMatches(rules []RuleSpec, eval func(RuleSpec) (bool, error)) ([]Match, error)

FindMatches will evaluate a list of rules and return a list of Match results.

type Report

type Report struct {
	Ruleset string `json:"ruleset,omitempty"`
	Path    string `json:"path,omitempty"`

	Result string   `json:"result,omitempty"`
	Error  string   `json:"error,omitempty"`
	Rules  []string `json:"rules,omitempty"`

	Maybe bool `json:"maybe,omitempty"`

	Groups    []string               `json:"groups,omitempty"`
	ReadFiles []string               `json:"read_files,omitempty"`
	With      map[string]ReportValue `json:"with,omitempty"`
}

Report contains results and other metadata after applying rules.

type ReportValue

type ReportValue struct {
	Value any    `json:"value,omitempty"`
	Error string `json:"error,omitempty"`
}

ReportValue contains a reported value or an error message.

type Rule

type Rule struct {
	Name string `yaml:"name,omitempty"`

	When  string           `yaml:"when"`
	Then  YAMLListOrString `yaml:"then"`
	Maybe YAMLListOrString `yaml:"maybe"`

	With map[string]string `yaml:"with"`

	Group     string           `yaml:"group"`
	GroupList YAMLListOrString `yaml:"groups"`

	Ignore YAMLListOrString `yaml:"ignore"`

	ReadFiles []string `yaml:"read_files"`
}

Rule is the default implementation of a rule (see RuleSpec).

func (*Rule) GetCondition

func (r *Rule) GetCondition() string

func (*Rule) GetGroups

func (r *Rule) GetGroups() []string

func (*Rule) GetIgnores

func (r *Rule) GetIgnores() []string

func (*Rule) GetMaybeResults

func (r *Rule) GetMaybeResults() []string

func (*Rule) GetMetadata

func (r *Rule) GetMetadata() map[string]string

func (*Rule) GetName

func (r *Rule) GetName() string

func (*Rule) GetReadFiles

func (r *Rule) GetReadFiles() []string

func (*Rule) GetResults

func (r *Rule) GetResults() []string

type RuleSpec

type RuleSpec interface {
	GetName() string
	GetCondition() string
	GetResults() []string
}

RuleSpec defines a rule.

type Ruleset

type Ruleset struct {
	Name  string     `yaml:"name,omitempty"`
	Rules []RuleSpec `yaml:"rules"`
}

Ruleset is the default implementation of a ruleset (see RulesetSpec).

func (*Ruleset) GetName

func (r *Ruleset) GetName() string

func (*Ruleset) GetRules

func (r *Ruleset) GetRules() []RuleSpec

type RulesetSpec

type RulesetSpec interface {
	GetName() string
	GetRules() []RuleSpec
}

RulesetSpec defines a ruleset.

func LoadFromYAMLDir

func LoadFromYAMLDir(fsys fs.FS, path string) ([]RulesetSpec, error)

LoadFromYAMLDir loads all YAML files in a directory and parses rulesets from them.

type WithGroups

type WithGroups interface {
	GetGroups() []string
}

WithGroups adds to a RuleSpec the feature of a rule having groups.

type WithMaybeResults

type WithMaybeResults interface {
	GetMaybeResults() []string
}

WithMaybeResults adds to a RuleSpec the possibility of a rule having uncertain results.

type WithMetadata

type WithMetadata interface {
	GetMetadata() map[string]string
}

WithMetadata adds to a RuleSpec the feature of a rule having metadata.

type WithReadFiles

type WithReadFiles interface {
	GetReadFiles() []string
}

type YAMLListOrString

type YAMLListOrString []string

func (*YAMLListOrString) UnmarshalYAML

func (l *YAMLListOrString) UnmarshalYAML(unmarshal func(interface{}) error) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL