mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-23 11:47:01 +00:00
differentiate between an invalid resource and a validation error in output
This commit is contained in:
parent
bb478da7e0
commit
928d694e66
2 changed files with 16 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ package output
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/yannh/kubeconform/pkg/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
|
|
@ -26,10 +27,16 @@ func NewJSONOutput(withSummary bool) Output{
|
||||||
func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
||||||
status := "VALID"
|
status := "VALID"
|
||||||
msg := ""
|
msg := ""
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status = "INVALID"
|
|
||||||
msg = err.Error()
|
msg = err.Error()
|
||||||
|
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||||
|
status = "INVALID"
|
||||||
|
} else {
|
||||||
|
status = "ERROR"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if skipped {
|
if skipped {
|
||||||
status = "SKIPPED"
|
status = "SKIPPED"
|
||||||
}
|
}
|
||||||
|
|
@ -47,6 +54,7 @@ func (o *JSONOutput) Flush() {
|
||||||
Summary struct {
|
Summary struct {
|
||||||
Valid int `json:"valid"`
|
Valid int `json:"valid"`
|
||||||
Invalid int `json:"invalid"`
|
Invalid int `json:"invalid"`
|
||||||
|
Errors int `json:"errors"`
|
||||||
Skipped int `json:"skipped"`
|
Skipped int `json:"skipped"`
|
||||||
} `json:"summary"`
|
} `json:"summary"`
|
||||||
} {
|
} {
|
||||||
|
|
@ -59,6 +67,8 @@ func (o *JSONOutput) Flush() {
|
||||||
jsonObj.Summary.Valid++
|
jsonObj.Summary.Valid++
|
||||||
case r.Status == "INVALID":
|
case r.Status == "INVALID":
|
||||||
jsonObj.Summary.Invalid++
|
jsonObj.Summary.Invalid++
|
||||||
|
case r.Status == "ERROR":
|
||||||
|
jsonObj.Summary.Errors++
|
||||||
case r.Status == "SKIPPED":
|
case r.Status == "SKIPPED":
|
||||||
jsonObj.Summary.Skipped++
|
jsonObj.Summary.Skipped++
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ import (
|
||||||
|
|
||||||
type TextOutput struct {
|
type TextOutput struct {
|
||||||
withSummary bool
|
withSummary bool
|
||||||
nValid, nInvalid, nSkipped int
|
nValid, nInvalid, nErrors, nSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTextOutput(withSummary bool) Output {
|
func NewTextOutput(withSummary bool) Output {
|
||||||
return &TextOutput{withSummary, 0,0,0}
|
return &TextOutput{withSummary, 0,0,0, 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||||
|
|
@ -22,11 +22,12 @@ func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
o.nInvalid++
|
|
||||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||||
fmt.Printf("invalid resource: %s\n", err)
|
fmt.Printf("invalid resource: %s\n", err)
|
||||||
|
o.nInvalid++
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||||
|
o.nErrors++
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +40,7 @@ func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||||
|
|
||||||
func (o *TextOutput) Flush() {
|
func (o *TextOutput) Flush() {
|
||||||
if o.withSummary {
|
if o.withSummary {
|
||||||
fmt.Printf("Run summary - Valid: %d, Invalid: %d, Skipped: %d\n", o.nValid, o.nInvalid, o.nSkipped)
|
fmt.Printf("Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue