add configurable output

This commit is contained in:
Yann Hamon 2020-05-30 17:20:24 +02:00
parent 2786a84a4c
commit 8a25986e79
4 changed files with 106 additions and 18 deletions

45
pkg/output/json.go Normal file
View file

@ -0,0 +1,45 @@
package output
import (
"encoding/json"
"fmt"
)
type result struct {
Filename string `json:"filename"`
Status string `json:"status"`
Msg string `json:"msg"`
}
type JSONOutput struct {
results []result
}
func NewJSONOutput() Output{
return &JSONOutput{
results: []result{},
}
}
func (o *JSONOutput) Write(filename string,err error, skipped bool) {
status := "VALID"
msg := ""
if err != nil {
status = "ERROR"
msg = err.Error()
}
if skipped {
status = "SKIPPED"
}
o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg})
}
func (o *JSONOutput) Flush() {
res, err := json.MarshalIndent(o.results,"", " ")
if err != nil {
fmt.Printf("error print results: %s", err)
return
}
fmt.Printf("%s\n", res)
}

7
pkg/output/main.go Normal file
View file

@ -0,0 +1,7 @@
package output
type Output interface {
Write (filename string, err error, skipped bool)
Flush ()
}

37
pkg/output/text.go Normal file
View file

@ -0,0 +1,37 @@
package output
import (
"github.com/yannh/kubeconform/pkg/validator"
"log"
)
type TextOutput struct {
}
func NewTextOutput() Output {
return &TextOutput{}
}
func (o *TextOutput) Write(filename string,err error, skipped bool) {
if skipped {
log.Printf("skipping resource\n")
return
}
if err != nil {
if _, ok := err.(validator.InvalidResourceError); ok {
log.Printf("invalid resource: %s\n", err)
} else {
log.Printf("failed validating resource in file %s: %s\n", filename, err)
}
return
}
if !skipped{
log.Printf("file %s is valid\n", filename)
}
}
func (o *TextOutput) Flush() {
}