-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
json.go
40 lines (34 loc) · 851 Bytes
/
json.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
package report
import (
"encoding/json"
"fmt"
"io"
"golang.org/x/xerrors"
)
type JSONWriter struct {
Output io.Writer
Report string
}
// Write writes the results in JSON format
func (jw JSONWriter) Write(report Report) error {
var output []byte
var err error
switch jw.Report {
case AllReport:
output, err = json.MarshalIndent(report, "", " ")
if err != nil {
return xerrors.Errorf("failed to write json: %w", err)
}
case SummaryReport:
output, err = json.MarshalIndent(report.consolidate(), "", " ")
if err != nil {
return xerrors.Errorf("failed to write json: %w", err)
}
default:
return xerrors.Errorf(`report %q not supported. Use "summary" or "all"`, jw.Report)
}
if _, err = fmt.Fprintln(jw.Output, string(output)); err != nil {
return xerrors.Errorf("failed to write json: %w", err)
}
return nil
}