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

@ -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
}