@@ -2,21 +2,41 @@ package rego
2
2
3
3
import (
4
4
"fmt"
5
+ "io"
5
6
"io/fs"
6
7
"os"
7
8
"path/filepath"
8
9
"slices"
9
10
"strings"
10
11
12
+ "github.com/liamg/memoryfs"
11
13
"github.com/open-policy-agent/opa/v1/loader"
12
14
"github.com/open-policy-agent/opa/v1/storage"
13
15
)
14
16
15
17
// initialize a store populated with OPA data files found in dataPaths
16
18
func initStore (dataFS fs.FS , dataPaths , namespaces []string ) (storage.Store , error ) {
19
+ fsys := memoryfs .New ()
20
+ // The virtual file system uses a slash ('/') as a path separator,
21
+ // but OPA uses the filepath package, which is OS-dependent.
22
+ // So we need to create a copy of the filesystem where the paths use the an OS-specific separator.
23
+ if err := fs .WalkDir (dataFS , "." , func (path string , d fs.DirEntry , err error ) error {
24
+ if err != nil {
25
+ return err
26
+ }
27
+ if d .IsDir () {
28
+ return fsys .MkdirAll (path , d .Type ().Perm ())
29
+ }
30
+ return fsys .WriteLazyFile (filepath .FromSlash (path ), func () (io.Reader , error ) {
31
+ return fsys .Open (path )
32
+ }, d .Type ().Perm ())
33
+ }); err != nil {
34
+ return nil , fmt .Errorf ("walk dir: %w" , err )
35
+ }
36
+
17
37
// FilteredPaths will recursively find all file paths that contain a valid document
18
38
// extension from the given list of data paths.
19
- allDocumentPaths , _ := loader .FilteredPathsFS (dataFS , dataPaths ,
39
+ allDocumentPaths , _ := loader .FilteredPathsFS (fsys , dataPaths ,
20
40
func (abspath string , info os.FileInfo , depth int ) bool {
21
41
return ! info .IsDir () && ! isDataFile (info )
22
42
},
0 commit comments