add quiet mode, fix race condition in statistics

This commit is contained in:
Yann Hamon 2020-05-31 03:48:38 +02:00
parent b918da9c59
commit ae5fbffb95
4 changed files with 60 additions and 25 deletions

2
pkg/cache/main.go vendored
View file

@ -24,7 +24,7 @@ func Key(resourceKind, resourceAPIVersion, k8sVersion string) string {
func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
c.RLock()
defer c.RUnlock()
schema, ok := c.schemas[key]
schema, ok := c.schemas[key]
return schema, ok
}

View file

@ -3,6 +3,7 @@ package output
import (
"encoding/json"
"fmt"
"sync"
)
type result struct {
@ -12,35 +13,51 @@ type result struct {
}
type JSONOutput struct {
withSummary bool
results []result
sync.Mutex
withSummary bool
quiet bool
results []result
nValid, nInvalid, nErrors, nSkipped int
}
func NewJSONOutput(withSummary bool) Output {
func NewJSONOutput(withSummary bool, quiet bool) Output {
return &JSONOutput{
withSummary: withSummary,
quiet: quiet,
results: []result{},
nValid: 0,
nInvalid: 0,
nErrors: 0,
nSkipped: 0,
}
}
func (o *JSONOutput) Write(filename string, err error, skipped bool) {
o.Lock()
defer o.Unlock()
msg, st := "", ""
s := status(err, skipped)
switch {
case s == VALID:
st = "VALID"
o.nValid++
case s == INVALID:
st = "INVALID"
msg = err.Error()
o.nInvalid++
case s == ERROR:
st = "ERROR"
msg = err.Error()
o.nErrors++
case s == SKIPPED:
st = "SKIPPED"
o.nSkipped++
}
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
if !o.quiet || (s != VALID && s != SKIPPED) {
o.results = append(o.results, result{Filename: filename, Status: st, Msg: msg})
}
}
func (o *JSONOutput) Flush() {
@ -58,25 +75,23 @@ func (o *JSONOutput) Flush() {
} `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 == "ERROR":
jsonObj.Summary.Errors++
case r.Status == "SKIPPED":
jsonObj.Summary.Skipped++
}
Summary: struct {
Valid int `json:"valid"`
Invalid int `json:"invalid"`
Errors int `json:"errors"`
Skipped int `json:"skipped"`
}{
Valid: o.nValid,
Invalid: o.nInvalid,
Errors: o.nErrors,
Skipped: o.nSkipped,
},
}
res, err = json.MarshalIndent(jsonObj, "", " ")
} else {
jsonObj := struct {
Resources []result
Resources []result `json:"resources"`
}{
Resources: o.results,
}

View file

@ -2,22 +2,37 @@ package output
import (
"fmt"
"sync"
)
type TextOutput struct {
sync.Mutex
withSummary bool
quiet bool
nValid, nInvalid, nErrors, nSkipped int
}
func NewTextOutput(withSummary bool) Output {
return &TextOutput{withSummary, 0, 0, 0, 0}
func NewTextOutput(withSummary, quiet bool) Output {
return &TextOutput{
withSummary: withSummary,
quiet: quiet,
nValid: 0,
nInvalid: 0,
nErrors: 0,
nSkipped: 0,
}
}
func (o *TextOutput) Write(filename string, err error, skipped bool) {
o.Lock()
defer o.Unlock()
s := status(err, skipped)
switch {
case s == VALID:
fmt.Printf("file %s is valid\n", filename)
if !o.quiet {
fmt.Printf("file %s is valid\n", filename)
}
o.nValid++
case s == INVALID:
fmt.Printf("invalid resource: %s\n", err)
@ -26,7 +41,9 @@ func (o *TextOutput) Write(filename string, err error, skipped bool) {
fmt.Printf("failed validating resource in file %s: %s\n", filename, err)
o.nErrors++
case s == SKIPPED:
fmt.Printf("skipping resource\n")
if !o.quiet {
fmt.Printf("skipping resource\n")
}
o.nSkipped++
}
}