-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
75 lines (61 loc) · 1.39 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ovs
import (
"bytes"
"fmt"
"io"
"os/exec"
"strings"
log "github.com/Sirupsen/logrus"
)
type (
command struct {
command string
stdin io.Reader
stdout io.Writer
}
// Error is an error which is returned when the `zfs` or `zpool` shell
// commands return with a non-zero exit code.
cmdError struct {
Err error
Stderr string
}
)
// Error returns the string representation of an Error.
func (ce cmdError) Error() string {
return fmt.Sprintf("%s: %s", ce.Err, ce.Stderr)
}
func (c *command) Run(arg ...string) ([]string, error) {
cmd := exec.Command(c.command, arg...)
// Set up command stdin, stdout, stderr
var stdout, stderr bytes.Buffer
cmd.Stderr = &stderr
if c.stdout != nil {
cmd.Stdout = c.stdout
} else {
cmd.Stdout = &stdout
}
if c.stdin != nil {
cmd.Stdin = c.stdin
}
logFields := log.Fields{
"command": cmd.Path,
"args": cmd.Args,
}
log.WithFields(logFields).Debug("running command")
if err := cmd.Run(); err != nil {
e := &cmdError{
Err: err,
Stderr: stderr.String(),
}
log.WithFields(logFields).WithField("error", e).Error("command failed")
return nil, e
}
// If stdout was specified, processing is left entirely up to the caller
if c.stdout != nil {
return nil, nil
}
// Split output by lines then spaces
lines := strings.Split(stdout.String(), "\n")
// Remove empty last line
return lines[:len(lines)-1], nil
}