Back to Microsandbox

Execution

docs/sdk/go/execution.mdx

0.6.733.2 KB
Original Source

Run commands inside a running sandbox, collect their output, stream events live, or drive an interactive pseudo-terminal programmatically. See Commands for usage examples.

The exec API mirrors os/exec conventions: a non-zero exit code is not a Go error. Transport, timeout, and spawn-failure paths return an error; a program that ran and exited non-zero is a normal *ExecOutput result, inspect Success() or ExitCode().

<p className="msb-label" id="typical-flow">Typical flow</p>
go
import m "github.com/superradcompany/microsandbox/sdk/go"

out, err := sb.Exec(ctx, "python3", []string{"-c", "print(1 + 1)"})
if err != nil {
    return err // transport / timeout / spawn failure
}
if !out.Success() {
    log.Printf("exited %d: %s", out.ExitCode(), out.Stderr())
}
fmt.Print(out.Stdout())

Sandbox methods

The exec entry points live on *Sandbox.

<span className="msb-recv">sb.</span><span className="msb-hn">Exec()</span>

go
func (s *Sandbox) Exec(ctx context.Context, cmd string, args []string, opts ...ExecOption) (*ExecOutput, error)
<Accordion title="Example">
go
out, err := sb.Exec(ctx, "make", []string{"build"},
    m.WithExecCwd("/app"),
    m.WithExecEnv(map[string]string{"DEBUG": "1"}),
)
</Accordion>

Run a command in the sandbox and return its collected output. Blocks until the command exits. The returned error is non-nil only on transport or runtime failures; a non-zero exit code is reported via ExecOutput.ExitCode, not as an error.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the wait. The guest process may continue in the background.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>cmd</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Program to run. Passed literally to the guest agent (the image ENTRYPOINT is not consulted).</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>args</code><span className="msb-type">[]string</span></div> <div className="msb-param-desc">Command arguments. May be <code>nil</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#execoption">...ExecOption</a></div> <div className="msb-param-desc">Per-command cwd, timeout, TTY allocation, user, and env.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#execoutput">*ExecOutput</a></div> <div className="msb-param-desc">Collected stdout, stderr, and exit code.</div> </div> </div>

<span className="msb-recv">sb.</span><span className="msb-hn">Shell()</span>

go
func (s *Sandbox) Shell(ctx context.Context, command string, opts ...ExecOption) (*ExecOutput, error)
<Accordion title="Example">
go
out, err := sb.Shell(ctx, "echo $HOME && ls /tmp")
if err != nil {
    return err
}
fmt.Print(out.Stdout())
</Accordion>

Run /bin/sh -c command in the sandbox and collect its output. Blocks until the command exits. A convenience wrapper over Exec for shell one-liners.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the wait.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>command</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Shell command line passed to <code>/bin/sh -c</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#execoption">...ExecOption</a></div> <div className="msb-param-desc">Per-command cwd, timeout, TTY allocation, user, and env.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#execoutput">*ExecOutput</a></div> <div className="msb-param-desc">Collected stdout, stderr, and exit code.</div> </div> </div>

<span className="msb-recv">sb.</span><span className="msb-hn">ExecStream()</span>

go
func (s *Sandbox) ExecStream(ctx context.Context, cmd string, args []string, opts ...ExecOption) (*ExecHandle, error)
<Accordion title="Example">
go
h, err := sb.ExecStream(ctx, "cat", nil, m.WithExecStdinPipe())
if err != nil {
    return err
}
defer h.Close()
</Accordion>

Start a streaming exec session and return an *ExecHandle. The handle MUST be closed with Close when the stream is no longer needed. Nonblocking: the handle returns immediately and the stream starts in the background.

ctx controls only the start handshake; individual Recv calls take their own ctx. Non-zero exit codes are not errors, inspect ExecEventExited.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Controls the start handshake only.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>cmd</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Program to run.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>args</code><span className="msb-type">[]string</span></div> <div className="msb-param-desc">Command arguments. May be <code>nil</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#execoption">...ExecOption</a></div> <div className="msb-param-desc">Per-command options; add <a className="msb-type" href="#withexecstdinpipe">WithExecStdinPipe()</a> to enable stdin and <a className="msb-type" href="#withexectty">WithExecTTY(true)</a> to allocate a pseudo-terminal.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#exechandle">*ExecHandle</a></div> <div className="msb-param-desc">Live exec session. Close it when done.</div> </div> </div>

<span className="msb-recv">sb.</span><span className="msb-hn">ShellStream()</span>

go
func (s *Sandbox) ShellStream(ctx context.Context, command string, opts ...ExecOption) (*ExecHandle, error)
<Accordion title="Example">
go
h, err := sb.ShellStream(ctx, "tail -f /var/log/app.log")
if err != nil {
    return err
}
defer h.Close()

for {
    ev, err := h.Recv(ctx)
    if err != nil {
        return err
    }
    switch ev.Kind {
    case m.ExecEventStdout:
        os.Stdout.Write(ev.Data)
    case m.ExecEventDone:
        return nil
    }
}
</Accordion>

Run /bin/sh -c command with streaming output. A convenience wrapper over ExecStream. Nonblocking: the handle returns immediately and the stream starts in the background.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Controls the start handshake only.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>command</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Shell command line passed to <code>/bin/sh -c</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#execoption">...ExecOption</a></div> <div className="msb-param-desc">Per-command options.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#exechandle">*ExecHandle</a></div> <div className="msb-param-desc">Live exec session. Close it when done.</div> </div> </div>

ExecHandle methods

A live streaming exec session returned by ExecStream and ShellStream. Signal, Kill, and Resize may be called concurrently with Recv, allowing a control goroutine to act while another goroutine waits for terminal output. Other method combinations, including concurrent Recv calls, are not supported.

<span className="msb-recv">h.</span><span className="msb-hn">ID()</span>

go
func (h *ExecHandle) ID() (string, error)

Return the unique identifier for this exec session, assigned by the guest agent. Useful for correlating log entries or referencing the session from external tooling.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Session correlation id.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">TakeStdin()</span>

go
func (h *ExecHandle) TakeStdin() *ExecSink
<Accordion title="Example">
go
sink := h.TakeStdin()
sink.Write([]byte("hello\n"))
sink.Close()
</Accordion>

Return the stdin sink for this exec session. Single-take: returns nil if the session was not started with WithExecStdinPipe, or if TakeStdin was already called on this handle (matching the Node and Python SDKs). The caller is responsible for closing the sink when done writing; closing the sink without closing the exec handle is fine, they own different Rust-side resources.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#execsink">*ExecSink</a></div> <div className="msb-param-desc">Stdin writer, or <code>nil</code> if unavailable.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Recv()</span>

go
func (h *ExecHandle) Recv(ctx context.Context) (*ExecEvent, error)
<Accordion title="Example">
go
for {
    ev, err := h.Recv(ctx)
    if err != nil {
        return err
    }
    switch ev.Kind {
    case m.ExecEventStarted:
        fmt.Printf("started pid=%d\n", ev.PID)
    case m.ExecEventStdout:
        os.Stdout.Write(ev.Data)
    case m.ExecEventStderr:
        os.Stderr.Write(ev.Data)
    case m.ExecEventExited:
        fmt.Printf("exited code=%d\n", ev.ExitCode)
    case m.ExecEventDone:
        return nil
    }
}
</Accordion>

Block until the next event arrives or the stream ends. Returns an event with Kind == ExecEventDone when all events have been consumed. ctx controls the wait; cancellation causes Recv to return ctx.Err() immediately. The underlying Rust call may continue to completion in the background.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the wait for the next event.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#execevent">*ExecEvent</a></div> <div className="msb-param-desc">The next streamed event.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Collect()</span>

go
func (h *ExecHandle) Collect(ctx context.Context) (*ExecOutput, error)
<Accordion title="Example">
go
out, err := h.Collect(ctx)
if err != nil {
    return err
}
fmt.Print(out.Stdout())
</Accordion>

Drain the stream, accumulate all output, and return it as an *ExecOutput. Equivalent to calling Recv in a loop and assembling the result. The handle should be closed after Collect returns.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the drain.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#execoutput">*ExecOutput</a></div> <div className="msb-param-desc">Collected stdout, stderr, and exit code.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Wait()</span>

go
func (h *ExecHandle) Wait(ctx context.Context) (int, error)

Block until the process exits and return its exit code. Unlike Collect, stdout and stderr are discarded. The handle should be closed after Wait returns.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the wait.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">int</span></div> <div className="msb-param-desc">Process exit code.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Kill()</span>

go
func (h *ExecHandle) Kill(ctx context.Context) error

Send SIGKILL to the running process.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the call.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Signal()</span>

go
func (h *ExecHandle) Signal(ctx context.Context, signal int) error
<Accordion title="Example">
go
import "syscall"

if err := h.Signal(ctx, int(syscall.SIGTERM)); err != nil {
    return err
}
</Accordion>

Send a Unix signal to the running process. Pass values from syscall (e.g. int(syscall.SIGTERM)).

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the call.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>signal</code><span className="msb-type">int</span></div> <div className="msb-param-desc">Signal number, e.g. <code>int(syscall.SIGTERM)</code>.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Resize()</span>

go
func (h *ExecHandle) Resize(ctx context.Context, rows, cols uint16) error
<Accordion title="Example">
go
if err := h.Resize(ctx, 40, 120); err != nil {
    return err
}
</Accordion>

Resize the pseudo-terminal for this exec session. Use with a session started with WithExecTTY(true), typically when relaying a terminal-size change from a remote client.

Resize may be called concurrently while another goroutine is blocked in Recv.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the resize call.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>rows</code><span className="msb-type">uint16</span></div> <div className="msb-param-desc">New terminal height in character cells.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>cols</code><span className="msb-type">uint16</span></div> <div className="msb-param-desc">New terminal width in character cells.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Close()</span>

go
func (h *ExecHandle) Close() error
<Accordion title="Example">
go
defer h.Close()
</Accordion>

Release the Rust-side exec handle. Does not kill the running process; call Signal or Kill first if you need to terminate it. Safe to call after ExecEventDone has been received.

ExecOutput methods

The collected result of a completed command execution, returned by Exec, Shell, and Collect.

<span className="msb-recv">out.</span><span className="msb-hn">Stdout()</span>

go
func (e *ExecOutput) Stdout() string

Captured standard output as a string.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Collected stdout.</div> </div> </div>

<span className="msb-recv">out.</span><span className="msb-hn">StdoutBytes()</span>

go
func (e *ExecOutput) StdoutBytes() []byte

Captured standard output as raw bytes. Use when the output may not be valid UTF-8.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Raw stdout bytes.</div> </div> </div>

<span className="msb-recv">out.</span><span className="msb-hn">Stderr()</span>

go
func (e *ExecOutput) Stderr() string

Captured standard error as a string.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Collected stderr.</div> </div> </div>

<span className="msb-recv">out.</span><span className="msb-hn">StderrBytes()</span>

go
func (e *ExecOutput) StderrBytes() []byte

Captured standard error as raw bytes.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Raw stderr bytes.</div> </div> </div>

<span className="msb-recv">out.</span><span className="msb-hn">ExitCode()</span>

go
func (e *ExecOutput) ExitCode() int

The process's exit code, or -1 if the guest did not report one (e.g. the process was killed by a signal).

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">int</span></div> <div className="msb-param-desc">Exit code, or <code>-1</code>.</div> </div> </div>

<span className="msb-recv">out.</span><span className="msb-hn">Success()</span>

go
func (e *ExecOutput) Success() bool

Reports whether the command exited with code 0.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">bool</span></div> <div className="msb-param-desc"><code>true</code> if <code>ExitCode()</code> is <code>0</code>.</div> </div> </div>

ExecSink methods

A write-only pipe to a running process's stdin, obtained from ExecHandle.TakeStdin. Implements io.WriteCloser. Write and Close use context.Background() under the hood; for caller-controlled cancellation use WriteCtx, or tear the session down via ExecHandle.Kill / Close.

<span className="msb-recv">sink.</span><span className="msb-hn">Write()</span>

go
func (sk *ExecSink) Write(p []byte) (int, error)
<Accordion title="Example">
go
h, err := sb.ExecStream(ctx, "cat", nil, m.WithExecStdinPipe())
if err != nil {
    return err
}
defer h.Close()

sink := h.TakeStdin()
sink.Write([]byte("hello\n"))
sink.Close()

out, _ := h.Collect(ctx)
fmt.Print(out.Stdout()) // "hello\n"
</Accordion>

Send data to the process stdin. Implements io.Writer. Uses context.Background() internally, there is no way to cancel a stuck write through this method alone.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>p</code><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Bytes to write to stdin.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">int</span></div> <div className="msb-param-desc">Number of bytes written.</div> </div> </div>

<span className="msb-recv">sink.</span><span className="msb-hn">WriteCtx()</span>

go
func (sk *ExecSink) WriteCtx(ctx context.Context, p []byte) (int, error)

Like Write, but with a caller-controlled context, so a stuck stdin write can be cancelled.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the write.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>p</code><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Bytes to write to stdin.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">int</span></div> <div className="msb-param-desc">Number of bytes written.</div> </div> </div>

<span className="msb-recv">sink.</span><span className="msb-hn">Close()</span>

go
func (sk *ExecSink) Close() error
<Accordion title="Example">
go
sink.Close()
</Accordion>

Close the stdin sink. In non-TTY pipe mode this sends EOF to the process. In TTY mode it prevents further writes through this sink, but the guest PTY remains open; send the terminal's EOF control character (usually \x04 in canonical mode) when the interactive program needs one. Implements io.Closer.

Types

ExecOutput

<p className="msb-backref">Returned by <a href="#sb-exec">sb.Exec()</a> · <a href="#sb-shell">sb.Shell()</a> · <a href="#h-collect">h.Collect()</a></p>

The collected result of a command execution. A non-zero ExitCode is not treated as a Go error, callers inspect Success or ExitCode explicitly, matching how os/exec.Cmd.Output works against a script that exits non-zero. In TTY mode, Stdout contains the combined terminal output and Stderr is empty because a PTY cannot preserve separate stdout and stderr streams.

MethodReturnsDescription
Stdout()stringCaptured stdout as a string
StdoutBytes()[]byteRaw stdout bytes
Stderr()stringCaptured stderr as a string
StderrBytes()[]byteRaw stderr bytes
ExitCode()intExit code, or -1 if the guest did not report one
Success()booltrue if ExitCode() is 0

ExecHandle

<p className="msb-backref">Returned by <a href="#sb-execstream">sb.ExecStream()</a> · <a href="#sb-shellstream">sb.ShellStream()</a></p>

A live streaming exec session. Must be closed with Close when done to release Rust-side resources. Signal, Kill, and Resize may run concurrently with Recv; other method combinations, including multiple concurrent Recv calls, are not supported.

MethodReturnsDescription
ID()(string, error)Session correlation id assigned by the guest agent
TakeStdin()*ExecSinkTake the stdin writer (single-take; only with WithExecStdinPipe)
Recv(ctx)(*ExecEvent, error)Block until the next event arrives
Collect(ctx)(*ExecOutput, error)Drain remaining output into an ExecOutput
Wait(ctx)(int, error)Wait for exit, discarding output; returns the exit code
Kill(ctx)errorSend SIGKILL
Signal(ctx, signal)errorSend a Unix signal
Resize(ctx, rows, cols)errorResize the pseudo-terminal
Close()errorRelease the Rust-side handle

ExecSink

<p className="msb-backref">Returned by <a href="#h-takestdin">h.TakeStdin()</a></p>
go
type ExecSink = ffi.ExecSink

A write-only pipe to a running process's stdin. Implements io.WriteCloser.

MethodReturnsDescription
Write(p)(int, error)Implements io.Writer
WriteCtx(ctx, p)(int, error)Write with explicit context
Close()errorClose the sink; sends EOF in non-TTY pipe mode

ExecEvent

<p className="msb-backref">Returned by <a href="#h-recv">h.Recv()</a></p>

One event from a streaming exec session. Kind identifies which fields are populated.

FieldTypeDescription
KindExecEventKindIdentifies which fields are populated
PIDuint32Guest process id, set on ExecEventStarted
Data[]byteChunk of stdout or stderr, set on ExecEventStdout / ExecEventStderr
ExitCodeintProcess exit code, set on ExecEventExited
Failure*ExecFailureFailure detail, set on ExecEventFailed and ExecEventStdinError

ExecEventKind

<p className="msb-backref">Field of <a href="#execevent">ExecEvent</a></p>
go
type ExecEventKind = ffi.ExecEventKind

Identifies what an ExecEvent carries.

ConstantDescription
ExecEventStartedSent once when the guest process starts; PID is valid
ExecEventStdoutA chunk of stdout; in TTY mode, carries the combined terminal output stream. Data is valid
ExecEventStderrA chunk of stderr; not emitted separately in TTY mode. Data is valid
ExecEventExitedThe process exited; ExitCode is valid
ExecEventFailedThe user program never started (binary missing, permission denied, ...); Failure is valid and ExitCode is not meaningful
ExecEventStdinErrorA stdin write failed; Failure is valid. Non-terminal: the process may still exit normally
ExecEventDoneAll events have been consumed

ExecFailure

<p className="msb-backref">Field of <a href="#execevent">ExecEvent</a></p>
go
type ExecFailure = ffi.ExecFailure

Structured detail about a failed-to-start exec, populated on ExecEventFailed and ExecEventStdinError. See Error Handling for the kinds it carries (not_found, permission_denied, etc.) and how to branch on them.

FieldTypeDescription
KindstringFailure category, e.g. not_found, permission_denied
Errno*intUnderlying errno, if known
ErrnoNamestringSymbolic errno name, if known
MessagestringHuman-readable failure message
PathstringOffending path, if applicable

ExecConfig

<p className="msb-backref">Populated by <a href="#execoption">ExecOption</a></p>

Configures a single Exec or ExecStream call. Most callers set fields through the WithExec* functional options; ExecConfig is exported for parity with the other SDKs' config types.

FieldTypeDescription
CwdstringWorking directory inside the guest
Timeouttime.DurationKill the process after this duration; sub-second precision is rounded up
StdinPipeboolEnable a stdin pipe; required for TakeStdin
TTYboolAllocate a pseudo-terminal; default false
UserstringGuest user (UID or name)
Envmap[string]stringPer-command environment variables

ExecOption

<p className="msb-backref">Accepted by <a href="#sb-exec">sb.Exec()</a> · <a href="#sb-shell">sb.Shell()</a> · <a href="#sb-execstream">sb.ExecStream()</a> · <a href="#sb-shellstream">sb.ShellStream()</a></p>
go
type ExecOption func(*ExecConfig)

A functional option that mutates an ExecConfig. Construct them with the WithExec* functions below.

<span className="msb-hn">WithExecCwd()</span>

go
func WithExecCwd(path string) ExecOption

Set the working directory for a single command.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>path</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Absolute path inside the guest.</div> </div> </div>

<span className="msb-hn">WithExecTimeout()</span>

go
func WithExecTimeout(d time.Duration) ExecOption
<Accordion title="Example">
go
out, err := sb.Shell(ctx, "long-running-task",
    m.WithExecTimeout(30*time.Second))
if m.IsKind(err, m.ErrExecTimeout) {
    log.Println("timed out")
}
</Accordion>

Set a per-command timeout. When exceeded, the guest terminates the process and the call returns an error with Kind == ErrExecTimeout. Sub-second precision rounds up to whole seconds; pass at least 1 second.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>d</code><span className="msb-type">time.Duration</span></div> <div className="msb-param-desc">Timeout duration, rounded up to whole seconds.</div> </div> </div>

<span className="msb-hn">WithExecStdinPipe()</span>

go
func WithExecStdinPipe() ExecOption

Enable a stdin pipe for the exec session, allowing data to be written via ExecHandle.TakeStdin.

<span className="msb-hn">WithExecTTY()</span>

go
func WithExecTTY(enabled bool) ExecOption
<Accordion title="Example">
go
h, err := sb.ExecStream(ctx, "/bin/bash", nil,
    m.WithExecStdinPipe(),
    m.WithExecTTY(true),
)
if err != nil {
    return err
}
defer h.Close()

stdin := h.TakeStdin()
if stdin == nil {
    return errors.New("stdin pipe is unavailable")
}

if err := h.Resize(ctx, 40, 120); err != nil {
    return err
}
if _, err := stdin.Write([]byte("echo hello\n")); err != nil {
    return err
}
</Accordion>

Control whether the command runs inside a pseudo-terminal. Enable it for interactive programs such as shells, editors, REPLs, and top. Default: false.

When enabled, the guest process runs with its stdin, stdout, and stderr connected to the PTY. Streaming sessions expose the combined terminal output through ExecEventStdout; they do not emit a separately attributable stderr stream. Collected output similarly places the combined stream in Stdout and leaves Stderr empty. Add WithExecStdinPipe() to write to the terminal programmatically, and use Resize to update its dimensions after the session starts.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>enabled</code><span className="msb-type">bool</span></div> <div className="msb-param-desc"><code>true</code> to allocate a pseudo-terminal.</div> </div> </div>

<span className="msb-hn">WithExecUser()</span>

go
func WithExecUser(user string) ExecOption

Run the command as the given guest user (UID or name).

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>user</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Guest user, as a UID or name.</div> </div> </div>

<span className="msb-hn">WithExecEnv()</span>

go
func WithExecEnv(env map[string]string) ExecOption
<Accordion title="Example">
go
out, err := sb.Exec(ctx, "make", []string{"build"},
    m.WithExecCwd("/app"),
    m.WithExecEnv(map[string]string{"DEBUG": "1"}),
)
</Accordion>

Add per-command environment variables. Called repeatedly, maps merge; later keys overwrite earlier ones.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>env</code><span className="msb-type">map[string]string</span></div> <div className="msb-param-desc">Environment variables for this command.</div> </div> </div>