profanity

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package profanity is the rules engine that powers the command `profanity` found in `github.com/blend/go-sdk/cmd/profanity`.

Index

Constants

View Source
const (
	DefaultRoot      = "."
	DefaultRulesFile = ".profanity.yml"
)

Defaults

View Source
const (
	Star = "*"
	Root = "."

	GoFiles     = "*.go"
	GoTestFiles = "*_test.go"
)

Glob constants

View Source
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

View Source
const (
	ErrContentsRequired ex.Class = "contents rule spec must provide `contains`, `glob` or `regex` values"
)

Errors

View Source
const (
	ErrFailure ex.Class = "profanity failure"
)

Errors

Variables

This section is empty.

Functions

func Glob

func Glob(subj, pattern string) bool

Glob returns if a given pattern matches a given subject.

func GlobAnyMatch

func GlobAnyMatch(filters []string, file string) bool

GlobAnyMatch tests if a file matches a (potentially) csv of glob filters.

func ListDir

func ListDir(path string) (dirs []os.FileInfo, files []os.FileInfo, err error)

ListDir reads the directory named by dirname and returns a sorted list of directory entries.

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

func (c Config) DebugOrDefault() bool

DebugOrDefault returns a value or a default.

func (Config) ExitFirstOrDefault added in v1.20210116.3

func (c Config) ExitFirstOrDefault() bool

ExitFirstOrDefault returns a value or a default.

func (Config) RootOrDefault added in v1.20210116.3

func (c Config) RootOrDefault() string

RootOrDefault returns the starting path or a default.

func (Config) RulesFileOrDefault

func (c Config) RulesFileOrDefault() string

RulesFileOrDefault returns the rules file or a default.

func (Config) VerboseOrDefault

func (c Config) VerboseOrDefault() bool

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.

func (Contents) Check

func (cm Contents) Check(filename string, contents []byte) (result RuleResult)

Check implements Rule.

func (Contents) String

func (cm Contents) String() string

String implements fmt.Stringer.

func (Contents) Validate

func (cm Contents) Validate() error

Validate returns validators.

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) Allow

func (f Filter) Allow(value string, filter func(string, string) bool) bool

Allow returns if the filters include or exclude a given value.

func (Filter) AllowMatch

func (f Filter) AllowMatch(includeMatch, excludeMatch string) bool

AllowMatch returns if the filter should allow a given match set.

func (Filter) IsZero

func (f Filter) IsZero() bool

IsZero returns if the filter is set or not.

func (Filter) Match

func (f Filter) Match(value string, filter func(string, string) bool) (includeMatch, excludeMatch string)

Match returns the matching glob filter for a given value.

func (Filter) String

func (f Filter) String() string

String implements fmt.Stringer.

func (Filter) Validate

func (f Filter) Validate() error

Validate doesn't do anything right now for Filter.

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

type GoCall struct {
	Package string `yaml:"package"`
	Func    string `yaml:"func"`
}

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.

func (GoCall) String

func (gc GoCall) String() string

String implements fmt.Stringer

type GoCalls

type GoCalls []GoCall

GoCalls returns a profanity error if a given function is called in a package.

func (GoCalls) Check

func (gc GoCalls) Check(filename string, contents []byte) RuleResult

Check implements Rule.

func (GoCalls) String

func (gc GoCalls) String() string

Strings implements fmt.Stringer.

func (GoCalls) Validate

func (gc GoCalls) Validate() error

Validate implements validation for the rule.

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.

func (GoImports) Check

func (gi GoImports) Check(filename string, contents []byte) RuleResult

Check implements Rule.

func (GoImports) String

func (gi GoImports) String() string

String implements fmt.Stringer.

type Logger

type Logger interface {
	Printf(string, ...interface{})
	Errorf(string, ...interface{})
}

Logger are the methods required on the logger.

type Option added in v1.20210116.3

type Option func(*Profanity)

Option is a function that modifies a config.

func OptConfig

func OptConfig(cfg Config) Option

OptConfig sets the config in its entirety.

func OptDebug

func OptDebug(debug bool) Option

OptDebug sets if we should show debug output.

func OptExcludeDirs added in v1.20210116.3

func OptExcludeDirs(excludeGlobs ...string) Option

OptExcludeDirs sets the exclude glob filter for directories.

func OptExcludeFiles added in v1.20210116.3

func OptExcludeFiles(excludeGlobs ...string) Option

OptExcludeFiles sets the exclude glob filter for files.

func OptExitFirst added in v1.20210116.3

func OptExitFirst(exitFirst bool) Option

OptExitFirst sets if we should stop after the first failure.

func OptIncludeDirs added in v1.20210116.3

func OptIncludeDirs(includeGlobs ...string) Option

OptIncludeDirs sets the include glob filter for files.

func OptIncludeFiles added in v1.20210116.3

func OptIncludeFiles(includeGlobs ...string) Option

OptIncludeFiles sets the include glob filter for files.

func OptRoot added in v1.20210116.3

func OptRoot(root string) Option

OptRoot sets the root directory to start the profanity check.

func OptRulesFile

func OptRulesFile(rulesFile string) Option

OptRulesFile sets the rules file to check for in each directory.

func OptVerbose

func OptVerbose(verbose bool) Option

OptVerbose sets if we should show verbose output.

type Profanity

type Profanity struct {
	Config Config
	Stdout io.Writer
	Stderr io.Writer
}

Profanity parses rules from the filesystem and applies them to a given root path. Creating a full rules set.

func New

func New(options ...Option) *Profanity

New creates a new profanity engine with a given set of config options.

func (*Profanity) Debugf

func (p *Profanity) Debugf(format string, args ...interface{})

Debugf prints a debug message.

func (*Profanity) Errorf

func (p *Profanity) Errorf(format string, args ...interface{})

Errorf writes to the error output stream.

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) Printf

func (p *Profanity) Printf(format string, args ...interface{})

Printf writes to the output stream.

func (*Profanity) Process

func (p *Profanity) Process() error

Process processes the profanity rules.

func (*Profanity) ReadRuleSpecsFile

func (p *Profanity) ReadRuleSpecsFile(filename string) (rules []RuleSpec, err error)

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.

func (*Profanity) Verbosef

func (p *Profanity) Verbosef(format string, args ...interface{})

Verbosef prints a verbose message.

func (*Profanity) Walk

func (p *Profanity) Walk(path string, rules ...RuleSpec) error

Walk walks a given path, inheriting a set of rules.

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

type RuleResult struct {
	OK      bool
	File    string
	Line    int
	Message string
	Err     error
}

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.

func (RuleSpec) String

func (r RuleSpec) String() string

String returns a string representation of the rule.

func (RuleSpec) Validate

func (r RuleSpec) Validate() error

Validate validates the RuleSpec.

type RuleSpecFile

type RuleSpecFile map[string]RuleSpec

RuleSpecFile is a map of string rule id to rule item.

It is the format for profanity rule files.

func (RuleSpecFile) Rules

func (rsf RuleSpecFile) Rules() []RuleSpec

Rules returns the

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"`
}

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.

Jump to

Keyboard shortcuts

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