Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NoFlags ¶ added in v0.0.2
func NoFlags(fn HandlerFunc) func(*FlagSet) HandlerFunc
NoFlags wraps a HandlerFunc in the MakeFlags format for commands that declare no flags.
Types ¶
type CallContext ¶
type CallContext struct {
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
// InLoop is true when the builtin runs inside a for loop.
InLoop bool
// LastExitCode is the exit code from the previous command.
LastExitCode uint8
// OpenFile opens a file within the shell's path restrictions.
OpenFile func(ctx context.Context, path string, flags int, mode os.FileMode) (io.ReadWriteCloser, error)
// ReadDir reads a directory within the shell's path restrictions.
// Entries are returned sorted by name.
ReadDir func(ctx context.Context, path string) ([]fs.DirEntry, error)
// StatFile returns file info within the shell's path restrictions (follows symlinks).
StatFile func(ctx context.Context, path string) (fs.FileInfo, error)
// LstatFile returns file info within the shell's path restrictions (does not follow symlinks).
LstatFile func(ctx context.Context, path string) (fs.FileInfo, error)
// AccessFile checks whether the file at path is accessible with the given mode
// within the shell's path restrictions. Mode: 0x04=read, 0x02=write, 0x01=execute.
AccessFile func(ctx context.Context, path string, mode uint32) error
// PortableErr normalizes an OS error to a POSIX-style message.
PortableErr func(err error) string
// Now returns the current time. Builtins should use this instead of
// calling time.Now() directly, so the time source is consistent and
// testable.
Now func() time.Time
}
CallContext provides the capabilities available to builtin commands. It is created by the Runner for each builtin invocation.
func (*CallContext) Errf ¶
func (c *CallContext) Errf(format string, a ...any)
Errf writes a formatted string to stderr.
func (*CallContext) Outf ¶
func (c *CallContext) Outf(format string, a ...any)
Outf writes a formatted string to stdout.
type Command ¶ added in v0.0.2
type Command struct {
Name string
MakeFlags func(*FlagSet) HandlerFunc
}
Command pairs a builtin name with its flag-declaring factory. MakeFlags registers any flags on the provided FlagSet and returns the bound handler. Commands that accept no flags may ignore fs via NoFlags.
func (Command) Register ¶ added in v0.0.2
func (c Command) Register()
Register adds the Command to the builtin registry. For each invocation the framework creates a fresh *FlagSet, passes it to MakeFlags so the command can register its flags, parses the raw args, writes any error to stderr (exit 1), and then calls the bound handler with positional args only.
If MakeFlags registers no flags (e.g. via NoFlags), the framework skips parsing entirely and passes all raw args to the handler unchanged. This lets commands like echo treat flag-shaped literals (e.g. -n) correctly.
type Flag ¶ added in v0.0.2
Flag is a type alias for pflag.Flag, exposed so command files can use FlagSet.Visit without importing pflag directly.
type FlagSet ¶ added in v0.0.2
FlagSet is a type alias for pflag.FlagSet. Command files receive a *FlagSet from the framework without needing to import pflag directly (the builtins package is always allowed by the import allowlist).
type HandlerFunc ¶
type HandlerFunc func(ctx context.Context, callCtx *CallContext, args []string) Result
HandlerFunc is the bound handler called by the framework after flags are parsed. args contains only the positional (non-flag) arguments.
func Lookup ¶
func Lookup(name string) (HandlerFunc, bool)
Lookup returns the handler for a builtin command.
type Result ¶
type Result struct {
// Code is the exit status code.
Code uint8
// Exiting signals that the shell should exit (set by the "exit" builtin).
Exiting bool
// BreakN > 0 means break out of N enclosing loops.
BreakN int
// ContinueN > 0 means continue from N enclosing loops.
ContinueN int
}
Result captures the outcome of executing a builtin command.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package breakcmd implements the break builtin command.
|
Package breakcmd implements the break builtin command. |
|
Package cat implements the cat builtin command.
|
Package cat implements the cat builtin command. |
|
Package continuecmd implements the continue builtin command.
|
Package continuecmd implements the continue builtin command. |
|
Package cut implements the cut builtin command.
|
Package cut implements the cut builtin command. |
|
Package echo implements the echo builtin command.
|
Package echo implements the echo builtin command. |
|
Package exit implements the exit builtin command.
|
Package exit implements the exit builtin command. |
|
Package falsecmd implements the false builtin command.
|
Package falsecmd implements the false builtin command. |
|
Package grep implements the grep builtin command.
|
Package grep implements the grep builtin command. |
|
Package head implements the head builtin command.
|
Package head implements the head builtin command. |
|
internal
|
|
|
Package ls implements the ls builtin command.
|
Package ls implements the ls builtin command. |
|
Package strings_cmd implements the strings builtin command.
|
Package strings_cmd implements the strings builtin command. |
|
Package tail implements the tail builtin command.
|
Package tail implements the tail builtin command. |
|
Package testcmd implements the POSIX test and [ builtin commands.
|
Package testcmd implements the POSIX test and [ builtin commands. |
|
Package testutil provides shared test helpers for builtin command tests.
|
Package testutil provides shared test helpers for builtin command tests. |
|
Package truecmd implements the true builtin command.
|
Package truecmd implements the true builtin command. |
|
Package uniq implements the uniq builtin command.
|
Package uniq implements the uniq builtin command. |
|
Package wc implements the wc builtin command.
|
Package wc implements the wc builtin command. |