filter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmbeddedFS *embed.FS

EmbeddedFS is set by the main package to provide embedded filter files. This avoids go:embed constraints on internal packages.

View Source
var GoTestFailureRegex = regexp.MustCompile(`^\s+([a-zA-Z0-9_/.]+\.go):(\d+):`)

Functions

func ValidateFilter

func ValidateFilter(f *Filter) error

ValidateFilter checks required fields and action validity.

Types

type Action

type Action struct {
	ActionName string         `yaml:"action"`
	Params     map[string]any `yaml:",inline"`
}

Action represents a single step in a filter pipeline.

type ActionFunc

type ActionFunc func(ctx context.Context, input ActionResult, params map[string]any) (ActionResult, error)

ActionFunc is the signature for built-in action implementations.

func GetAction

func GetAction(name string) (ActionFunc, bool)

GetAction returns the ActionFunc for the given action name.

type ActionResult

type ActionResult struct {
	Lines    []string
	Metadata map[string]any
	Resolver SourceResolver
}

ActionResult is the data passed between pipeline actions.

type AstGrepFilter

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

AstGrepFilter implements structural filtering using ast-grep (sg).

func (*AstGrepFilter) Apply

func (a *AstGrepFilter) Apply(ctx context.Context, input ActionResult, params map[string]any) (ActionResult, error)

Apply executes ast-grep against the input lines.

type AstGrepMatch

type AstGrepMatch struct {
	Text        string `json:"text"`
	File        string `json:"file"`
	Replacement string `json:"replacement,omitempty"`
	Lines       string `json:"lines"`
	Range       struct {
		Start struct {
			Line   int `json:"line"`
			Column int `json:"column"`
		} `json:"start"`
		End struct {
			Line   int `json:"line"`
			Column int `json:"column"`
		} `json:"end"`
	} `json:"range"`
}

AstGrepMatch represents a single match from ast-grep --json=stream.

type Filter

type Filter struct {
	Name        string   `yaml:"name"`
	Version     int      `yaml:"version"`
	Description string   `yaml:"description"`
	Match       Match    `yaml:"match"`
	Inject      *Inject  `yaml:"inject,omitempty"`
	Pipeline    Pipeline `yaml:"pipeline"`
	OnError     string   `yaml:"on_error"` // "passthrough", "empty", "template"
}

Filter represents a declarative YAML filter for a command.

func LoadAll

func LoadAll(userDir string) ([]Filter, error)

LoadAll loads user filters (priority) and embedded filters, merging by name.

func LoadEmbedded

func LoadEmbedded() ([]Filter, error)

LoadEmbedded loads all embedded YAML filter files.

func LoadUserFilters

func LoadUserFilters(dir string) ([]Filter, error)

LoadUserFilters loads all YAML files from a directory.

func ParseFilter

func ParseFilter(data []byte) (*Filter, error)

ParseFilter parses YAML bytes into a Filter struct.

type Inject

type Inject struct {
	Args          []string          `yaml:"args,omitempty"`
	Defaults      map[string]string `yaml:"defaults,omitempty"`
	SkipIfPresent []string          `yaml:"skip_if_present,omitempty"`
}

Inject defines args to inject before execution.

type Match

type Match struct {
	Command      string   `yaml:"command"`
	Subcommand   string   `yaml:"subcommand,omitempty"`
	ExcludeFlags []string `yaml:"exclude_flags,omitempty"`
	RequireFlags []string `yaml:"require_flags,omitempty"`
}

Match defines which command a filter applies to.

type Pipeline

type Pipeline []Action

Pipeline is an ordered sequence of actions.

type Registry

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

Registry holds loaded filters indexed for fast command matching.

func NewRegistry

func NewRegistry(filters []Filter) *Registry

NewRegistry builds a registry from a list of filters.

func (*Registry) Match

func (r *Registry) Match(command, subcommand string, args []string) *Filter

Match finds the first filter matching the given command, subcommand, and args.

func (*Registry) ShouldInject

func (r *Registry) ShouldInject(f *Filter, args []string) ([]string, bool)

ShouldInject computes final args with injections, respecting skip_if_present.

type SourceResolver

type SourceResolver interface {
	ResolveSource(ctx context.Context, file string, line int) (string, error)
}

SourceResolver defines an interface for actions to retrieve code fragments.

Jump to

Keyboard shortcuts

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