Skip to content

Commit a2c4b3c

Browse files
Merge pull request #680 from joelanford/declcfg-load-fs
declcfg: change LoadDir to LoadFS
2 parents 7cf404a + 5df0ca5 commit a2c4b3c

File tree

21 files changed

+972
-929
lines changed

21 files changed

+972
-929
lines changed

cmd/opm/alpha/serve/serve.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"os"
78

89
"github.com/sirupsen/logrus"
910
"github.com/spf13/cobra"
@@ -72,7 +73,7 @@ func (s *serve) run(ctx context.Context) error {
7273

7374
s.logger = s.logger.WithFields(logrus.Fields{"configs": s.configDir, "port": s.port})
7475

75-
cfg, err := declcfg.LoadDir(s.configDir)
76+
cfg, err := declcfg.LoadFS(os.DirFS(s.configDir))
7677
if err != nil {
7778
return fmt.Errorf("load declarative config directory: %v", err)
7879
}

internal/action/render.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (r Render) Run(ctx context.Context) (*declcfg.DeclarativeConfig, error) {
5151
)
5252
// TODO(joelanford): Add support for detecting and rendering sqlite files.
5353
if stat, serr := os.Stat(ref); serr == nil && stat.IsDir() {
54-
cfg, err = declcfg.LoadDir(ref)
54+
cfg, err = declcfg.LoadFS(os.DirFS(ref))
5555
} else {
5656
cfg, err = r.imageToDeclcfg(ctx, ref)
5757
}
@@ -111,7 +111,7 @@ func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.D
111111
}
112112
} else if configsDir, ok := labels["operators.operatorframework.io.index.configs.v1"]; ok {
113113
// TODO(joelanford): Make a constant for above configs location label
114-
cfg, err = declcfg.LoadDir(filepath.Join(tmpDir, configsDir))
114+
cfg, err = declcfg.LoadFS(os.DirFS(filepath.Join(tmpDir, configsDir)))
115115
if err != nil {
116116
return nil, err
117117
}

internal/declcfg/load.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
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

Comments
 (0)