Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Awk ¶
Awk returns a command that processes input lines through an awk-style program. The program defines Begin, Condition, Action, and End phases.
If opts contains positional file paths or io.Reader inputs, those are used as the data source, overriding the upstream input stream. This lets callers do either Awk(prog) (transform an input stream) or Awk(prog, file...) / Awk(prog, reader) (self-source from those inputs).
Example ¶
package main
import (
"fmt"
"github.com/gloo-foo/testable"
command "github.com/gloo-foo/cmd-awk"
)
// printAll prints every line unchanged.
type printAll struct{ command.SimpleProgram }
func main() {
lines, _ := testable.TestLines(command.Awk(printAll{}), "hello\nworld\n")
for _, line := range lines {
fmt.Println(line)
}
}
Output: hello world
Example (PatternMatch) ¶
package main
import (
"fmt"
"strings"
"github.com/gloo-foo/testable"
command "github.com/gloo-foo/cmd-awk"
)
// patternMatch only emits lines containing "err".
type patternMatch struct{ command.SimpleProgram }
func (patternMatch) Condition(ctx command.Context) bool {
return strings.Contains(ctx.Field(0), "err")
}
func main() {
lines, _ := testable.TestLines(command.Awk(patternMatch{}), "info\nerror\nwarn\n")
for _, line := range lines {
fmt.Println(line)
}
}
Output: error
Types ¶
type AwkFieldSeparator ¶
type AwkFieldSeparator string
AwkFieldSeparator is the -F flag: the input field separator.
type AwkOutputFieldSeparator ¶
type AwkOutputFieldSeparator string
AwkOutputFieldSeparator sets OFS: the output field separator.
type AwkVariable ¶
AwkVariable is the -v flag: a variable pre-set before the program runs.
type Context ¶
type Context struct {
Variables map[string]any
FS string
OFS string
RS string
Fields []string
NR int64
NF int
}
Context provides access to awk's execution context for each line. It is an immutable value: the Set… methods return an updated copy rather than mutating in place, and programs hand the updated context back to the pipeline (Begin and Action return it).
func (Context) Field ¶
Field returns the field at the given index (0 = whole line, 1 = first field, etc.)
func (Context) SetField ¶
SetField returns a copy of the context with the field at index set, growing the record as needed. A negative index returns the context unchanged.
type Program ¶
type Program interface {
// Begin is called once before processing any lines
// Use this for initialization; return the (possibly updated) context
Begin(ctx Context) (Context, error)
// Condition is called for each line to determine if Action should run
// Return true to run the action, false to skip
Condition(ctx Context) bool
// Action is called for each line where Condition returns true
// Return the updated context, the output string, and whether to emit it
Action(ctx Context) (updated Context, output string, isEmit bool)
// End is called once after processing all lines
// Return any final output and an error if needed
End(ctx Context) (output string, err error)
}
Program defines the interface for awk-style programs all methods are optional - implement only what you need
type SimpleProgram ¶
type SimpleProgram struct{}
SimpleProgram provides default implementations for all Program methods Embed this in your program struct and override only what you need
func (SimpleProgram) Condition ¶
func (SimpleProgram) Condition(Context) bool