From 7db0febbd100069f5b522000dd6dfc171b64a434 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 6 Jun 2020 18:37:11 +0200 Subject: [PATCH] move file finding to own function --- main.go | 75 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/main.go b/main.go index d6898c0..50b4ac7 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "github.com/xeipuuv/gojsonschema" + "github.com/yannh/kubeconform/pkg/fsutils" "github.com/yannh/kubeconform/pkg/output" "io" "io/ioutil" @@ -14,7 +15,6 @@ import ( "sync" "github.com/yannh/kubeconform/pkg/cache" - "github.com/yannh/kubeconform/pkg/fsutils" "github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/resource" "github.com/yannh/kubeconform/pkg/validator" @@ -177,6 +177,43 @@ func processResults(o output.Output, validationResults chan []validationResult, result <- success } +func getFiles(files []string, fileBatches chan []string, validationResults chan []validationResult) { + for _, filename := range files { + file, err := os.Open(filename) + if err != nil { + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: false, + }} + continue + } + defer file.Close() + + fi, err := file.Stat() + switch { + case err != nil: + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: false, + }} + + case fi.IsDir(): + if err := fsutils.FindYamlInDir(filename, fileBatches, 10); err != nil { + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: false, + }} + } + + default: + fileBatches <- []string{filename} + } + } +} + func realMain() int { var schemas arrayParam var skipKindsCSV, k8sVersion, outputFormat string @@ -221,41 +258,7 @@ func realMain() int { fileBatches := make(chan []string) go func() { - for _, filename := range files { - file, err := os.Open(filename) - if err != nil { - validationResults <- []validationResult{{ - filename: filename, - err: err, - skipped: false, - }} - continue - } - defer file.Close() - - fi, err := file.Stat() - switch { - case err != nil: - validationResults <- []validationResult{{ - filename: filename, - err: err, - skipped: false, - }} - - case fi.IsDir(): - if err := fsutils.FindYamlInDir(filename, fileBatches, 10); err != nil { - validationResults <- []validationResult{{ - filename: filename, - err: err, - skipped: false, - }} - } - - default: - fileBatches <- []string{filename} - } - } - + getFiles(files, fileBatches, validationResults) close(fileBatches) }()