mirror of
https://github.com/rancher/os.git
synced 2025-07-06 19:38:37 +00:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
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
|
|
}
|