Package cmd

import ""

Overview ▾

Package cmd provides convenience types and functions for running shell commands in Go. It also includes a mechanism for running commands in parallel using a pool of workers.

type Command

type Command struct {
    BufStdin, BufStdout, BufStderr *bytes.Buffer

Command embeds a exec.Cmd but also includes buffers for stdin, stdout and stderr. These buffers are automatically attached when "New" is called.

func New

func New(name string, arg ...string) *Command

New creates a new pointer to a Command. Byte buffers are created and attached to the command's Stdin, Stdout and Stderr.

func (*Command) Run

func (cmd *Command) Run() error

Run calls (*exec.Cmd).Run on the embedded command. If (*exec.Cmd).Run returns an error, then Run will also return the error. But Run also checks the stderr buffer, and if it isn't empty, an error is returned with the contents of stderr (only on non-zero exit code).

func (*Command) String

func (cmd *Command) String() string

func (*Command) Wait

func (cmd *Command) Wait() error

Wait calls (*exec.Cmd).Wait on the embedded command and handles errors as described in Run(). Note that you may call (*Command).Start() since the Command type embeds a *exec.Cmd type.

type Commander

type Commander interface {
    Run() error

Commands allows any kind of command with a "Run() error" method to be used with the pool. (i.e., you aren't forced to use this packages Command type.)

type Commands

type Commands []Commander

Commands is a list of values that implement the Commander interface. This is used as the list of commands to be executed in a pool.

func NewCmds

func NewCmds(cmds []*exec.Cmd) Commands

NewCmds is a convenience function for creating a list of Commanders from a list of *exec.Cmd.

func NewCommands

func NewCommands(cmds []*Command) Commands

NewCommands is a convenience function for creating a list of Commanders from a list of *Command.

func (Commands) RunMany

func (cmds Commands) RunMany(workers int) []error

RunMany creates a pool with a number of workers specified by "workers". If "workers" is less than 1, then the value of GOMAXPROCS is used. Every command in "cmds" is executed once by a single worker. A list of errors corresponding to the list of 'cmds' is returned, where the length of the list of errors is always equivalent to the length of 'cmds'.

A convenient way to use this method, given a list of *Command:

errs := NewCommands(commands).RunMany(0)