-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
157 lines (137 loc) · 3.38 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"sync"
)
// OutputWriterCollection holds and manages a collection of OutputWriters
// It is thread safe and can construct output from many OutputWriters
type OutputWriterCollection struct {
writers []OutputWriter
mutex *sync.Mutex
}
func NewOutputWriterCollection() *OutputWriterCollection {
return &OutputWriterCollection{
writers: make([]OutputWriter, 0),
mutex: &sync.Mutex{},
}
}
// Add adds a OutputWriter to the OutputWriter collection
func (owc *OutputWriterCollection) Add(ow OutputWriter) {
owc.mutex.Lock()
owc.writers = append(owc.writers, ow)
owc.mutex.Unlock()
}
// Flush flushes the OutputWriters to Stdout
func (owc *OutputWriterCollection) Flush() error {
if JsonFlag {
output := make([]json.RawMessage, 0)
for _, ow := range owc.writers {
flushed := json.RawMessage(ow.Flush())
output = append(output, flushed)
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
return encoder.Encode(output)
} else {
output := make([]byte, 0)
for _, ow := range owc.writers {
flushed := ow.Flush()
output = append(output, flushed...)
}
fmt.Fprint(os.Stdout, string(output))
return nil
}
}
type OutputWriter interface {
Flush() []byte
io.Writer
}
func CreateOutputWriter(target string) OutputWriter {
if JsonFlag {
return NewJsonOutputWriter(target)
} else {
return NewStdOutputWriter(target)
}
}
var _ OutputWriter = &JsonOutputWriter{}
var _ OutputWriter = &StdOutputWriter{}
type StdOutputWriter struct {
buf *bytes.Buffer
target string
}
func NewStdOutputWriter(target string) *StdOutputWriter {
return &StdOutputWriter{
buf: &bytes.Buffer{},
target: target,
}
}
// Write implements the io.Writer interface
func (o *StdOutputWriter) Write(p []byte) (n int, err error) {
return o.buf.Write(p)
}
// Flush flushes the OutputWriter to a json.RawMessage
func (o *StdOutputWriter) Flush() []byte {
prefix := fmt.Sprintf("#target:%v\n", o.target)
return append([]byte(prefix), o.buf.Bytes()...)
}
type JsonOutputWriter struct {
buf *bytes.Buffer
target string
exitCode int
}
// NewJsonOutputWriter creates a new OutputWriter generating JSON output
// from a Command
func NewJsonOutputWriter(target string) *JsonOutputWriter {
return &JsonOutputWriter{
buf: &bytes.Buffer{},
target: target,
exitCode: 0,
}
}
// Write implements the io.Writer interface
func (o *JsonOutputWriter) Write(p []byte) (n int, err error) {
return o.buf.Write(p)
}
// Flush flushes the OutputWriter to a json.RawMessage
func (o *JsonOutputWriter) Flush() []byte {
bytes := o.buf.Bytes()
c := map[string]interface{}{}
// We need to determine if the output is a JSON object or just bytes
err := json.Unmarshal(bytes, &c)
if err == nil {
s := struct {
Target string `json:"target"`
Output json.RawMessage `json:"output"`
ExitCode int `json:"exitCode"`
}{
Target: o.target,
Output: json.RawMessage(bytes),
ExitCode: o.exitCode,
}
buf, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
return buf
} else {
s := struct {
Target string `json:"target"`
Output string `json:"output"`
ExitCode int `json:"exitCode"`
}{
Target: o.target,
Output: string(bytes),
ExitCode: o.exitCode,
}
buf, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
return buf
}
}