use different pattern for processResults

This commit is contained in:
Yann Hamon 2020-11-02 00:23:03 +01:00
parent 81c097f1bb
commit dfb80055fe
3 changed files with 24 additions and 19 deletions

View file

@ -85,18 +85,26 @@ func ValidateResources(resources <-chan resource.Resource, validationResults cha
} }
} }
func processResults(o output.Output, validationResults <-chan validator.Result, result chan<- bool) { func processResults(o output.Output, validationResults <-chan validator.Result) <-chan bool {
success := true success := true
for res := range validationResults { result := make(chan bool)
if res.Err != nil {
success = false
}
if err := o.Write(res); err != nil {
fmt.Fprint(os.Stderr, "failed writing log\n")
}
}
result <- success go func() {
for res := range validationResults {
if res.Status == validator.Error || res.Status == validator.Invalid {
success = false
}
if o != nil {
if err := o.Write(res); err != nil {
fmt.Fprint(os.Stderr, "failed writing log\n")
}
}
}
result <- success
close(result)
}()
return result
} }
func realMain() int { func realMain() int {
@ -135,9 +143,8 @@ func realMain() int {
var resourcesChan <-chan resource.Resource var resourcesChan <-chan resource.Resource
var errors <-chan error var errors <-chan error
validationResults := make(chan validator.Result) validationResults := make(chan validator.Result)
res := make(chan bool)
go processResults(o, validationResults, res) successChan := processResults(o, validationResults)
if isStdin { if isStdin {
resourcesChan, errors = resource.FromStream("stdin", os.Stdin) resourcesChan, errors = resource.FromStream("stdin", os.Stdin)
@ -170,7 +177,7 @@ func realMain() int {
wg.Wait() wg.Wait()
close(validationResults) close(validationResults)
success := <-res success := <-successChan
o.Flush() o.Flush()
if !success { if !success {

View file

@ -41,7 +41,7 @@ metadata:
`), `),
}, },
Status: validator.Valid, Status: validator.Valid,
Err: nil, Err: nil,
}, },
}, },
`{ `{
@ -71,9 +71,8 @@ metadata:
`), `),
}, },
Status: validator.Valid, Status: validator.Valid,
Err: nil, Err: nil,
}, },
}, },
`{ `{
"resources": [ "resources": [

View file

@ -40,7 +40,7 @@ metadata:
`), `),
}, },
Status: validator.Valid, Status: validator.Valid,
Err: nil, Err: nil,
}, },
}, },
"Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0\n", "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0\n",
@ -61,9 +61,8 @@ metadata:
`), `),
}, },
Status: validator.Valid, Status: validator.Valid,
Err: nil, Err: nil,
}, },
}, },
`deployment.yml - Deployment my-app is valid `deployment.yml - Deployment my-app is valid
Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0 Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0