-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
55 lines (45 loc) · 1.12 KB
/
run.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
// run.go contains the interface to os.exec with a mockable object.
package main
import (
"io"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
type runner interface {
run(string, ...string) error
}
// runner provides a simple and mockable interface to exec.Command()
type runCommand int
// run creates an *exec.Cmd object using exec.Command and runs
// it using exec.Run. The return is the return of exec.Run.
func (x runCommand) run(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
_, _ = io.Copy(os.Stdout, stdout)
_, _ = io.Copy(os.Stderr, stderr)
return cmd.Wait()
}
// fakeRunCommand provides a runner for dry-run operations.
type fakeRunCommand int
// Fakerunner just logs the commands (dry-run)
func (x fakeRunCommand) run(name string, args ...string) error {
var quoted []string
for _, a := range args {
quoted = append(quoted, strconv.Quote(a))
}
log.Printf("%q %s", name, strings.Join(quoted, " "))
return nil
}