adding option for JSON output

This commit is contained in:
Ben Hirschberg
2021-08-25 17:37:49 +03:00
parent ce908aa748
commit b97f7d6e36
2 changed files with 26 additions and 5 deletions

View File

@@ -42,7 +42,7 @@ func CliSetup() error {
reporterObj := opaprocessor.NewOPAProcessor(&processNotification, &reportResults)
reporterObj.ProcessRulesListenner()
}()
p := printer.NewPrinter(&reportResults)
p := printer.NewPrinter(&reportResults, printer.PrettyPrinter)
p.ActionPrint()
return nil

View File

@@ -1,6 +1,7 @@
package printer
import (
"encoding/json"
"fmt"
"kube-escape/cautils"
"os"
@@ -15,16 +16,24 @@ import (
var INDENT = " "
const (
PrettyPrinter string = "pretty-printer"
JsonPrinter string = "json-printer"
JunitResultPrinter string = "junit-result-printer"
)
type Printer struct {
opaSessionObj *chan *cautils.OPASessionObj
summary Summary
sortedControlNames []string
printerType string
}
func NewPrinter(opaSessionObj *chan *cautils.OPASessionObj) *Printer {
func NewPrinter(opaSessionObj *chan *cautils.OPASessionObj, printerType string) *Printer {
return &Printer{
opaSessionObj: opaSessionObj,
summary: NewSummery(),
printerType: printerType,
}
}
@@ -33,9 +42,21 @@ func (printer *Printer) ActionPrint() {
for {
opaSessionObj := <-*printer.opaSessionObj
printer.SummerySetup(opaSessionObj.PostureReport)
printer.PrintResults()
printer.PrintSummaryTable()
if printer.printerType == PrettyPrinter {
printer.SummerySetup(opaSessionObj.PostureReport)
printer.PrintResults()
printer.PrintSummaryTable()
} else if printer.printerType == JsonPrinter {
postureReportStr, err := json.Marshal(opaSessionObj.PostureReport)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
}
os.Stdout.Write(postureReportStr)
} else {
fmt.Println("unknown output printer")
os.Exit(1)
}
if !k8sinterface.RunningIncluster {
break