run

package module
v0.0.0-...-31429b3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2017 License: MIT Imports: 3 Imported by: 1

README

go-cmd/Run

Strategies for running commands with a uniform interface.

Documentation

Overview

Package run provides strategies for running command with a uniform interface. This packages requires github.com/go-cmd/cmd.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRunning is returned by a Runner if its Run method is called while still running.
	ErrRunning = errors.New("already running")

	// ErrStopped is returned by a Runner if its Stop method is called before Run finishes.
	ErrStopped = errors.New("Stop called")

	// ErrNonzeroExit is returned by a Runner if a Cmd returns a non-zero exit code.
	ErrNonzeroExit = errors.New("non-zero exit")
)

Functions

This section is empty.

Types

type Factory

type Factory func(strategy string) Runner

A Factory makes a Runner for the given strategy. This is useful for testing: mock runners can be returned instead of real runners when testing to avoid running real commands.

type RunSync

type RunSync struct {

	// --
	*sync.Mutex
	// contains filtered or unexported fields
}

RunSync is a Runner that runs commands synchronously in the order given. No timeouts or retries are used. It's the simplest possible Runner which expects all commands to be quick and reliable.

func NewRunSync

func NewRunSync(stopOnError bool) *RunSync

NewRunSync creates a new RunSync. If stopOnError is true, Run stops immediately and returns ErrNonzeroExit when a command exits non-zero.

func (*RunSync) Run

func (r *RunSync) Run(cmds []cmd.Cmd) error

Run runs the list of Cmd and waits for them to complete, or one of them to exit non-zero if the runner was created with stopOnError = true.

func (*RunSync) Status

func (r *RunSync) Status() ([]cmd.Status, int)

func (*RunSync) Stop

func (r *RunSync) Stop() error

Stop stops Run if Run is still running. The return error is from stopping the currently active Process, if any. Stop is idempotent.

type Runner

type Runner interface {
	Run([]cmd.Cmd) error
	Stop() error
	Status() ([]cmd.Status, int)
}

A Runner runs a list of commands. The interface is intentionally trivial because the real benefit lies in its implementation. For example, RunSync runs commands synchronously in the order given without retries or timeouts. Another implementation could implement a more complex strategy, for example, running commands in parallel. The implementation details are hidden from and irrelevant to the caller, which allows the caller to focus on running commands, not how they are ran.

Jump to

Keyboard shortcuts

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