mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-21 10:57:01 +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}}
|
return []validationResult{{err: nil}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type arrayFiles []string
|
type arrayFiles []string
|
||||||
|
|
||||||
func (i *arrayFiles) String() string {
|
func (i *arrayFiles) String() string {
|
||||||
|
|
@ -76,6 +74,8 @@ func (i *arrayFiles) Set(value string) error {
|
||||||
func realMain() int {
|
func realMain() int {
|
||||||
var files, dirs, schemas arrayFiles
|
var files, dirs, schemas arrayFiles
|
||||||
var skipKinds, k8sVersion, outputFormat string
|
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(&files, "file", "file to validate (can be specified multiple times)")
|
||||||
flag.Var(&dirs, "dir", "directory 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")
|
flag.Var(&schemas, "schema", "file containing an additional Schema")
|
||||||
|
|
@ -87,9 +87,9 @@ func realMain() int {
|
||||||
var o output.Output
|
var o output.Output
|
||||||
switch {
|
switch {
|
||||||
case outputFormat == "text":
|
case outputFormat == "text":
|
||||||
o = output.NewTextOutput()
|
o = output.NewTextOutput(printSummary)
|
||||||
case outputFormat == "json":
|
case outputFormat == "json":
|
||||||
o = output.NewJSONOutput()
|
o = output.NewJSONOutput(printSummary)
|
||||||
default:
|
default:
|
||||||
log.Fatalf("-output must be text or json")
|
log.Fatalf("-output must be text or json")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,13 @@ type result struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONOutput struct {
|
type JSONOutput struct {
|
||||||
|
withSummary bool
|
||||||
results []result
|
results []result
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJSONOutput() Output{
|
func NewJSONOutput(withSummary bool) Output{
|
||||||
return &JSONOutput{
|
return &JSONOutput{
|
||||||
|
withSummary: withSummary,
|
||||||
results: []result{},
|
results: []result{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -25,7 +27,7 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
||||||
status := "VALID"
|
status := "VALID"
|
||||||
msg := ""
|
msg := ""
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status = "ERROR"
|
status = "INVALID"
|
||||||
msg = err.Error()
|
msg = err.Error()
|
||||||
}
|
}
|
||||||
if skipped {
|
if skipped {
|
||||||
|
|
@ -36,7 +38,43 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *JSONOutput) Flush() {
|
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 {
|
if err != nil {
|
||||||
fmt.Printf("error print results: %s", err)
|
fmt.Printf("error print results: %s", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,45 @@
|
||||||
package output
|
package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/yannh/kubeconform/pkg/validator"
|
"github.com/yannh/kubeconform/pkg/validator"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextOutput struct {
|
type TextOutput struct {
|
||||||
|
withSummary bool
|
||||||
|
nValid, nInvalid, nSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTextOutput() Output {
|
func NewTextOutput(withSummary bool) Output {
|
||||||
return &TextOutput{}
|
return &TextOutput{withSummary, 0,0,0}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
func (o *TextOutput) Write(filename string,err error, skipped bool) {
|
||||||
if skipped {
|
if skipped {
|
||||||
log.Printf("skipping resource\n")
|
fmt.Printf("skipping resource\n")
|
||||||
|
o.nSkipped++
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
o.nInvalid++
|
||||||
if _, ok := err.(validator.InvalidResourceError); ok {
|
if _, ok := err.(validator.InvalidResourceError); ok {
|
||||||
log.Printf("invalid resource: %s\n", err)
|
fmt.Printf("invalid resource: %s\n", err)
|
||||||
} else {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !skipped{
|
if !skipped{
|
||||||
log.Printf("file %s is valid\n", filename)
|
fmt.Printf("file %s is valid\n", filename)
|
||||||
|
o.nValid++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *TextOutput) Flush() {
|
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