github.com/go-tstr/tstr/dep/cmd
go get github.com/go-tstr/tstr/dep/cmd@v0.2.5
Constants
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")
)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) errorStopWithSignal 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) *Cmdfunc (*Cmd) Ready
func (c *Cmd) Ready() errorfunc (*Cmd) Start
func (c *Cmd) Start() errorfunc (*Cmd) Stop
func (c *Cmd) Stop() errortype Opt
type Opt func(*Cmd) errorfunc WithArgsAppend
func WithArgsAppend(args ...string) OptWithArgsAppend adds arguments to commands current argument list.
func WithArgsSet
func WithArgsSet(args ...string) OptWithArgsSet sets arguments for the command.
func WithCommand
func WithCommand(name string, args ...string) OptWithCommand creates a new command with the given name and arguments.
func WithCommandFn
func WithCommandFn(fn func() (*exec.Cmd, error)) OptWithCommandFn 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) OptWithDir sets the working directory for the command.
func WithEnvAppend
func WithEnvAppend(env ...string) OptWithEnvAppend 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
func WithEnvSet(env ...string) OptWithEnvSet 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) OptWithExecCmd allows user to construct the command with custom exec.Cmd.
func WithGoCode
func WithGoCode(modulePath, mainPkg string) OptWithGoCode 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
func WithGoCover() OptWithGoCover calls WithGoCoverDir with the os.Getenv("GOCOVERDIR") value if it's set. Otherwise it's a no-op.
func WithGoCoverDir
func WithGoCoverDir(dir string) OptWithGoCoverDir 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) OptWithReadyFn allows user to provide custom readiness function. Given fn should block until the command is ready.
func WithReadyHTTP
func WithReadyHTTP(url string) OptWithReadyHTTP sets the ready function to wait for url to return 200 OK.
func WithReadyTimeout
func WithReadyTimeout(d time.Duration) OptWithReadyTimeout overrides the default 30s timeout for the ready function.
func WithStopFn
func WithStopFn(fn func(*exec.Cmd) error) OptWithStopFn allows user to provide custom stop function.
func WithWaitExit
func WithWaitExit() OptWithWaitExit 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) OptWithWaitMatchingLine sets the ready function so that it waits for the command to output a line that matches the given regular expression.