command

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package command provides a simple facade for running shell commands synchronously.

Use this package when executing external programs with control over input, working directory, environment, and output limits. It captures combined output and supports timeouts through context cancellation.

Index

Constants

This section is empty.

Variables

View Source
var ErrMaxOutputSizeExceeded = errors.New("maximum output size exceeded")

ErrMaxOutputSizeExceeded is returned when the output exceeds the maximum size limit.

Functions

func Run

func Run(ctx context.Context, name string, args []string, opts ...Option) ([]byte, error)

Run executes a command synchronously and waits for it to complete. The combined stdout and stderr output is stored in memory and returned. Use WithMaxOutputSize to limit the amount of output stored. It returns an error if the command fails to start or returns a non-zero exit code.

func RunJSON added in v0.1.1

func RunJSON[T any](ctx context.Context, name string, args []string, opts ...Option) (*T, error)

RunJSON executes a command synchronously and unmarshals the output as JSON. It returns a pointer to the unmarshalled value of type T. It returns an error if the command fails or if the output cannot be unmarshalled as JSON.

Types

type Option

type Option func(cmdOpts *commandOptions)

Option is used to configure command execution.

func WithDir

func WithDir(dir string) Option

WithDir sets the working directory for the command. If empty, the command runs in the calling process's current directory.

func WithEnv

func WithEnv(env []string) Option

WithEnv sets the environment variables for the command. Each entry should be in the form "key=value". If nil, the new process uses the current process's environment.

func WithExtraEnv

func WithExtraEnv(env ...string) Option

WithExtraEnv appends additional environment variables to the current process's environment. Each entry should be in the form "key=value".

func WithMaxOutputSize added in v0.1.1

func WithMaxOutputSize(size int) Option

WithMaxOutputSize sets the maximum size in bytes for the combined output buffer. If the output exceeds this limit, it will be truncated. If size is 0 or negative, the output buffer has no size limit.

func WithStdin

func WithStdin(r io.Reader) Option

WithStdin sets the reader for the command's standard input.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets a timeout for the command execution. If the command does not complete within the specified duration, it will be terminated.

Jump to

Keyboard shortcuts

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