Documentation
¶
Overview ¶
Package profanity is the rules engine that powers the command `profanity` found in `github.com/blend/go-sdk/cmd/profanity`.
Index ¶
- Constants
- func Glob(subj, pattern string) bool
- func GlobAnyMatch(filters []string, file string) bool
- func ListDir(path string) (dirs []os.FileInfo, files []os.FileInfo, err error)
- type Config
- type ContainsFilter
- type Contents
- type Filter
- func (f Filter) Allow(value string, filter func(string, string) bool) bool
- func (f Filter) AllowMatch(includeMatch, excludeMatch string) bool
- func (f Filter) IsZero() bool
- func (f Filter) Match(value string, filter func(string, string) bool) (includeMatch, excludeMatch string)
- func (f Filter) String() string
- func (f Filter) Validate() error
- type GlobFilter
- type GoCall
- type GoCalls
- type GoImports
- type Logger
- type NoGenericDecls
- type Option
- func OptConfig(cfg Config) Option
- func OptDebug(debug bool) Option
- func OptExcludeDirs(excludeGlobs ...string) Option
- func OptExcludeFiles(excludeGlobs ...string) Option
- func OptExitFirst(exitFirst bool) Option
- func OptIncludeDirs(includeGlobs ...string) Option
- func OptIncludeFiles(includeGlobs ...string) Option
- func OptRoot(root string) Option
- func OptRulesFile(rulesFile string) Option
- func OptVerbose(verbose bool) Option
- type Profanity
- func (p *Profanity) Debugf(format string, args ...interface{})
- func (p *Profanity) Errorf(format string, args ...interface{})
- func (p Profanity) FormatRuleResultFailure(r RuleSpec, rr RuleResult) error
- func (p *Profanity) Printf(format string, args ...interface{})
- func (p *Profanity) Process() error
- func (p *Profanity) ReadRuleSpecsFile(filename string) (rules []RuleSpec, err error)
- func (p *Profanity) ReadRuleSpecsFromReader(filename string, reader io.Reader) (rules []RuleSpec, err error)
- func (p *Profanity) Verbosef(format string, args ...interface{})
- func (p *Profanity) Walk(path string, rules ...RuleSpec) error
- type RegexFilter
- type Rule
- type RuleResult
- type RuleSpec
- type RuleSpecFile
- type RuleSpecRules
Constants ¶
const ( DefaultRoot = "." DefaultRulesFile = ".profanity.yml" )
Defaults
const ( Star = "*" Root = "." GoFiles = "*.go" GoTestFiles = "*_test.go" )
Glob constants
const ( ErrRuleSpecMultipleRules ex.Class = "rule spec invalid; multiple rule types specified" ErrRuleSpecRuleMissing ex.Class = "rule spec invalid; at least one rule type is required" )
Errors
const (
ErrContentsRequired ex.Class = "contents rule spec must provide `contains`, `glob` or `regex` values"
)
Errors
const (
ErrFailure ex.Class = "profanity failure"
)
Errors
Variables ¶
This section is empty.
Functions ¶
func GlobAnyMatch ¶
GlobAnyMatch tests if a file matches a (potentially) csv of glob filters.
Types ¶
type Config ¶
type Config struct {
Root string `yaml:"root"`
ExitFirst *bool `yaml:"failFast"`
RulesFile string `yaml:"rulesFile"`
Rules GlobFilter `yaml:"rules"`
Files GlobFilter `yaml:"files"`
Dirs GlobFilter `yaml:"dirs"`
Verbose *bool `yaml:"verbose"`
Debug *bool `yaml:"debug"`
}
Config is the profanity rules parsing config.
func (Config) DebugOrDefault ¶
DebugOrDefault returns a value or a default.
func (Config) ExitFirstOrDefault ¶ added in v1.20210116.3
ExitFirstOrDefault returns a value or a default.
func (Config) RootOrDefault ¶ added in v1.20210116.3
RootOrDefault returns the starting path or a default.
func (Config) RulesFileOrDefault ¶
RulesFileOrDefault returns the rules file or a default.
func (Config) VerboseOrDefault ¶
VerboseOrDefault returns a value or a default.
type ContainsFilter ¶
type ContainsFilter struct {
Filter `yaml:",inline"`
}
ContainsFilter is the contains filter.
func (ContainsFilter) Allow ¶
func (c ContainsFilter) Allow(value string) bool
Allow returns if apply returns a result.
func (ContainsFilter) Match ¶
func (c ContainsFilter) Match(value string) (includeMatch, excludeMatch string)
Match applies the filter.
type Contents ¶
type Contents struct {
// Contains is a filter set that uses `strings.Contains` as the predicate.
Contains *ContainsFilter `yaml:"contains,omitempty"`
// Glob is a filter set that uses `Glob` as the predicate.
Glob *GlobFilter `yaml:"glob,omitempty"`
// Regex is a filter set that uses `regexp.MustMatch` as the predicate
Regex *RegexFilter `yaml:"regex,omitempty"`
}
Contents creates a new contents rule. It failes if any of the expressions match.
type Filter ¶
type Filter struct {
// Include sets a glob filter for file inclusion by name.
Include []string `yaml:"include,omitempty"`
// ExcludeGlob sets a glob filter for file exclusion by name.
Exclude []string `yaml:"exclude,omitempty"`
}
Filter is the base rule helper.
func (Filter) AllowMatch ¶
AllowMatch returns if the filter should allow a given match set.
type GlobFilter ¶
type GlobFilter struct {
Filter `yaml:",inline"`
}
GlobFilter rules for if we should include or exclude file or directory by name.
func (GlobFilter) Allow ¶
func (gf GlobFilter) Allow(value string) bool
Allow returns if the filters include or exclude a given value.
func (GlobFilter) Match ¶
func (gf GlobFilter) Match(value string) (includeMatch, excludeMatch string)
Match returns the matching glob filter for a given value.
type GoCall ¶
GoCall is a package and function name pair.
`Package` is the package selector, typically the last path segment of the import (ex. "github.com/foo/bar" would be "bar")
`Func` is the function name.
If package is empty string, it is assumed that the function is local to the calling package or a builtin.
type GoCalls ¶
type GoCalls []GoCall
GoCalls returns a profanity error if a given function is called in a package.
type GoImports ¶
type GoImports struct {
GlobFilter `yaml:",inline"`
}
GoImports returns a profanity error if a given file contains any of a list of imports based on a glob match.
type NoGenericDecls ¶ added in v1.20240719.1
type NoGenericDecls struct {
Enabled bool `yaml:"enabled"`
}
NoGenericDecls returns a profanity error if a generic function or type declaration exists
func (NoGenericDecls) Check ¶ added in v1.20240719.1
func (ngd NoGenericDecls) Check(filename string, contents []byte) RuleResult
Check implements Rule.
func (NoGenericDecls) String ¶ added in v1.20240719.1
func (ngd NoGenericDecls) String() string
Strings implements fmt.Stringer.
func (NoGenericDecls) Validate ¶ added in v1.20240719.1
func (ngd NoGenericDecls) Validate() error
Validate implements validation for the rule.
type Option ¶ added in v1.20210116.3
type Option func(*Profanity)
Option is a function that modifies a config.
func OptExcludeDirs ¶ added in v1.20210116.3
OptExcludeDirs sets the exclude glob filter for directories.
func OptExcludeFiles ¶ added in v1.20210116.3
OptExcludeFiles sets the exclude glob filter for files.
func OptExitFirst ¶ added in v1.20210116.3
OptExitFirst sets if we should stop after the first failure.
func OptIncludeDirs ¶ added in v1.20210116.3
OptIncludeDirs sets the include glob filter for files.
func OptIncludeFiles ¶ added in v1.20210116.3
OptIncludeFiles sets the include glob filter for files.
func OptRulesFile ¶
OptRulesFile sets the rules file to check for in each directory.
func OptVerbose ¶
OptVerbose sets if we should show verbose output.
type Profanity ¶
Profanity parses rules from the filesystem and applies them to a given root path. Creating a full rules set.
func (Profanity) FormatRuleResultFailure ¶
func (p Profanity) FormatRuleResultFailure(r RuleSpec, rr RuleResult) error
FormatRuleResultFailure formats a rule result with the rule that produced it.
func (*Profanity) ReadRuleSpecsFile ¶
ReadRuleSpecsFile reads rules from a file path.
It is expected to be passed the fully qualified path for the rules file.
func (*Profanity) ReadRuleSpecsFromReader ¶
func (p *Profanity) ReadRuleSpecsFromReader(filename string, reader io.Reader) (rules []RuleSpec, err error)
ReadRuleSpecsFromReader reads rules from a reader.
type RegexFilter ¶
type RegexFilter struct {
Filter `yaml:",inline"`
// contains filtered or unexported fields
}
RegexFilter represents rules around matching (or excluding) based on regular expressions.
func (*RegexFilter) Allow ¶
func (rf *RegexFilter) Allow(value string) (result bool)
Allow returns if the filters include or exclude a given filename.
func (*RegexFilter) Match ¶
func (rf *RegexFilter) Match(value string) (includeMatch, excludeMatch string)
Match returns the matching glob filter for a given value.
func (*RegexFilter) MustMatch ¶
func (rf *RegexFilter) MustMatch(value, expr string) bool
MustMatch regexp but panics
type Rule ¶
type Rule interface {
Check(file string, contents []byte) RuleResult
}
Rule is a criteria for profanity.
type RuleResult ¶
RuleResult is a result from a rule.
type RuleSpec ¶
type RuleSpec struct {
// ID is a unique identifier for the rule.
ID string `yaml:"id"`
// SourceFile is the rules file path the rule came from.
SourceFile string `yaml:"-"`
// Description is a descriptive message for the rule.
Description string `yaml:"description,omitempty"`
// Files is the glob filter for inclusion and exclusion'
// for this specific rule spec.
Files GlobFilter `yaml:"files,omitempty"`
// RuleSpecRules are the rules for the rule spec.
RuleSpecRules `yaml:",inline"`
}
RuleSpec is a serialized rule.
type RuleSpecFile ¶
RuleSpecFile is a map of string rule id to rule item.
It is the format for profanity rule files.
func (RuleSpecFile) String ¶
func (rsf RuleSpecFile) String() string
String implements fmt.Stringer.
type RuleSpecRules ¶
type RuleSpecRules struct {
// Contains implies we should fail if a file contains a given string.
Contents *Contents `yaml:"contents,omitempty"`
// GoImportsContain enforces that a given list of imports are used.
GoImports *GoImports `yaml:"goImports,omitempty"`
// GoCalls enforces that a given list of imports are used.
GoCalls *GoCalls `yaml:"goCalls,omitempty"`
// NoGenericDecls enforces that no generic declarations exist.
NoGenericDecls *NoGenericDecls `yaml:"noGenericDecls,omitempty"`
}
RuleSpecRules are the specific rules for a given RuleSpec.
The usage of this should be that only _one_ of these rules should be set for a given rule spec.
func (RuleSpecRules) Check ¶
func (r RuleSpecRules) Check(filename string, contents []byte) (result RuleResult)
Check applies the rule.
func (RuleSpecRules) Rule ¶
func (r RuleSpecRules) Rule() Rule
Rule returns the active rule from the spec.
func (RuleSpecRules) Rules ¶
func (r RuleSpecRules) Rules() (output []Rule)
Rules returns the rules from the spec.
Note: you should add new rule types here and on the type itself.
func (RuleSpecRules) String ¶
func (r RuleSpecRules) String() string
String implements fmt.Stringer.
func (RuleSpecRules) Validate ¶
func (r RuleSpecRules) Validate() error
Validate validates the rule spec rules.