From a938c47e2ec23549643e07d09d75628e46994cdb Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 6 Jun 2020 18:23:56 +0200 Subject: [PATCH] handle files and dirs as parameters --- Readme.md | 6 ++++-- main.go | 46 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index 527f310..8e59f87 100644 --- a/Readme.md +++ b/Readme.md @@ -74,10 +74,12 @@ $ echo $? * Validating a folder, increasing the number of parallel workers ``` -$ ./bin/kubeconform -dir fixtures -summary -n 16 +$ ./bin/kubeconform -summary -n 16 fixtures fixtures/multi_invalid.yaml - Service is invalid: Invalid type. Expected: integer, given: string fixtures/invalid.yaml - ReplicationController is invalid: Invalid type. Expected: [integer,null], given: string -Run summary - Valid: 40, Invalid: 2, Errors: 0 Skipped: 6 +[...] +Summary: 48 resources found in 25 files - Valid: 39, Invalid: 2, Errors: 7 Skipped: 0 + ``` * Validating a custom resources, using a local schema diff --git a/main.go b/main.go index b66c295..cc728fa 100644 --- a/main.go +++ b/main.go @@ -178,7 +178,7 @@ func processResults(o output.Output, validationResults chan []validationResult, } func realMain() int { - var dirs, schemas arrayParam + var schemas arrayParam var skipKindsCSV, k8sVersion, outputFormat string var summary, strict, verbose, ignoreMissingSchemas bool var nWorkers int @@ -186,7 +186,6 @@ func realMain() int { var files []string flag.StringVar(&k8sVersion, "k8sversion", "1.18.0", "version of Kubernetes to test against") - flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)") flag.Var(&schemas, "schema", "file containing an additional Schema (can be specified multiple times)") flag.BoolVar(&ignoreMissingSchemas, "ignore-missing-schemas", false, "skip files with missing schemas instead of failing") flag.BoolVar(&summary, "summary", false, "print a summary at the end") @@ -218,16 +217,43 @@ func realMain() int { registries = append(registries, localRegistry) } + validationResults := make(chan []validationResult) + fileBatches := make(chan []string) go func() { - for _, dir := range dirs { - if err := fsutils.FindYamlInDir(dir, fileBatches, 10); err != nil { - log.Printf("failed processing folder %s: %s", dir, err) - } - } - for _, filename := range files { - fileBatches <- []string{filename} + file, err := os.Open(filename) + if err != nil { + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: true, + }} + continue + } + defer file.Close() + + fi, err := file.Stat(); + switch { + case err != nil: + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: true, + }} + + case fi.IsDir(): + if err := fsutils.FindYamlInDir(filename, fileBatches, 10); err != nil { + validationResults <- []validationResult{{ + filename: filename, + err: err, + skipped: true, + }} + } + + default: + fileBatches <- []string{filename} + } } close(fileBatches) @@ -240,7 +266,6 @@ func realMain() int { } res := make(chan bool) - validationResults := make(chan []validationResult) go processResults(o, validationResults, res) c := cache.New() @@ -254,7 +279,6 @@ func realMain() int { for _, filename := range fileBatch { f, err := os.Open(filename) if err != nil { - fmt.Printf("failed opening %s\n", filename) validationResults <- []validationResult{{ filename: filename, err: err,