better logic mgmt in output plugins, go fmt

This commit is contained in:
Yann Hamon 2020-05-31 02:10:19 +02:00
parent 224e9ca17d
commit 8eb297d4c4
11 changed files with 93 additions and 79 deletions

View file

@ -3,45 +3,44 @@ package output
import (
"encoding/json"
"fmt"
"github.com/yannh/kubeconform/pkg/validator"
)
type result struct {
Filename string `json:"filename"`
Status string `json:"status"`
Msg string `json:"msg"`
Filename string `json:"filename"`
Status string `json:"status"`
Msg string `json:"msg"`
}
type JSONOutput struct {
withSummary bool
results []result
results []result
}
func NewJSONOutput(withSummary bool) Output{
func NewJSONOutput(withSummary bool) Output {
return &JSONOutput{
withSummary: withSummary,
results: []result{},
results: []result{},
}
}
func (o *JSONOutput) Write(filename string,err error, skipped bool) {
status := "VALID"
msg := ""
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
msg, st := "", ""
if err != nil {
s := status(err, skipped)
switch {
case s == VALID:
st = "VALID"
case s == INVALID:
st = "INVALID"
msg = err.Error()
if _, ok := err.(validator.InvalidResourceError); ok {
status = "INVALID"
} else {
status = "ERROR"
}
case s == ERROR:
st = "ERROR"
msg = err.Error()
case s == SKIPPED:
st = "SKIPPED"
}
if skipped {
status = "SKIPPED"
}
o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg})
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
}
func (o *JSONOutput) Flush() {
@ -51,13 +50,13 @@ func (o *JSONOutput) Flush() {
if o.withSummary {
jsonObj := struct {
Resources []result `json:"resources"`
Summary struct {
Valid int `json:"valid"`
Summary struct {
Valid int `json:"valid"`
Invalid int `json:"invalid"`
Errors int `json:"errors"`
Errors int `json:"errors"`
Skipped int `json:"skipped"`
} `json:"summary"`
} {
}{
Resources: o.results,
}
@ -74,15 +73,15 @@ func (o *JSONOutput) Flush() {
}
}
res, err = json.MarshalIndent(jsonObj,"", " ")
res, err = json.MarshalIndent(jsonObj, "", " ")
} else {
jsonObj := struct {
Resources []result
} {
}{
Resources: o.results,
}
res, err = json.MarshalIndent(jsonObj,"", " ")
res, err = json.MarshalIndent(jsonObj, "", " ")
}
if err != nil {

View file

@ -1,7 +1,31 @@
package output
import "github.com/yannh/kubeconform/pkg/validator"
const (
VALID = iota
INVALID = iota
ERROR = iota
SKIPPED = iota
)
type Output interface {
Write (filename string, err error, skipped bool)
Flush ()
Write(filename string, err error, skipped bool)
Flush()
}
func status(err error, skipped bool) int {
if err != nil {
if _, ok := err.(validator.InvalidResourceError); ok {
return INVALID
} else {
return ERROR
}
}
if skipped {
return SKIPPED
}
return VALID
}

View file

@ -2,39 +2,32 @@ package output
import (
"fmt"
"github.com/yannh/kubeconform/pkg/validator"
)
type TextOutput struct {
withSummary bool
withSummary bool
nValid, nInvalid, nErrors, nSkipped int
}
func NewTextOutput(withSummary bool) Output {
return &TextOutput{withSummary, 0,0,0, 0}
return &TextOutput{withSummary, 0, 0, 0, 0}
}
func (o *TextOutput) Write(filename string,err error, skipped bool) {
if skipped {
fmt.Printf("skipping resource\n")
o.nSkipped++
return
}
if err != nil {
if _, ok := err.(validator.InvalidResourceError); ok {
fmt.Printf("invalid resource: %s\n", err)
o.nInvalid++
} else {
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
o.nErrors++
}
return
}
if !skipped{
func (o *TextOutput) Write(filename string, err error, skipped bool) {
s := status(err, skipped)
switch {
case s == VALID:
fmt.Printf("file %s is valid\n", filename)
o.nValid++
case s == INVALID:
fmt.Printf("invalid resource: %s\n", err)
o.nInvalid++
case s == ERROR:
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
o.nErrors++
case s == SKIPPED:
fmt.Printf("skipping resource\n")
o.nSkipped++
}
}
@ -43,4 +36,3 @@ func (o *TextOutput) Flush() {
fmt.Printf("Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped)
}
}