forked from open-policy-agent/conftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
64 lines (50 loc) · 1.67 KB
/
table.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
package output
import (
"fmt"
"io"
"github.com/olekukonko/tablewriter"
"github.com/open-policy-agent/opa/tester"
)
// Table represents an Outputter that outputs
// results in a tabular format.
type Table struct {
Writer io.Writer
}
// NewTable creates a new Table with the given writer.
func NewTable(w io.Writer) *Table {
table := Table{
Writer: w,
}
return &table
}
// Output outputs the results.
func (t *Table) Output(checkResults []CheckResult) error {
table := tablewriter.NewWriter(t.Writer)
table.SetHeader([]string{"result", "file", "namespace", "message"})
var tableData [][]string
for _, checkResult := range checkResults {
for r := 0; r < checkResult.Successes; r++ {
tableData = append(tableData, []string{"success", checkResult.FileName, checkResult.Namespace, "SUCCESS"})
}
for _, result := range checkResult.Exceptions {
tableData = append(tableData, []string{"exception", checkResult.FileName, checkResult.Namespace, result.Message})
}
for _, result := range checkResult.Warnings {
tableData = append(tableData, []string{"warning", checkResult.FileName, checkResult.Namespace, result.Message})
}
for _, result := range checkResult.Skipped {
tableData = append(tableData, []string{"skipped", checkResult.FileName, checkResult.Namespace, result.Message})
}
for _, result := range checkResult.Failures {
tableData = append(tableData, []string{"failure", checkResult.FileName, checkResult.Namespace, result.Message})
}
}
if len(tableData) > 0 {
table.AppendBulk(tableData)
table.Render()
}
return nil
}
func (t *Table) Report(_ []*tester.Result, _ string) error {
return fmt.Errorf("report is not supported in table output")
}