forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
48 lines (41 loc) · 1.11 KB
/
cmd.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
package util
import (
"io"
"os/exec"
)
// CommandOpts contains options to attach Stdout/err to a command to run
// or set its initial directory
type CommandOpts struct {
Stdout io.Writer
Stderr io.Writer
Dir string
}
// CommandRunner executes OS commands with the given parameters and options
type CommandRunner interface {
RunWithOptions(opts CommandOpts, name string, arg ...string) error
Run(name string, arg ...string) error
}
// NewCommandRunner creates a new instance of the default implementation of
// CommandRunner
func NewCommandRunner() CommandRunner {
return &runner{}
}
type runner struct{}
// RunWithOptions runs a command with the provided options
func (c *runner) RunWithOptions(opts CommandOpts, name string, arg ...string) error {
cmd := exec.Command(name, arg...)
if opts.Stdout != nil {
cmd.Stdout = opts.Stdout
}
if opts.Stderr != nil {
cmd.Stderr = opts.Stderr
}
if opts.Dir != "" {
cmd.Dir = opts.Dir
}
return cmd.Run()
}
// Run executes a command with default options
func (c *runner) Run(name string, arg ...string) error {
return c.RunWithOptions(CommandOpts{}, name, arg...)
}