mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
enable printing a summary at the end of the run
This commit is contained in:
parent
5f3504724f
commit
bb478da7e0
3 changed files with 60 additions and 14 deletions
8
main.go
8
main.go
|
|
@ -58,8 +58,6 @@ func validateFile(f io.Reader, regs []registry.Registry, k8sVersion string, skip
|
|||
return []validationResult{{err: nil}}
|
||||
}
|
||||
|
||||
|
||||
|
||||
type arrayFiles []string
|
||||
|
||||
func (i *arrayFiles) String() string {
|
||||
|
|
@ -76,6 +74,8 @@ func (i *arrayFiles) Set(value string) error {
|
|||
func realMain() int {
|
||||
var files, dirs, schemas arrayFiles
|
||||
var skipKinds, k8sVersion, outputFormat string
|
||||
var printSummary bool
|
||||
flag.BoolVar(&printSummary, "printsummary", false, "print a summary at the end")
|
||||
flag.Var(&files, "file", "file to validate (can be specified multiple times)")
|
||||
flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)")
|
||||
flag.Var(&schemas, "schema", "file containing an additional Schema")
|
||||
|
|
@ -87,9 +87,9 @@ func realMain() int {
|
|||
var o output.Output
|
||||
switch {
|
||||
case outputFormat == "text":
|
||||
o = output.NewTextOutput()
|
||||
o = output.NewTextOutput(printSummary)
|
||||
case outputFormat == "json":
|
||||
o = output.NewJSONOutput()
|
||||
o = output.NewJSONOutput(printSummary)
|
||||
default:
|
||||
log.Fatalf("-output must be text or json")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,11 +12,13 @@ type result struct {
|
|||
}
|
||||
|
||||
type JSONOutput struct {
|
||||
withSummary bool
|
||||
results []result
|
||||
}
|
||||
|
||||
func NewJSONOutput() Output{
|
||||
func NewJSONOutput(withSummary bool) Output{
|
||||
return &JSONOutput{
|
||||
withSummary: withSummary,
|
||||
results: []result{},
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +27,7 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
|||
status := "VALID"
|
||||
msg := ""
|
||||
if err != nil {
|
||||
status = "ERROR"
|
||||
status = "INVALID"
|
||||
msg = err.Error()
|
||||
}
|
||||
if skipped {
|
||||
|
|
@ -36,7 +38,43 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
|||
}
|
||||
|
||||
func (o *JSONOutput) Flush() {
|
||||
res, err := json.MarshalIndent(o.results,"", " ")
|
||||
var err error
|
||||
var res []byte
|
||||
|
||||
if o.withSummary {
|
||||
jsonObj := struct {
|
||||
Resources []result `json:"resources"`
|
||||
Summary struct {
|
||||
Valid int `json:"valid"`
|
||||
Invalid int `json:"invalid"`
|
||||
Skipped int `json:"skipped"`
|
||||
} `json:"summary"`
|
||||
} {
|
||||
Resources: o.results,
|
||||
}
|
||||
|
||||
for _, r := range o.results {
|
||||
switch {
|
||||
case r.Status == "VALID":
|
||||
jsonObj.Summary.Valid++
|
||||
case r.Status == "INVALID":
|
||||
jsonObj.Summary.Invalid++
|
||||
case r.Status == "SKIPPED":
|
||||
jsonObj.Summary.Skipped++
|
||||
}
|
||||
}
|
||||
|
||||
res, err = json.MarshalIndent(jsonObj,"", " ")
|
||||
} else {
|
||||
jsonObj := struct {
|
||||
Resources []result
|
||||
} {
|
||||
Resources: o.results,
|
||||
}
|
||||
|
||||
res, err = json.MarshalIndent(jsonObj,"", " ")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("error print results: %s", err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,37 +1,45 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
"log"
|
||||
)
|
||||
|
||||
type TextOutput struct {
|
||||
withSummary bool
|
||||
nValid, nInvalid, nSkipped int
|
||||
}
|
||||
|
||||
func NewTextOutput() Output {
|
||||
return &TextOutput{}
|
||||
func NewTextOutput(withSummary bool) Output {
|
||||
return &TextOutput{withSummary, 0,0,0}
|
||||
}
|
||||
|
||||
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||
if skipped {
|
||||
log.Printf("skipping resource\n")
|
||||
fmt.Printf("skipping resource\n")
|
||||
o.nSkipped++
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
o.nInvalid++
|
||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||
log.Printf("invalid resource: %s\n", err)
|
||||
fmt.Printf("invalid resource: %s\n", err)
|
||||
} else {
|
||||
log.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !skipped{
|
||||
log.Printf("file %s is valid\n", filename)
|
||||
fmt.Printf("file %s is valid\n", filename)
|
||||
o.nValid++
|
||||
}
|
||||
}
|
||||
|
||||
func (o *TextOutput) Flush() {
|
||||
if o.withSummary {
|
||||
fmt.Printf("Run summary - Valid: %d, Invalid: %d, Skipped: %d\n", o.nValid, o.nInvalid, o.nSkipped)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue