exec

package
v1.21.21 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 23 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsShellOperators

func ContainsShellOperators(cmd string) bool

ContainsShellOperators checks if a command contains shell-specific operators that require wrapping in a shell (bash -c or sh -c)

func NewDebugLogger

func NewDebugLogger(log logger.Logger, level logger.LogLevel) logger.Logger

Types

type ExecLogger

type ExecLogger struct {
	Stderr io.Writer
	Stdout io.Writer
	// contains filtered or unexported fields
}

ExecLogger buffers a subprocess's stdout/stderr. The subprocess writes the buffers (via the writers returned by GetStdoutWriter/GetStderrWriter) on the os/exec copy goroutines while callers read them via GetStdout/GetStderr — so every buffer access is guarded by mu. bytes.Buffer is not safe for concurrent read/write on its own.

func NewExecLogger

func NewExecLogger() *ExecLogger

func (*ExecLogger) GetOutput

func (e *ExecLogger) GetOutput() string

func (*ExecLogger) GetStderr

func (e *ExecLogger) GetStderr() string

func (*ExecLogger) GetStderrWriter

func (l *ExecLogger) GetStderrWriter() (writer io.Writer)

func (*ExecLogger) GetStdout

func (e *ExecLogger) GetStdout() string

func (*ExecLogger) GetStdoutWriter

func (l *ExecLogger) GetStdoutWriter() (writer io.Writer)

func (*ExecLogger) Reset

func (l *ExecLogger) Reset()

func (*ExecLogger) Tee

func (l *ExecLogger) Tee(stdout, stderr io.Writer) *ExecLogger

WithTee returns a new ExecLogger that tees logs to stdout/stderr as well.

type ExecResult

type ExecResult struct {
	Stdout   string        `json:"stdout,omitempty"`
	Stderr   string        `json:"stderr,omitempty"`
	Status   string        `json:"status,omitempty"`
	ExitCode int           `json:"exit_code,omitempty"`
	Started  *time.Time    `json:"started,omitempty"`
	Duration time.Duration `json:"duration,omitempty"`
	PID      int           `json:"pid,omitempty"`
	Command  string        `json:"command,omitempty"`
	Args     []string      `json:"args,omitempty"`
	Error    error         `json:"error,omitempty"`
	// contains filtered or unexported fields
}

ExecResult contains the result of a command execution with structured output and metadata.

func (ExecResult) IsCompleted

func (r ExecResult) IsCompleted() bool

func (ExecResult) IsOk

func (r ExecResult) IsOk() bool

func (ExecResult) IsPending

func (r ExecResult) IsPending() bool

func (ExecResult) Output

func (r ExecResult) Output() string

func (ExecResult) Pretty

func (r ExecResult) Pretty() api.Text

func (ExecResult) PrettyFull

func (r ExecResult) PrettyFull() api.Textable

func (*ExecResult) Refresh

func (e *ExecResult) Refresh() *ExecResult

Refresh refreshes the ExecResult by re-fetching data from the underlying Process

type Process

type Process struct {
	Started *time.Time

	Timeout time.Duration
	Env     map[string]string
	Cwd     string
	Err     error
	Shell   string
	Cmd     string
	Args    []string
	// Consider a non-zero exit code as an error
	SucceedOnNonZero bool
	// contains filtered or unexported fields
}

func NewExec

func NewExec(cmd string, args ...string) *Process

NewExec creates a new Process with the specified command and arguments

func NewExecf

func NewExecf(cmd string, args ...any) *Process

NewExecf creates a new Process with formatted command string

func (*Process) AsWrapper

func (p *Process) AsWrapper() WrapperFunc

AsWrapper converts the Process into a reusable WrapperFunc that executes commands with the template's configuration. Each invocation creates a new Process instance by copying the template's settings.

Example:

docker := clicky.Exec("docker", "-v").AsWrapper()
result, err := docker("ps", "-a")
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.Stdout)

func (*Process) Debug

func (p *Process) Debug() *Process

func (*Process) ExitCode

func (p *Process) ExitCode() int

func (*Process) ForceKill

func (p *Process) ForceKill() error

func (*Process) GetOutput

func (p *Process) GetOutput() string

func (*Process) GetStderr

func (p *Process) GetStderr() string

func (*Process) GetStdout

func (p *Process) GetStdout() string

func (*Process) GetTask

func (p *Process) GetTask() *task.Task

GetTask implements the Taskable interface

func (*Process) IsOK

func (p *Process) IsOK() bool

func (*Process) IsRunning

func (p *Process) IsRunning() bool

func (*Process) Kill

func (p *Process) Kill(timeout time.Duration) error

func (*Process) KillTree

func (p *Process) KillTree() error

KillTree terminates the subprocess and every descendant. Prefer this over Kill/ForceKill for processes that fork or exec grandchildren (test runners, headless browsers, etc). When WithProcessGroup was set at spawn, the kill is atomic (POSIX: SIGKILL to -pgid; Windows: TerminateJobObject); otherwise KillTree falls back to a gopsutil-style descendant walk, which is racy.

func (*Process) MustStop

func (p *Process) MustStop(timeout time.Duration) error

MustStop attempts to gracefully stop a process, after which it is forcefully killed

func (*Process) Name

func (p *Process) Name() string

func (*Process) Out

func (p *Process) Out() string

func (*Process) Pid

func (p *Process) Pid() int

Pid returns the OS pid of the running subprocess, or 0 if it hasn't started yet. Safe to call concurrently with Run().

func (*Process) Pretty

func (p *Process) Pretty() api.Text

func (*Process) Result

func (p *Process) Result() *ExecResult

func (*Process) Run

func (p *Process) Run() *Process

func (*Process) RunAsTask

func (p *Process) RunAsTask(name string, opts ...task.Option) task.TypedTask[ExecResult]

StartAsTask creates and starts a Task for this Process with typed result handling

func (*Process) Short

func (p *Process) Short() api.Text

func (*Process) Start

func (p *Process) Start() error

Start runs the process in the background

func (*Process) StartAsTask

func (p *Process) StartAsTask(name string, opts ...task.Option) task.TypedTask[*Process]

StartAsTask creates and starts a Task for this Process with typed result handling

func (*Process) Stop

func (p *Process) Stop() error

func (*Process) Stream

func (p *Process) Stream(stdout, stderr io.Writer) *Process

func (*Process) Terminate

func (p *Process) Terminate() error

func (*Process) Wait

func (p *Process) Wait() error

func (*Process) WaitForStdout

func (p *Process) WaitForStdout(message string, timeout time.Duration) error

WaitForStdout waits for a specific message to appear in the process stdout This is useful for waiting for server startup messages like "Server started on port"

func (*Process) WithCwd

func (p *Process) WithCwd(cwd string) *Process

func (*Process) WithEnv

func (p *Process) WithEnv(env map[string]string) *Process

func (*Process) WithProcessGroup

func (p *Process) WithProcessGroup() *Process

WithProcessGroup spawns the subprocess in its own process group so the entire descendant tree can be terminated atomically via KillTree. Must be called before Run/Start. On POSIX this sets Setpgid=true; on Windows it sets CREATE_NEW_PROCESS_GROUP.

func (*Process) WithShell

func (p *Process) WithShell(shell string) *Process

func (*Process) WithTask

func (p *Process) WithTask(t *task.Task) *Process

func (*Process) WithTimeout

func (p *Process) WithTimeout(timeout time.Duration) *Process

func (*Process) WithoutShell

func (p *Process) WithoutShell() *Process

type WrapperFunc

type WrapperFunc func(...any) (*ExecResult, error)

WrapperFunc is a function type returned by AsWrapper that executes commands with pre-configured settings from a template Process.

type WrapperOption

type WrapperOption interface {
	// contains filtered or unexported methods
}

WrapperOption is a functional option that modifies a Process for a single execution.

func WithContext

func WithContext(ctx context.Context) WrapperOption

WithContext returns a WrapperOption that sets a context for cancellation/deadline. Note: Currently not fully implemented for context-based cancellation.

func WithDebug

func WithDebug() WrapperOption

func WithDir

func WithDir(path string) WrapperOption

WithDir returns a WrapperOption that overrides the working directory.

func WithEnv

func WithEnv(key, value string) WrapperOption

WithEnv returns a WrapperOption that adds or overrides an environment variable.

func WithLogger

func WithLogger(log logger.Logger) WrapperOption

func WithTee

func WithTee(stdout, stderr io.Writer) WrapperOption

func WithTimeout

func WithTimeout(timeout time.Duration) WrapperOption

WithTimeout returns a WrapperOption that overrides the execution timeout.

func WithoutErrorOnNonZero

func WithoutErrorOnNonZero() WrapperOption

WithoutErrorOnNonZero returns a WrapperOption that makes non-zero exit codes succeed

Jump to

Keyboard shortcuts

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