mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-14 16:34:46 +00:00
Refactor resource discovery
This commit is contained in:
parent
2391d82281
commit
0a7f885768
4 changed files with 78 additions and 139 deletions
|
|
@ -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
|
||||
})
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue