command

package module
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 6 Imported by: 0

README

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Awk

func Awk(program Program, opts ...any) gloo.Command[[]byte, []byte]

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

type AwkVariable struct {
	Value any
	Name  string
}

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

func (c Context) Field(index int) string

Field returns the field at the given index (0 = whole line, 1 = first field, etc.)

func (Context) Print

func (c Context) Print(values ...any) string

Print formats and returns a string with fields separated by OFS

func (Context) SetField

func (c Context) SetField(index int, value string) Context

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.

func (Context) SetVar

func (c Context) SetVar(name string, value any) Context

SetVar returns a copy of the context with the variable set.

func (Context) Var

func (c Context) Var(name string) any

Var returns a variable value

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

func (SimpleProgram) Action(ctx Context) (Context, string, bool)

func (SimpleProgram) Begin

func (SimpleProgram) Begin(ctx Context) (Context, error)

func (SimpleProgram) Condition

func (SimpleProgram) Condition(Context) bool

func (SimpleProgram) End

Directories

Path Synopsis
Package alias provides unprefixed type aliases for awk command flags.
Package alias provides unprefixed type aliases for awk command flags.

Jump to

Keyboard shortcuts

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