kubeconform/pkg/resource/stream.go
2020-11-08 22:35:17 +01:00

40 lines
734 B
Go

package resource
import (
"bytes"
"context"
"io"
"io/ioutil"
)
// FromStream reads resources from a byte stream, usually here stdin
func FromStream(ctx context.Context, path string, r io.Reader) (<-chan Resource, <-chan error) {
resources := make(chan Resource)
errors := make(chan error)
stop := false
go func() {
<-ctx.Done()
stop = true
}()
go func() {
data, err := ioutil.ReadAll(r)
if err != nil {
errors <- DiscoveryError{path, err}
}
rawResources := bytes.Split(data, []byte("---\n"))
for _, rawResource := range rawResources {
if stop == true {
break
}
resources <- Resource{Path: path, Bytes: rawResource}
}
close(resources)
close(errors)
}()
return resources, errors
}