dsl

package
v0.7.12 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dsl is a declarative, user-extensible text-filter engine, adapted from rtk's TOML filter DSL (design D11). It lets command/log/tool-output shrinking be authored in YAML — no recompile — with the same 8-stage pipeline and Lossiness typing rtk proved out.

Adaptation for the proxy world: rtk matches a shell command string; we match a per-message "selector" (the tool name, or the first line of content) since a proxy sees tool OUTPUTS, not the command that produced them. The content stages are unchanged.

Pipeline order (each stage optional, applied in this exact order):

  1. strip_ansi 2. replace[] 3. match_output[]+unless
  2. strip/keep lines 5. truncate_lines_at 6. head/tail
  3. max_lines 8. on_empty

Filters are lossy (they drop lines), so the cmdfilter component that wraps this engine is an Offload: it stashes the original before applying a filter.

Index

Constants

This section is empty.

Variables

View Source
var Caps = map[string]int{
	"errors":    20,
	"warnings":  10,
	"list":      20,
	"inventory": 50,
	"buildlog":  80,
}

Caps are the shared line budgets a filter picks by SIGNAL DENSITY (`cap: errors`) instead of hand-picking a max_lines per filter. The first four names and values are rtk's (src/core/truncate.rs); `buildlog` is ours — a build/plan transcript is mostly noise but the line that matters can sit anywhere in it, so it gets a deliberately generous budget. One map tunes the whole filter set.

Functions

func ReducedCap added in v0.1.1

func ReducedCap(cap, by int) int

ReducedCap is rtk's `reduced` deviation helper: a cap lowered for a more verbose data class, underflow-safe — a deviation can never empty the budget.

func RunTests

func RunTests(b []byte) (failures []string, err error)

RunTests compiles and runs the inline [tests] in a filter document, returning the names of failing cases (empty = all pass). Powers a `verify` command.

func TruncateRunes added in v0.1.1

func TruncateRunes(s string, n int) string

TruncateRunes caps a line at n runes, marking the cut with an ellipsis that fits INSIDE the budget (so the result is never longer than n). Ported from rtk's utils::truncate.

Exported because the generic per-line cap in the `linecap` component applies the same rule outside this package, and a second copy of "the ellipsis has to fit inside the budget" is the kind of subtlety that drifts.

Types

type Compiled

type Compiled struct {
	Name string
	// contains filtered or unexported fields
}

Compiled is a filter with its regexes prebuilt.

func Compile

func Compile(name string, d Def) (*Compiled, error)

Compile validates and precompiles a filter definition.

func (*Compiled) Family added in v0.1.1

func (c *Compiled) Family() string

Family is the filter's command family, used for per-family metrics. "" when unset.

type Def

type Def struct {
	Description string `yaml:"description"`
	Match       string `yaml:"match"` // regex against the selector key
	// Family groups filters for per-family metrics (builds, tests, iac, pkg, net, ...).
	Family string `yaml:"family"`
	// Priority orders matching: higher first, then by name. Absent (0) = today's
	// behavior (name order). Use it to put a specific filter ahead of a generic one,
	// which matters more here than in rtk because we match on output shape.
	Priority int `yaml:"priority"`
	// Cap selects a shared budget class from Caps instead of a literal MaxLines;
	// CapReduce lowers it for an extra-verbose variant. MaxLines wins if both are set.
	Cap                string        `yaml:"cap"`
	CapReduce          int           `yaml:"cap_reduce"`
	StripANSI          bool          `yaml:"strip_ansi"`
	Replace            []ReplaceRule `yaml:"replace"`
	MatchOutput        []MatchRule   `yaml:"match_output"`
	StripLinesMatching []string      `yaml:"strip_lines_matching"`
	KeepLinesMatching  []string      `yaml:"keep_lines_matching"`
	TruncateLinesAt    *int          `yaml:"truncate_lines_at"`
	HeadLines          *int          `yaml:"head_lines"`
	TailLines          *int          `yaml:"tail_lines"`
	MaxLines           *int          `yaml:"max_lines"`
	OnEmpty            *string       `yaml:"on_empty"`
}

Def is a raw filter definition (from YAML). All fields except Match are optional.

type File

type File struct {
	SchemaVersion int                   `yaml:"schema_version"`
	Filters       map[string]Def        `yaml:"filters"`
	Tests         map[string][]TestCase `yaml:"tests"`
}

File is a filter document: a schema version + named filters + inline tests.

type Lossiness

type Lossiness int

Lossiness records what a filter did to its input, so the caller can emit an accurate recovery hint (after rtk's Lossiness enum).

const (
	LossNone  Lossiness = iota // nothing dropped, or only reversible reformatting
	LossTail                   // a clean contiguous tail was dropped (tail -n +N recovers)
	LossWhole                  // non-contiguous / whole-blob loss; only full retrieval recovers
)

func Apply

func Apply(c *Compiled, input string) (string, Lossiness)

Apply runs the 8-stage pipeline over input, returning the filtered output and what kind of loss occurred.

type MatchRule

type MatchRule struct {
	Pattern string `yaml:"pattern"`
	Message string `yaml:"message"`
	Unless  string `yaml:"unless"`
}

MatchRule short-circuits: if Pattern matches the whole blob (and Unless does not), return Message immediately.

type Registry

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

Registry holds compiled filters, matched by descending priority then by name for determinism (specific-before-generic without relying on alphabetical luck).

func (*Registry) Len

func (r *Registry) Len() int

Len reports how many filters are loaded.

func (*Registry) Load

func (r *Registry) Load(b []byte) error

Load parses a YAML filter document and appends its filters to the registry. schema_version must be 1. Duplicate filter names are rejected (a silently shadowed filter is a debugging trap — rtk's build.rs rejects them too) and any inline tests the document carries must pass, so a broken filter fails loudly at load instead of quietly mangling output. (Requiring a test to EXIST is enforced for the shipped builtins by a unit test, not here — user configs stay free.)

func (*Registry) Match

func (r *Registry) Match(key string) *Compiled

Match returns the first filter whose match regex matches key, or nil.

The union gate short-circuits the miss (see buildGate); on a hit the ordered scan runs exactly as before, so priority still decides the winner.

type ReplaceRule

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

ReplaceRule is a chained line-by-line regex substitution ($1 backrefs allowed).

type TestCase

type TestCase struct {
	Name     string `yaml:"name"`
	Input    string `yaml:"input"`
	Expected string `yaml:"expected"`
}

TestCase is an inline filter test (input -> expected), run by RunTests.

Jump to

Keyboard shortcuts

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