Documentation
¶
Overview ¶
Package cmd provides higher-level wrappers around os/exec.Cmd. All operations are thread-safe and designed to be used asynchronously by multiple goroutines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
Name string
Args []string
// --
*sync.Mutex
// contains filtered or unexported fields
}
Cmd represents an external command, similar to the Go built-in os/exec.Cmd. A Cmd cannot be reused after calling Start.
func NewCmd ¶
NewCmd creates a new Cmd for the given command name and arguments. The command is not started until Start is called.
func (*Cmd) Start ¶
Start starts the command and immediately returns a channel that the caller can use to receive the final Status of the command when it ends. The caller can start the command and wait like,
status := <-c.Start() // blocking
or start the command asynchronously and be notified later when it ends,
statusChan := c.Start() // non-blocking // do other stuff... status := <-statusChan // blocking
Either way, exactly one Status is sent on the channel when the command ends. The channel is not closed. Any error is set to Status.Error. Start is idempotent; it always returns the same channel.
type Status ¶
type Status struct {
Cmd string
PID int
Complete bool // false if stopped or signaled
Exit int // exit code of process
Error error // Go error
StartTs int64 // Unix ts (nanoseconds)
StopTs int64 // Unix ts (nanoseconds)
Runtime float64 // seconds
Stdout []string
Stderr []string
}
Status represents the status of a Cmd. It is valid during the entire lifecycle of the command. If StartTs > 0 (or PID > 0), the command has started. If StopTs > 0, the command has stopped. After the command has stopped, Exit = 0 is usually enough to indicate success, but complete success is indicated by:
Exit = 0 Error = nil Complete = true
If Complete is false, the command was stopped or timed out. Error is a Go error related to starting or running the command.