Documentation ¶
Overview ¶
Package exec runs external commands. It wraps os.StartProcess to make it easier to remap stdin and stdout, connect I/O with pipes, and do other adjustments.
Index ¶
- Variables
- func LookPath(file string) (string, os.Error)
- type Cmd
- func (c *Cmd) CombinedOutput() ([]byte, os.Error)
- func (c *Cmd) Output() ([]byte, os.Error)
- func (c *Cmd) Run() os.Error
- func (c *Cmd) Start() os.Error
- func (c *Cmd) StderrPipe() (io.ReadCloser, os.Error)
- func (c *Cmd) StdinPipe() (io.WriteCloser, os.Error)
- func (c *Cmd) StdoutPipe() (io.ReadCloser, os.Error)
- func (c *Cmd) Wait() os.Error
- type Error
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = os.NewError("executable file not found in $PATH")
ErrNotFound is the error resulting if a path search failed to find an executable file.
Functions ¶
Types ¶
type Cmd ¶
type Cmd struct { // Path is the path of the command to run. // // This is the only field that must be set to a non-zero // value. Path string // Args holds command line arguments, including the command as Args[0]. // If the Args field is empty or nil, Run uses {Path}. // // In typical use, both Path and Args are set by calling Command. Args []string // Env specifies the environment of the process. // If Env is nil, Run uses the current process's environment. Env []string // Dir specifies the working directory of the command. // If Dir is the empty string, Run runs the command in the // calling process's current directory. Dir string // Stdin specifies the process's standard input. // If Stdin is nil, the process reads from DevNull. Stdin io.Reader // Stdout and Stderr specify the process's standard output and error. // // If either is nil, Run connects the // corresponding file descriptor to /dev/null. // // If Stdout and Stderr are are the same writer, at most one // goroutine at a time will call Write. Stdout io.Writer Stderr io.Writer // SysProcAttr holds optional, operating system-specific attributes. // Run passes it to os.StartProcess as the os.ProcAttr's Sys field. SysProcAttr *syscall.SysProcAttr // Process is the underlying process, once started. Process *os.Process // contains filtered or unexported fields }
Cmd represents an external command being prepared or run.
func Command ¶
Command returns the Cmd struct to execute the named program with the given arguments.
It sets Path and Args in the returned structure and zeroes the other fields.
If name contains no path separators, Command uses LookPath to resolve the path to a complete name if possible. Otherwise it uses name directly.
The returned Cmd's Args field is constructed from the command name followed by the elements of arg, so arg should not include the command name itself. For example, Command("echo", "hello")
func (*Cmd) CombinedOutput ¶
CombinedOutput runs the command and returns its combined standard output and standard error.
func (*Cmd) Run ¶
Run starts the specified command and waits for it to complete.
The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.
If the command fails to run or doesn't complete successfully, the error is of type *os.Waitmsg. Other error types may be returned for I/O problems.
func (*Cmd) StderrPipe ¶
func (c *Cmd) StderrPipe() (io.ReadCloser, os.Error)
StderrPipe returns a pipe that will be connected to the command's standard error when the command starts. The pipe will be closed automatically after Wait sees the command exit.
func (*Cmd) StdinPipe ¶
func (c *Cmd) StdinPipe() (io.WriteCloser, os.Error)
StdinPipe returns a pipe that will be connected to the command's standard input when the command starts.
func (*Cmd) StdoutPipe ¶
func (c *Cmd) StdoutPipe() (io.ReadCloser, os.Error)
StdoutPipe returns a pipe that will be connected to the command's standard output when the command starts. The pipe will be closed automatically after Wait sees the command exit.
func (*Cmd) Wait ¶
Wait waits for the command to exit. It must have been started by Start.
The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.
If the command fails to run or doesn't complete successfully, the error is of type *os.Waitmsg. Other error types may be returned for I/O problems.