-
Notifications
You must be signed in to change notification settings - Fork 151
/
runner.go
109 lines (83 loc) · 2.39 KB
/
runner.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package runner
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
"sync"
"github.com/pkg/errors"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
// Runner is an entity which can execute commands with whatever backing medium behind it.
//counterfeiter:generate . Runner
type Runner interface {
// Run takes a command name and some arguments and will apply it to the current environment.
Run(command string, args ...string) ([]byte, error)
// RunWithStream takes a command name and some argument and outputs the command in realtime to stdout
RunWithOutputStream(command string, args ...string) ([]byte, error)
// RunWithStdin take a command name, arguments and passes stdin data to the command
RunWithStdin(command string, args []string, stdinData []byte) ([]byte, error)
}
// CLIRunner will use exec.Command as runtime medium.
type CLIRunner struct{}
// Run will use exec.Command as a backing medium and run CombinedOutput.
func (*CLIRunner) Run(c string, args ...string) ([]byte, error) {
cmd := exec.Command(c, args...)
return cmd.CombinedOutput()
}
// Run will use exec.Command as a backing medium and run CombinedOutput.
func (*CLIRunner) RunWithOutputStream(c string, args ...string) ([]byte, error) {
cmd := exec.Command(c, args...)
var out strings.Builder
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderrReader, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
var wg sync.WaitGroup
stdoutScanner := bufio.NewScanner(stdoutReader)
wg.Add(1)
go func() {
defer wg.Done()
for stdoutScanner.Scan() {
data := stdoutScanner.Text()
fmt.Println(data)
out.WriteString(data)
out.WriteRune('\n')
}
}()
stderrScanner := bufio.NewScanner(stderrReader)
wg.Add(1)
go func() {
defer wg.Done()
for stderrScanner.Scan() {
data := stderrScanner.Text()
fmt.Println(data)
out.WriteString(data)
out.WriteRune('\n')
}
}()
err = cmd.Start()
if err != nil {
return nil, err
}
wg.Wait()
err = cmd.Wait()
return []byte(out.String()), err
}
func (*CLIRunner) RunWithStdin(c string, args []string, stdinData []byte) ([]byte, error) {
cmd := exec.Command(c, args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return []byte{}, errors.Wrap(err, "failed to get stdinpipe")
}
go func() {
defer stdin.Close()
_, _ = io.WriteString(stdin, string(stdinData))
}()
return cmd.CombinedOutput()
}