command

package
v3.0.0-rc46 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package command provide utilities around os/exec package

Index

Constants

View Source
const DefaultWaitDelay = 5 * time.Second

DefaultWaitDelay is how long a command is given, after it has exited, for the output it wrote to reach this process.

It is the grace a well-behaved command never uses: what it wrote is already in the pipe when it exits, and the pipe reaches EOF as soon as the last holder of its write end is gone. The delay is for the command that hands a write end to something that outlives it, where EOF never comes at all.

Variables

View Source
var (
	ErrAlreadyStarted = errors.New("command: already started")
	ErrAlreadyWaited  = errors.New("command: already waited")
	ErrPromptAbort    = errors.New("command: aborted by prompt")
)

Functions

func CmdArgsFromString

func CmdArgsFromString(s string) ([]string, error)

CmdArgsFromString returns args for exec.Command from a string command 's' When string command 's' contains multiple commands,

exec.Command("/bin/sh", "-c", s)

else

exec.Command from shlex.Split(s)

func ShLimitCommands

func ShLimitCommands(l limits.T) string

ShLimitCommands provides ulimit commands for sh launcher max value of LimitVMem, LimitAs is used to set virtual memory limit

func WithArgs

func WithArgs(args []string) funcopt.O

WithArgs sets the process args[1:] from a string slice

func WithBufferedStderr

func WithBufferedStderr() funcopt.O

WithBufferedStderr activates the buffering of the lines emitted by the process on stderr

func WithBufferedStdout

func WithBufferedStdout() funcopt.O

WithBufferedStdout activates the buffering of the lines emitted by the process on stdout

func WithCWD

func WithCWD(cwd string) funcopt.O

func WithCommandLogLevel

func WithCommandLogLevel(l zerolog.Level) funcopt.O

WithCommandLogLevel show command name and args during Start

default zerolog.DebugLevel

func WithContext

func WithContext(ctx context.Context) funcopt.O

WithContext sets a custom context.Context for the command execution, using context.Background() if none is provided.

func WithEnv

func WithEnv(env []string) funcopt.O

func WithErrorExitCodeLogLevel

func WithErrorExitCodeLogLevel(l zerolog.Level) funcopt.O

WithErrorExitCodeLogLevel sets the level of the log entries for error exit code.

func WithGroup

func WithGroup(group string) funcopt.O

func WithIgnoredExitCodes

func WithIgnoredExitCodes(codes ...int) funcopt.O

WithIgnoredExitCodes set alternate list of successful exit codes.

exit codes are checked during Wait().
- default successful exit code is 0 when WithIgnoredExitCodes is not used
- Ignore all exit codes: WithIgnoredExitCodes()
- Accept 0, 1 or 6 exit code: WithIgnoredExitCodes(0, 1, 6)

func WithLogLevel

func WithLogLevel(l zerolog.Level) funcopt.O

WithLogLevel sets the level of the log entries.

func WithLogger

func WithLogger(l *plog.Logger) funcopt.O

WithLogger defines the Logger that will receive this pkg logs and process outputs

func WithName

func WithName(name string) funcopt.O

WithName sets the process args[0]

func WithOnStderrLine

func WithOnStderrLine(f func(string)) funcopt.O

func WithOnStdoutLine

func WithOnStdoutLine(f func(string)) funcopt.O

func WithProcessGroup

func WithProcessGroup() funcopt.O

WithProcessGroup runs the command in a process group of its own, which a cancel of its context kills as a whole: the processes the command started die with it, rather than outliving it holding its output.

func WithPrompt

func WithPrompt(l *bufio.Reader) funcopt.O

WithLogger defines the Logger that will receive this pkg logs and process outputs

func WithStderrLogLevel

func WithStderrLogLevel(l zerolog.Level) funcopt.O

WithStderrLogLevel sets the level of the log entries coming from the process stderr If not set, stderr lines are not logged.

func WithStdoutLogLevel

func WithStdoutLogLevel(l zerolog.Level) funcopt.O

WithStdoutLogLevel sets the level of the log entries coming from the process stdout. If not set, stdout lines are not logged.

func WithTimeout

func WithTimeout(timeout time.Duration) funcopt.O

WithTimeout sets the max duration the process is allowed to run. After this duration, the process is killed and an error is reported.

func WithUser

func WithUser(user string) funcopt.O

func WithVarArgs

func WithVarArgs(args ...string) funcopt.O

WithVarArgs sets the process args[1:] from a variadic string slice

func WithVarEnv

func WithVarEnv(env ...string) funcopt.O

func WithWaitDelay

func WithWaitDelay(d time.Duration) funcopt.O

WithWaitDelay bounds how long a command is given, after it has exited, for the output it wrote to reach this process.

Zero waits for the output pipes to reach EOF however long that takes, which is never when the command left something behind holding them open. The default is DefaultWaitDelay, and a command whose output is worth waiting longer for can say so.

Types

type ErrExitCode

type ErrExitCode struct {
	// contains filtered or unexported fields
}

func (*ErrExitCode) Error

func (e *ErrExitCode) Error() string

func (*ErrExitCode) ExitCode

func (e *ErrExitCode) ExitCode() int

type T

type T struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...funcopt.O) *T

func (*T) Cmd

func (t *T) Cmd() *exec.Cmd

func (*T) ExitCode

func (t *T) ExitCode() int

func (*T) NormalizedExitCode

func (t *T) NormalizedExitCode() int

NormalizedExitCode returns the process exit status, and 128 + the signal number when the process was terminated by one, which is the shell convention and the value this package's own error and log entries already carry.

ExitCode returns the raw os/exec value, which is -1 for a signaled process and says nothing about which signal. A caller comparing against a command's documented codes wants that one. A caller reporting the outcome to a person wants this one, and wants it to agree with the error text beside it.

It is -1 when the process never ran, which is the one case no exit status exists for.

func (*T) Output

func (t *T) Output() ([]byte, error)

Output returns stdout results of command (meaningful after Wait() or Run()), command created without funcopt WithBufferedStdout() return nil valid results

func (*T) Run

func (t *T) Run() error

func (*T) Start

func (t *T) Start() (err error)

Start prepare command, then call underlying cmd.Start() it takes care of preparing logging, timeout, stdout and stderr watchers

func (*T) Stderr

func (t *T) Stderr() []byte

Stderr returns stderr results of command (meaningful after Wait() or Run()) command created without funcopt WithBufferedStderr() return nil

func (*T) Stdout

func (t *T) Stdout() []byte

Stdout returns stdout results of command (meaningful after Wait() or Run()), command created without funcopt WithBufferedStdout() return nil valid results

func (*T) String

func (t *T) String() string

func (*T) Wait

func (t *T) Wait() error

Jump to

Keyboard shortcuts

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