awk

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package awk is a thin wrapper on top of interp.Interp and parser.Parser providing a compatible unix.Filter interface for goawk.

Copyright 2023 Michal Vyskocil. All rights reserved. Use of this source code is governed by a MIT license that can be found in the LICENSE file.

adapted from https://github.com/benhoyt/goawk/blob/v1.31.0/goawk.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromArgs

func FromArgs(name string, args []string, opts ...unix.ShellContextOption) (unix.Filter, error)

Types

type AWK

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

AWK is a thin wrapper on top of github.com/benhoyt/goawk

func Compile

func Compile(src []byte, config *interp.Config) (AWK, error)

func New

func New(prog *parser.Program, config *interp.Config) AWK

func (AWK) Run

func (c AWK) Run(ctx context.Context, stdio unix.StandardIO) error

type Builder added in v0.2.1

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

Builder configures the goawk interpreter, that can use a different functions for a opening a files and executing the commands. This allows awk programs to be sandboxed and executed fully in user space.

func NewBuilder added in v0.2.1

func NewBuilder() Builder

func (Builder) FromArgs added in v0.2.1

func (b Builder) FromArgs(name string, args []string, opts ...unix.ShellContextOption) (unix.Filter, error)

func (Builder) WithCommandFunc added in v0.2.3

func (b Builder) WithCommandFunc(c interp.CommandFunc) Builder

WithCommandFunc replaces goawk's default [exec.CommandContext] based command execution with a custom function, used for system(), `cmd | getline`, and `print | cmd`. goawk invokes it as a shell: c(ctx, "/bin/sh", "-c", "<script>"), so it receives the command line as the last argument and must interpret it with shell semantics (pipes, redirection, quoting) — not merely split it. Use NewCommandBuilder to properly adapt a unix.FilterBuilderFunc into the returned interp.Cmd.

func (Builder) WithOpenFileFunc added in v0.2.1

func (b Builder) WithOpenFileFunc(f interp.OpenFileFunc) Builder

WithOpenFileFunc makes goawk open files through f instead of os.OpenFile. Use it to confine file access, e.g. route every open through an os.Root sandbox or an in-memory filesystem.

type Command added in v0.2.3

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

Command is an adapter between [goawk.Cmd] and unix.Filter allowing external code to inject a different code implementing script execution.

func (*Command) SetStderr added in v0.2.3

func (c *Command) SetStderr(w io.Writer)

func (*Command) SetStdin added in v0.2.3

func (c *Command) SetStdin(r io.Reader)

func (*Command) SetStdout added in v0.2.3

func (c *Command) SetStdout(w io.Writer)

func (*Command) Start added in v0.2.3

func (c *Command) Start() error

Start runs the filter in own goroutine, which stdio taken from the appropriate methods. The wrapper closes stdout and stdin pipes when filter's run mthod returns. The interface is mapped according [exec.Cmd] except [cmd.WaitExitCode], which immplements the goawk expected semantics.

While it passes the context to the underlying unix.Filter this does not deal with a context cancellation, except for an error reporting. It's expected that underlying filter of stdio are context aware.

func (*Command) StdinPipe added in v0.2.3

func (c *Command) StdinPipe() (io.WriteCloser, error)

func (*Command) StdoutPipe added in v0.2.3

func (c *Command) StdoutPipe() (io.ReadCloser, error)

func (*Command) WaitExitCode added in v0.2.3

func (c *Command) WaitExitCode() (int, error)

WaitExitCode blocks until the filter's Run returns, then reports the result using goawk's status convention, so it flows correctly into system()'s return value and a command pipe's close status:

  • Run returned nil — a clean exit. Reports 0 and a nil error.
  • Run returned an error implementing ExitCode() int a normal non-zero exit.
  • Run returned any other error — a genuine I/O or internal failure. Reports -1 and the error.
  • ctx was cancelled — reports -1 and ctx.Err(), which goawk re-checks and propagates to abort the script. The ctx check takes precedence, so cancellation is reported as cancellation even if Run also returned an error on its way out.

type CommandBuilder added in v0.2.3

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

func NewCommandBuilder added in v0.2.3

func NewCommandBuilder(bf unix.FilterBuilderFunc, opts ...unix.ShellContextOption) CommandBuilder

func (CommandBuilder) Command added in v0.2.3

func (b CommandBuilder) Command(ctx context.Context, name string, args ...string) interp.Cmd

Jump to

Keyboard shortcuts

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