55 "errors"
66 "fmt"
77 "io"
8- "os "
8+ "io/fs "
99 "path/filepath"
1010 "strings"
1111
@@ -17,20 +17,23 @@ import (
1717 "github.com/operator-framework/operator-registry/internal/property"
1818)
1919
20- func LoadDir (configDir string ) (* DeclarativeConfig , error ) {
21- w := & dirWalker {}
22- return loadFS (configDir , w )
23- }
24-
25- func loadFS (root string , w fsWalker ) (* DeclarativeConfig , error ) {
20+ // LoadFS loads a declarative config from the provided root FS. LoadFS walks the
21+ // filesystem from root and uses a gitignore-style filename matcher to skip files
22+ // that match patterns found in .indexignore files found throughout the filesystem.
23+ // If LoadFS encounters an error loading or parsing any file, the error will be
24+ // immedidately returned.
25+ func LoadFS (root fs.FS ) (* DeclarativeConfig , error ) {
26+ if root == nil {
27+ return nil , fmt .Errorf ("no declarative config filesystem provided" )
28+ }
2629 cfg := & DeclarativeConfig {}
2730
28- matcher , err := ignore .NewMatcher (os . DirFS ( root ) , ".indexignore" )
31+ matcher , err := ignore .NewMatcher (root , ".indexignore" )
2932 if err != nil {
3033 return nil , err
3134 }
3235
33- if err := w . WalkFiles (root , func (path string , r io.Reader ) error {
36+ if err := walkFiles (root , func (path string , r io.Reader ) error {
3437 if matcher .Match (path , false ) {
3538 return nil
3639 }
@@ -52,7 +55,7 @@ func loadFS(root string, w fsWalker) (*DeclarativeConfig, error) {
5255 return cfg , nil
5356}
5457
55- func readBundleObjects (bundles []Bundle , root , path string ) error {
58+ func readBundleObjects (bundles []Bundle , root fs. FS , path string ) error {
5659 for bi , b := range bundles {
5760 props , err := property .Parse (b .Properties )
5861 if err != nil {
@@ -123,21 +126,15 @@ func readYAMLOrJSON(r io.Reader) (*DeclarativeConfig, error) {
123126 return cfg , nil
124127}
125128
126- type fsWalker interface {
127- WalkFiles (root string , f func (path string , r io.Reader ) error ) error
128- }
129-
130- type dirWalker struct {}
131-
132- func (w dirWalker ) WalkFiles (root string , f func (string , io.Reader ) error ) error {
133- return filepath .Walk (root , func (path string , info os.FileInfo , err error ) error {
129+ func walkFiles (root fs.FS , f func (string , io.Reader ) error ) error {
130+ return fs .WalkDir (root , "." , func (path string , info fs.DirEntry , err error ) error {
134131 if err != nil {
135132 return err
136133 }
137134 if info .IsDir () {
138135 return nil
139136 }
140- file , err := os .Open (path )
137+ file , err := root .Open (path )
141138 if err != nil {
142139 return err
143140 }
0 commit comments