Refactor resource discovery

This commit is contained in:
Yann Hamon 2020-11-01 20:09:48 +01:00
parent 2391d82281
commit 0a7f885768
4 changed files with 78 additions and 139 deletions

View file

@ -1,27 +0,0 @@
package fsutils
import (
"os"
"path/filepath"
"strings"
)
func isYaml(info os.FileInfo) bool {
return !info.IsDir() && (strings.HasSuffix(strings.ToLower(info.Name()), ".yaml") || strings.HasSuffix(strings.ToLower(info.Name()), ".yml"))
}
// FindYamlInDir will find yaml files in folder dir, and send their filenames in batches
// of size batchSize to channel fileBatches
func FindYamlInDir(dir string, files chan<- string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if isYaml(info) {
files <- path
}
return nil
})
}

View file

@ -6,17 +6,24 @@ import (
"io/ioutil"
)
func FromStream(path string, r io.Reader) ([]Resource, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return []Resource{}, err
}
func FromStream(path string, r io.Reader) (<-chan Resource, <-chan error) {
resources := make(chan Resource)
errors := make(chan error)
resources := []Resource{}
rawResources := bytes.Split(data, []byte("---\n"))
for _, rawResource := range rawResources {
resources = append(resources, Resource{Path: path, Bytes: rawResource})
}
go func() {
data, err := ioutil.ReadAll(r)
if err != nil {
errors <- DiscoveryError{path, err}
}
return resources, nil
rawResources := bytes.Split(data, []byte("---\n"))
for _, rawResource := range rawResources {
resources <- Resource{Path: path, Bytes: rawResource}
}
close(resources)
close(errors)
}()
return resources, errors
}