router

package
v0.12.0 Latest Latest
Warning

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

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

Documentation

Overview

Package router routes a command to drop/host/container and executes it, independent of any transport (MCP, CLI). Output is written to the caller's io.Writers.

Index

Constants

This section is empty.

Variables

View Source
var ErrSandboxNotRunning = errors.New("sandbox is not running")

ErrSandboxNotRunning signals that a command was routed to the sandbox container but the container is not running. A ContainerRunner returns it (possibly wrapped) so the router can print an actionable message instead of a raw Docker/Compose error. Detect it with errors.Is.

View Source
var ErrUnterminatedQuote = errors.New("unterminated quote")

ErrUnterminatedQuote is returned by ParseLine when a quote is never closed.

Functions

func Route

func Route(cmd string, allow, drop []string) (decision, matched string)

func Run added in v0.9.2

func Run(ctx context.Context, req Request) (int, error)

Run routes req.Command per segment and executes the resulting pipelines in sequence, honoring && / || / ; operators between pipelines.

func RunHost added in v0.9.2

func RunHost(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error)

func RunHostShell added in v0.9.2

func RunHostShell(ctx context.Context, raw string, stdin io.Reader, stdout, stderr io.Writer) (int, error)

RunHostShell runs raw via "bash -c" on the host. Used for segments that contain a redirect (which the shell-free argv path cannot express).

Types

type Config added in v0.9.2

type Config struct {
	AllowPatterns           []string
	DropPatterns            []string
	ContainerEnvPassthrough []string
	ContainerRunner         ContainerRunner // nil allowed (host/drop-only lines)
}

Config holds routing patterns and the optional container runner.

type ContainerRunner added in v0.9.2

type ContainerRunner interface {
	RunContainer(ctx context.Context, argv []string, env []string, stdin io.Reader, stdout, stderr io.Writer) (int, error)
}

ContainerRunner executes an argv inside the sandbox container.

type Line added in v0.9.2

type Line struct {
	Raw       string
	Pipelines []PipelineNode
	Seps      []string // "&&" | "||" | ";"  (len == len(Pipelines)-1)
	Fallback  bool
}

Line is a parsed command line: a sequence of pipelines joined by sequential operators. When Fallback is true the line contains a construct we do not split (command substitution or background &) and must run whole.

func ParseLine added in v0.9.2

func ParseLine(raw string) (Line, error)

ParseLine tokenizes raw into a structured Line. It returns ErrUnterminatedQuote for an unclosed quote.

type PipelineNode added in v0.9.2

type PipelineNode struct {
	Raw      string
	Segments []Segment
}

PipelineNode is a sequence of segments joined by "|".

type Request added in v0.9.2

type Request struct {
	Command                 string
	AllowPatterns           []string
	DropPatterns            []string
	ContainerRunner         ContainerRunner
	ContainerEnvPassthrough []string
	Stdout                  io.Writer
	Stderr                  io.Writer
}

Request carries everything Run needs for a single command.

type Result added in v0.9.2

type Result struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
}

Result is the buffered outcome of RunBuffered.

type Router added in v0.9.2

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

Router routes and runs command lines.

func New added in v0.9.2

func New(cfg Config) *Router

New builds a Router from cfg.

func (*Router) NeedsContainer added in v0.9.2

func (s *Router) NeedsContainer(command string) (bool, error)

NeedsContainer reports whether running command requires a container runner. It uses ParseLine to check each segment individually, so a pipeline with any container-routed segment returns true, and a Fallback line always returns true.

func (*Router) Run added in v0.9.2

func (s *Router) Run(ctx context.Context, command string, stdout, stderr io.Writer) (int, error)

Run routes command and streams output to stdout/stderr, returning the exit code. The error is non-nil only on host-execution infrastructure failure.

func (*Router) RunBuffered added in v0.9.2

func (s *Router) RunBuffered(ctx context.Context, command string) (Result, error)

RunBuffered runs command and captures stdout/stderr into memory.

type Segment added in v0.9.2

type Segment struct {
	Raw         string
	Args        []string
	HasRedirect bool
}

Segment is a simple command, possibly carrying a redirect.

Jump to

Keyboard shortcuts

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