-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
168 lines (138 loc) · 3.55 KB
/
output.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package shell
// The structs and functions in this file are almost exactly the same as the version in terratest
// (https://github.com/gruntwork-io/terratest/blob/37812f27666423c28ea22acb2bac2c80513dd318/modules/shell/output.go),
// except this version does not trim newlines from the streamed and captured texts. This ensures that the newlines
// reflect exactly how the underlying shell commands outputted. Otherwise, the newline is always stripped out on the
// last line, regardless of if the original command included it. That final terminating newline is more significant in
// production CLI usage vs testing purposes.
import (
"bufio"
"io"
"strings"
"sync"
"github.com/sirupsen/logrus"
)
// Output contains the output after runnig a command.
type Output struct {
stdout *outputStream
stderr *outputStream
// merged contains stdout and stderr merged into one stream.
merged *merged
}
func newOutput() *Output {
m := new(merged)
return &Output{
merged: m,
stdout: &outputStream{
merged: m,
},
stderr: &outputStream{
merged: m,
},
}
}
func (o *Output) Stdout() string {
if o == nil {
return ""
}
return o.stdout.String()
}
func (o *Output) Stderr() string {
if o == nil {
return ""
}
return o.stderr.String()
}
func (o *Output) Combined() string {
if o == nil {
return ""
}
return o.merged.String()
}
type outputStream struct {
Lines []string
*merged
}
func (st *outputStream) WriteString(s string) (n int, err error) {
st.Lines = append(st.Lines, string(s))
return st.merged.WriteString(s)
}
func (st *outputStream) String() string {
if st == nil {
return ""
}
return strings.Join(st.Lines, "")
}
type merged struct {
// ensure that there are no parallel writes
sync.Mutex
Lines []string
}
func (m *merged) String() string {
if m == nil {
return ""
}
return strings.Join(m.Lines, "")
}
func (m *merged) WriteString(s string) (n int, err error) {
m.Lock()
defer m.Unlock()
m.Lines = append(m.Lines, string(s))
return len(s), nil
}
// This function captures stdout and stderr into the given variables while still printing it to the stdout and stderr
// of this Go program.
// This is almost exactly the same as
// https://github.com/gruntwork-io/terratest/blob/37812f27666423c28ea22acb2bac2c80513dd318/modules/shell/command.go#L130,
// except it uses a different logger.
func readStdoutAndStderr(log *logrus.Logger, streamOutput bool, stdout, stderr io.ReadCloser) (*Output, error) {
out := newOutput()
stdoutReader := bufio.NewReader(stdout)
stderrReader := bufio.NewReader(stderr)
wg := &sync.WaitGroup{}
wg.Add(2)
var stdoutErr, stderrErr error
go func() {
defer wg.Done()
stdoutErr = readData(log, streamOutput, stdoutReader, out.stdout)
}()
go func() {
defer wg.Done()
stderrErr = readData(log, streamOutput, stderrReader, out.stderr)
}()
wg.Wait()
if stdoutErr != nil {
return out, stdoutErr
}
if stderrErr != nil {
return out, stderrErr
}
return out, nil
}
func readData(log *logrus.Logger, streamOutput bool, reader *bufio.Reader, writer io.StringWriter) error {
var line string
var readErr error
for {
line, readErr = reader.ReadString('\n')
// only return early if the line does not have
// any contents. We could have a line that does
// not not have a newline before io.EOF, we still
// need to add it to the output.
if len(line) == 0 && readErr == io.EOF {
break
}
if streamOutput {
log.Println(line)
}
if _, err := writer.WriteString(line); err != nil {
return err
}
if readErr != nil {
break
}
}
if readErr != io.EOF {
return readErr
}
return nil
}