From 0c2245791920c22c4a4a5b41ba4227a2147bfc77 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 1 Nov 2020 16:35:38 +0100 Subject: [PATCH] refactor --- cmd/kubeconform/main.go | 36 ++++++------------------------------ pkg/validator/validator.go | 8 ++++++++ 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/cmd/kubeconform/main.go b/cmd/kubeconform/main.go index 14e4b26..fd97338 100644 --- a/cmd/kubeconform/main.go +++ b/cmd/kubeconform/main.go @@ -107,11 +107,7 @@ func getFiles(files []string, filesChan chan<- string, validationResults chan va for _, filename := range files { file, err := os.Open(filename) if err != nil { - validationResults <- validator.Result{ - Resource: resource.Resource{Path: filename}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError(filename, err) continue } defer file.Close() @@ -119,19 +115,11 @@ func getFiles(files []string, filesChan chan<- string, validationResults chan va fi, err := file.Stat() switch { case err != nil: - validationResults <- validator.Result{ - Resource: resource.Resource{Path: filename}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError(filename, err) case fi.IsDir(): if err := fsutils.FindYamlInDir(filename, filesChan); err != nil { - validationResults <- validator.Result{ - Resource: resource.Resource{Path: filename}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError(filename, err) } default: @@ -195,11 +183,7 @@ func realMain() int { if isStdin { resources, err := resource.FromStream("stdin", os.Stdin) if err != nil { - validationResults <- validator.Result{ - Resource: resource.Resource{Path: "stdin"}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError("stdin", err) } else { resourcesChan <- resources } @@ -207,21 +191,13 @@ func realMain() int { for filename := range files { f, err := os.Open(filename) if err != nil { - validationResults <- validator.Result{ - Resource: resource.Resource{Path: filename}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError(filename, err) continue } resources, err := resource.FromStream(filename, f) if err != nil { - validationResults <- validator.Result{ - Resource: resource.Resource{Path: filename}, - Err: err, - Status: validator.Error, - } + validationResults <- validator.NewError(filename, err) continue } diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 6191eb8..966cebe 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -43,6 +43,14 @@ type Result struct { Status Status } +func NewError(filename string, err error) Result { + return Result{ + Resource: resource.Resource{Path: filename}, + Err: err, + Status: Error, + } +} + // Validate validates a single Kubernetes resource against a Json Schema func Validate(res resource.Resource, schema *gojsonschema.Schema) Result { if schema == nil {