go-process/specs/exec.md

2.7 KiB

exec

Import: dappco.re/go/core/process/exec Files: 3

Types

Options

struct

Execution settings stored on Cmd.

Fields:

  • Dir string: Working directory.
  • Env []string: Additional environment entries appended to os.Environ().
  • Stdin io.Reader: Reader wired to command stdin.
  • Stdout io.Writer: Writer wired to command stdout.
  • Stderr io.Writer: Writer wired to command stderr.

Cmd

struct

Fluent wrapper around os/exec.Cmd.

Exported fields:

  • None.

Logger

interface

Structured logger contract used by the package.

Methods:

  • Debug(msg string, keyvals ...any): Debug-level event logging.
  • Error(msg string, keyvals ...any): Error-level event logging.

NopLogger

struct

No-op logger implementation used as the package default.

Exported fields:

  • None.

Functions

Package Functions

  • func Command(ctx context.Context, name string, args ...string) *Cmd: Creates a Cmd with the supplied command name, arguments, and context.
  • func RunQuiet(ctx context.Context, name string, args ...string) error: Runs a command with stdout suppressed, captures stderr into a buffer, and wraps any failure with core.E("RunQuiet", ...).
  • func SetDefaultLogger(l Logger): Replaces the package-level default logger. Passing nil resets it to NopLogger.
  • func DefaultLogger() Logger: Returns the current package-level default logger.

Cmd Methods

  • func (c *Cmd) WithDir(dir string) *Cmd: Sets the working directory on the stored options and returns the same command for chaining.
  • func (c *Cmd) WithEnv(env []string) *Cmd: Sets extra environment variables and returns the same command for chaining.
  • func (c *Cmd) WithStdin(r io.Reader) *Cmd: Sets stdin and returns the same command for chaining.
  • func (c *Cmd) WithStdout(w io.Writer) *Cmd: Sets stdout and returns the same command for chaining.
  • func (c *Cmd) WithStderr(w io.Writer) *Cmd: Sets stderr and returns the same command for chaining.
  • func (c *Cmd) WithLogger(l Logger) *Cmd: Sets a per-command logger that overrides the package default.
  • func (c *Cmd) Run() error: Builds the underlying exec.Cmd, logs a debug event, runs the command, and wraps failures with command context.
  • func (c *Cmd) Output() ([]byte, error): Builds the underlying exec.Cmd, logs a debug event, returns stdout bytes, and wraps failures.
  • func (c *Cmd) CombinedOutput() ([]byte, error): Builds the underlying exec.Cmd, logs a debug event, returns combined stdout and stderr bytes, and wraps failures while preserving the output bytes from exec.Cmd.CombinedOutput.

NopLogger Methods

  • func (NopLogger) Debug(string, ...any): Discards debug messages.
  • func (NopLogger) Error(string, ...any): Discards error messages.