cmd

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMissingCmd     = strerr.Error("missing command")
	ErrStartFailed    = strerr.Error("failed to start command")
	ErrReadyFailed    = strerr.Error("failed to verify readiness")
	ErrStopFailed     = strerr.Error("command didn't stop successfully")
	ErrOptApply       = strerr.Error("failed apply Opt")
	ErrNoMatchingLine = strerr.Error("no matching line found")
	ErrNilCmdRegexp   = strerr.Error("command has to be set before this option can be applied, check the order of options")
	ErrPreCmdFailed   = strerr.Error("pre command failed")
	ErrBadRegexp      = strerr.Error("bad regular expression for matching line")
	ErrOutputPipe     = strerr.Error("failed to acquire output pipe for command")
	ErrBuildFailed    = strerr.Error("failed to build go binary")
	ErrCreateCoverDir = strerr.Error("failed create coverage dir")
)

Variables

This section is empty.

Functions

func MatchingLine

func MatchingLine(exp string, cmd *exec.Cmd) (func(context.Context, *exec.Cmd) error, error)

MatchLine waits for the command to output a line that matches the given regular expression.

func StopWithSignal

func StopWithSignal(s os.Signal) func(*exec.Cmd) error

StopWithSignal returns a stop function that sends the given signal to the command and waits for it to exit. This can be used with WithStopFn to stop the command with a specific signal.

Types

type Cmd

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

func New

func New(opts ...Opt) *Cmd

func (*Cmd) Ready

func (c *Cmd) Ready() error

func (*Cmd) Start

func (c *Cmd) Start() error

func (*Cmd) Stop

func (c *Cmd) Stop() error

type Opt

type Opt func(*Cmd) error

func WithArgsAppend added in v0.1.2

func WithArgsAppend(args ...string) Opt

WithArgsAppend adds arguments to commands current argument list.

func WithArgsSet added in v0.1.2

func WithArgsSet(args ...string) Opt

WithArgsSet sets arguments for the command.

func WithCommand

func WithCommand(name string, args ...string) Opt

WithCommand creates a new command with the given name and arguments.

func WithCommandFn added in v0.2.0

func WithCommandFn(fn func() (*exec.Cmd, error)) Opt

WithCommandFn creates a new command using the given function. This is useful for lazy loading of the command and using arguments from other dependencies.

func WithDir

func WithDir(dir string) Opt

WithDir sets the working directory for the command.

func WithEnvAppend

func WithEnvAppend(env ...string) Opt

WithEnvAppend adds environment variables to commands current env. By default the command inherits the environment of the current process and setting this option will override it.

func WithEnvSet added in v0.0.2

func WithEnvSet(env ...string) Opt

WithEnvSet sets environment variables for the command. By default the command inherits the environment of the current process and setting this option will override it.

func WithExecCmd

func WithExecCmd(cmd *exec.Cmd) Opt

WithExecCmd allows user to construct the command with custom exec.Cmd.

func WithGoCode

func WithGoCode(modulePath, mainPkg string) Opt

WithGoCode builds the given Go projects and sets the main package as the command. By default the output binary is instrumented to collect coverage data Working directory for build command is set to modulePath which means that the mainPkg should be relative to it. Building the binary is done in a separate goroutine and the command is started only after the build is finished. Also building is done only once which allows to reuse the reusing the same Cmd instance without rebuilding the binary.

func WithGoCover added in v0.1.1

func WithGoCover() Opt

WithGoCover calls WithGoCoverDir with the os.Getenv("GOCOVERDIR") value if it's set. Otherwise it's a no-op.

func WithGoCoverDir added in v0.0.2

func WithGoCoverDir(dir string) Opt

WithGoCoverDir creates the dir if it doesn't exist and appends the GOCOVERDIR env variable into the commands env.

func WithReadyFn

func WithReadyFn(fn func(context.Context, *exec.Cmd) error) Opt

WithReadyFn allows user to provide custom readiness function. Given fn should block until the command is ready.

func WithReadyHTTP

func WithReadyHTTP(url string) Opt

WithReadyHTTP sets the ready function to wait for url to return 200 OK.

func WithReadyTimeout

func WithReadyTimeout(d time.Duration) Opt

WithReadyTimeout overrides the default 30s timeout for the ready function.

func WithStopFn

func WithStopFn(fn func(*exec.Cmd) error) Opt

WithStopFn allows user to provide custom stop function.

func WithWaitExit

func WithWaitExit() Opt

WithWaitExit sets the ready and stop functions so that ready waits for the command to exit successfully and stop returns nil immediately. This is useful for commands that exit on their own and don't need to be stopped manually.

func WithWaitMatchingLine

func WithWaitMatchingLine(exp string) Opt

WithWaitMatchingLine sets the ready function so that it waits for the command to output a line that matches the given regular expression.

Jump to

Keyboard shortcuts

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