tomlfilter

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tomlfilter provides TOML-based declarative filtering for command output. This enables users to define filters without writing Go code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadFileLines

func ReadFileLines(path string) ([]string, error)

ReadFileLines reads a file and returns its lines.

func ValidateFilter

func ValidateFilter(name string, filter TomlFilter) error

ValidateFilter validates a filter definition without adding it to the registry.

Types

type FilterTest

type FilterTest struct {
	Name     string `toml:"name"`
	Input    string `toml:"input"`
	Expected string `toml:"expected"`
}

FilterTest represents an inline test case in a TOML filter.

type MatchRule

type MatchRule struct {
	Pattern string `toml:"pattern"`
	Message string `toml:"message"`
}

MatchRule defines a short-circuit rule based on output matching.

type Registry

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

Registry holds loaded TOML filters and provides matching functionality.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty filter registry.

func (*Registry) AllFilters

func (r *Registry) AllFilters() []*TomlFilter

AllFilters returns all loaded filters.

func (*Registry) ApplyToCommand

func (r *Registry) ApplyToCommand(command, input string) (string, bool)

ApplyToCommand finds a matching filter and applies it to the input. Returns the filtered output and true if a filter was applied, or the original input and false if no filter matched.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of loaded filters.

func (*Registry) GetFilter

func (r *Registry) GetFilter(name string) *TomlFilter

GetFilter returns a filter by name, or nil if not found.

func (*Registry) LoadFilters

func (r *Registry) LoadFilters(directory string) error

LoadFilters loads all TOML filter files from the given directory.

func (*Registry) LoadFromEmbedded

func (r *Registry) LoadFromEmbedded(content string) error

LoadFromEmbedded loads filters from embedded TOML content (for built-in filters).

func (*Registry) MatchFilter

func (r *Registry) MatchFilter(command string) *TomlFilter

MatchFilter finds a filter that matches the given command. Returns nil if no filter matches.

type ReplaceRule

type ReplaceRule struct {
	Pattern     string `toml:"pattern"`
	Replacement string `toml:"replacement"`
}

ReplaceRule defines a regex substitution rule.

type TomlFilter

type TomlFilter struct {
	// Name is the filter identifier (from TOML table name)
	Name string `toml:"-"`

	// Description is a human-readable description of the filter
	Description string `toml:"description"`

	// MatchCommand is a regex pattern to match against the full command string
	MatchCommand string `toml:"match_command"`

	// StripANSI removes ANSI escape codes before processing
	StripANSI bool `toml:"strip_ansi"`

	// StripLinesMatching drops lines matching any of these regex patterns
	StripLinesMatching []string `toml:"strip_lines_matching"`

	// KeepLinesMatching keeps only lines matching at least one of these regex patterns
	KeepLinesMatching []string `toml:"keep_lines_matching"`

	// Replace applies regex substitutions
	Replace []ReplaceRule `toml:"replace"`

	// MatchOutput applies short-circuit rules based on output content
	MatchOutput []MatchRule `toml:"match_output"`

	// TruncateLinesAt truncates lines longer than N characters
	TruncateLinesAt int `toml:"truncate_lines_at"`

	// MaxLines keeps only the first N lines after filtering
	MaxLines int `toml:"max_lines"`

	// TailLines keeps only the last N lines (applied after other filters)
	TailLines int `toml:"tail_lines"`

	// OnEmpty is the fallback message when filtered output is empty
	OnEmpty string `toml:"on_empty"`
	// contains filtered or unexported fields
}

TomlFilter represents a single filter definition from a TOML file.

func (*TomlFilter) Apply

func (f *TomlFilter) Apply(input string) string

Apply applies the filter to the input and returns the filtered output.

func (*TomlFilter) Matches

func (f *TomlFilter) Matches(command string) bool

Matches checks if this filter should be applied to the given command.

type TomlFilterFile

type TomlFilterFile struct {
	Filters map[string]TomlFilter   `toml:"filters"`
	Tests   map[string][]FilterTest `toml:"tests"`
}

TomlFilterFile represents the structure of a TOML filter file.

Jump to

Keyboard shortcuts

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