12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package exec
- import (
- "bytes"
- "errors"
- )
- // Below is largely a copy from go's os/exec/exec.go
- // 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 *ExitError. Other error types may be
- // returned for I/O problems.
- func (c *Cmd) Run() error {
- if err := c.Start(); err != nil {
- return translateError(err)
- }
- return translateError(c.Wait())
- }
- // Output runs the command and returns its standard output.
- // Any returned error will usually be of type *ExitError.
- // If c.Stderr was nil, Output populates ExitError.Stderr.
- func (c *Cmd) Output() ([]byte, error) {
- if c.Stdout != nil {
- return nil, errors.New("exec: Stdout already set")
- }
- var stdout bytes.Buffer
- c.Stdout = &stdout
- err := c.Run()
- return stdout.Bytes(), err
- }
- // CombinedOutput runs the command and returns its combined standard
- // output and standard error.
- func (c *Cmd) CombinedOutput() ([]byte, error) {
- if c.Stdout != nil {
- return nil, errors.New("exec: Stdout already set")
- }
- if c.Stderr != nil {
- return nil, errors.New("exec: Stderr already set")
- }
- var b bytes.Buffer
- c.Stdout = &b
- c.Stderr = &b
- err := c.Run()
- return b.Bytes(), err
- }
|