From 92f8f4593d6e66223057a0eb36050990675a6abb Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 2 Jan 2021 12:52:58 +0100 Subject: [PATCH] break down FromFiles for easier testing --- cmd/kubeconform/main.go | 2 +- pkg/resource/files.go | 62 ++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/cmd/kubeconform/main.go b/cmd/kubeconform/main.go index fdc1b75..554d439 100644 --- a/cmd/kubeconform/main.go +++ b/cmd/kubeconform/main.go @@ -104,7 +104,7 @@ func realMain() int { if isStdin { resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin) } else { - resourcesChan, errors = resource.FromFiles(ctx, cfg.IgnoreFilenamePatterns, cfg.Files...) + resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns) } // Process discovered resources across multiple workers diff --git a/pkg/resource/files.go b/pkg/resource/files.go index a3b5e16..aa8ed13 100644 --- a/pkg/resource/files.go +++ b/pkg/resource/files.go @@ -40,8 +40,7 @@ func isIgnored(path string, ignoreFilePatterns []string) (bool, error) { return false, nil } -func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string) (<-chan Resource, <-chan error) { - resources := make(chan Resource) +func findFilesInFolders(ctx context.Context, paths []string, ignoreFilePatterns []string) (chan string, chan error) { files := make(chan string) errors := make(chan error) @@ -85,33 +84,44 @@ func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string close(files) }() + return files, errors +} + +func findResourcesInFile(p string, resources chan<- Resource, errors chan<- error, buf []byte) { + f, err := os.Open(p) + defer f.Close() + if err != nil { + errors <- DiscoveryError{p, err} + return + } + + scanner := bufio.NewScanner(f) + scanner.Buffer(buf, len(buf)) + scanner.Split(SplitYAMLDocument) + nRes := 0 + for res := scanner.Scan(); res != false; res = scanner.Scan() { + resources <- Resource{Path: p, Bytes: []byte(scanner.Text())} + nRes++ + } + if err := scanner.Err(); err != nil { + errors <- DiscoveryError{p, err} + } + if nRes == 0 { + resources <- Resource{Path: p, Bytes: []byte{}} + } +} + +func FromFiles(ctx context.Context, paths []string, ignoreFilePatterns []string) (<-chan Resource, <-chan error) { + resources := make(chan Resource) + + files, errors := findFilesInFolders(ctx, paths, ignoreFilePatterns) + go func() { - maxResourceSize := 4 * 1024 * 1024 - buf := make([]byte, maxResourceSize) + maxResourceSize := 4 * 1024 * 1024 // 4MB ought to be enough for everybody + buf := make([]byte, maxResourceSize) // We reuse this to avoid multiple large memory allocations for p := range files { - f, err := os.Open(p) - if err != nil { - errors <- DiscoveryError{p, err} - continue - } - - scanner := bufio.NewScanner(f) - scanner.Buffer(buf, maxResourceSize) - scanner.Split(SplitYAMLDocument) - nRes := 0 - for res := scanner.Scan(); res != false; res = scanner.Scan() { - resources <- Resource{Path: p, Bytes: []byte(scanner.Text())} - nRes++ - } - if err := scanner.Err(); err != nil { - errors <- DiscoveryError{p, err} - } - if nRes == 0 { - resources <- Resource{Path: p, Bytes: []byte{}} - } - - f.Close() + findResourcesInFile(p, resources, errors, buf) } close(errors)