Documentation
¶
Index ¶
- func ContainsShellOperators(cmd string) bool
- func NewDebugLogger(log logger.Logger, level logger.LogLevel) logger.Logger
- type ExecLogger
- func (e *ExecLogger) GetOutput() string
- func (e *ExecLogger) GetStderr() string
- func (l *ExecLogger) GetStderrWriter() (writer io.Writer)
- func (e *ExecLogger) GetStdout() string
- func (l *ExecLogger) GetStdoutWriter() (writer io.Writer)
- func (l *ExecLogger) Reset()
- func (l *ExecLogger) Tee(stdout, stderr io.Writer) *ExecLogger
- type ExecResult
- type Process
- func (p *Process) AsWrapper() WrapperFunc
- func (p *Process) Debug() *Process
- func (p *Process) ExitCode() int
- func (p *Process) ForceKill() error
- func (p *Process) GetOutput() string
- func (p *Process) GetStderr() string
- func (p *Process) GetStdout() string
- func (p *Process) GetTask() *task.Task
- func (p *Process) IsOK() bool
- func (p *Process) IsRunning() bool
- func (p *Process) Kill(timeout time.Duration) error
- func (p *Process) KillTree() error
- func (p *Process) MustStop(timeout time.Duration) error
- func (p *Process) Name() string
- func (p *Process) Out() string
- func (p *Process) Pid() int
- func (p *Process) Pretty() api.Text
- func (p *Process) Result() *ExecResult
- func (p *Process) Run() *Process
- func (p *Process) RunAsTask(name string, opts ...task.Option) task.TypedTask[ExecResult]
- func (p *Process) Short() api.Text
- func (p *Process) Start() error
- func (p *Process) StartAsTask(name string, opts ...task.Option) task.TypedTask[*Process]
- func (p *Process) Stop() error
- func (p *Process) Stream(stdout, stderr io.Writer) *Process
- func (p *Process) Terminate() error
- func (p *Process) Wait() error
- func (p *Process) WaitForStdout(message string, timeout time.Duration) error
- func (p *Process) WithCwd(cwd string) *Process
- func (p *Process) WithEnv(env map[string]string) *Process
- func (p *Process) WithProcessGroup() *Process
- func (p *Process) WithShell(shell string) *Process
- func (p *Process) WithTask(t *task.Task) *Process
- func (p *Process) WithTimeout(timeout time.Duration) *Process
- func (p *Process) WithoutShell() *Process
- type WrapperFunc
- type WrapperOption
- func WithContext(ctx context.Context) WrapperOption
- func WithDebug() WrapperOption
- func WithDir(path string) WrapperOption
- func WithEnv(key, value string) WrapperOption
- func WithLogger(log logger.Logger) WrapperOption
- func WithTee(stdout, stderr io.Writer) WrapperOption
- func WithTimeout(timeout time.Duration) WrapperOption
- func WithoutErrorOnNonZero() WrapperOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsShellOperators ¶
ContainsShellOperators checks if a command contains shell-specific operators that require wrapping in a shell (bash -c or sh -c)
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 (*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) KillTree ¶
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 ¶
MustStop attempts to gracefully stop a process, after which it is forcefully killed
func (*Process) Pid ¶
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) Result ¶
func (p *Process) Result() *ExecResult
func (*Process) RunAsTask ¶
StartAsTask creates and starts a Task for this Process with typed result handling
func (*Process) StartAsTask ¶
StartAsTask creates and starts a Task for this Process with typed result handling
func (*Process) WaitForStdout ¶
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) WithProcessGroup ¶
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) WithoutShell ¶
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