-
Notifications
You must be signed in to change notification settings - Fork 162
/
results_writer.go
96 lines (74 loc) · 2.27 KB
/
results_writer.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
package ssh
import (
"bytes"
"fmt"
"io"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
boshtbl "github.com/cloudfoundry/bosh-cli/v7/ui/table"
)
type ResultsWriter struct {
ui boshui.UI
instances []*resultsInstanceWriter
}
func NewResultsWriter(ui boshui.UI) *ResultsWriter {
return &ResultsWriter{ui: ui}
}
func (w *ResultsWriter) ForInstance(jobName, indexOrID string) InstanceWriter {
w.instances = append(w.instances, newBufferedInstanceWriter(jobName, indexOrID))
return w.instances[len(w.instances)-1]
}
func (w *ResultsWriter) Flush() {
table := boshtbl.Table{
Content: "results",
Header: []boshtbl.Header{
boshtbl.NewHeader("Instance"),
boshtbl.NewHeader("Stdout"),
boshtbl.NewHeader("Stderr"),
boshtbl.NewHeader("Exit Code"),
boshtbl.NewHeader("Error"),
},
SortBy: []boshtbl.ColumnSort{
{Column: 0, Asc: true},
},
Transpose: true,
}
for _, inst := range w.instances {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueString(inst.Instance()),
boshtbl.NewValueString(inst.StdoutAsString()),
boshtbl.NewValueString(inst.StderrAsString()),
boshtbl.NewValueInt(inst.ExitStatus()),
boshtbl.NewValueError(inst.Error()),
})
}
w.ui.PrintTable(table)
}
type resultsInstanceWriter struct {
jobName string
indexOrID string
stdout *bytes.Buffer
stderr *bytes.Buffer
exitStatus int
error error
}
func newBufferedInstanceWriter(jobName, indexOrID string) *resultsInstanceWriter {
return &resultsInstanceWriter{
jobName: jobName,
indexOrID: indexOrID,
stdout: bytes.NewBufferString(""),
stderr: bytes.NewBufferString(""),
}
}
func (w *resultsInstanceWriter) Instance() string {
return fmt.Sprintf("%s/%s", w.jobName, w.indexOrID)
}
func (w *resultsInstanceWriter) Stdout() io.Writer { return w.stdout }
func (w *resultsInstanceWriter) StdoutAsString() string { return w.stdout.String() }
func (w *resultsInstanceWriter) Stderr() io.Writer { return w.stderr }
func (w *resultsInstanceWriter) StderrAsString() string { return w.stderr.String() }
func (w *resultsInstanceWriter) End(exitStatus int, err error) {
w.exitStatus = exitStatus
w.error = err
}
func (w *resultsInstanceWriter) ExitStatus() int { return w.exitStatus }
func (w *resultsInstanceWriter) Error() error { return w.error }