mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-27 23:47:33 -05:00
Bumps [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) from 0.51.0 to 0.59.0. - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v0.51.0...v0.59.0) --- updated-dependencies: - dependency-name: github.com/open-policy-agent/opa dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
//go:build go1.16
|
|
// +build go1.16
|
|
|
|
package bundle
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"github.com/open-policy-agent/opa/loader/filter"
|
|
)
|
|
|
|
const (
|
|
defaultFSLoaderRoot = "."
|
|
)
|
|
|
|
type dirLoaderFS struct {
|
|
sync.Mutex
|
|
filesystem fs.FS
|
|
files []string
|
|
idx int
|
|
filter filter.LoaderFilter
|
|
root string
|
|
pathFormat PathFormat
|
|
}
|
|
|
|
// NewFSLoader returns a basic DirectoryLoader implementation
|
|
// that will load files from a fs.FS interface
|
|
func NewFSLoader(filesystem fs.FS) (DirectoryLoader, error) {
|
|
return NewFSLoaderWithRoot(filesystem, defaultFSLoaderRoot), nil
|
|
}
|
|
|
|
// NewFSLoaderWithRoot returns a basic DirectoryLoader implementation
|
|
// that will load files from a fs.FS interface at the supplied root
|
|
func NewFSLoaderWithRoot(filesystem fs.FS, root string) DirectoryLoader {
|
|
d := dirLoaderFS{
|
|
filesystem: filesystem,
|
|
root: normalizeRootDirectory(root),
|
|
pathFormat: Chrooted,
|
|
}
|
|
|
|
return &d
|
|
}
|
|
|
|
func (d *dirLoaderFS) walkDir(path string, dirEntry fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if dirEntry != nil {
|
|
info, err := dirEntry.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if dirEntry.Type().IsRegular() {
|
|
if d.filter != nil && d.filter(filepath.ToSlash(path), info, getdepth(path, false)) {
|
|
return nil
|
|
}
|
|
|
|
d.files = append(d.files, path)
|
|
} else if dirEntry.Type().IsDir() {
|
|
if d.filter != nil && d.filter(filepath.ToSlash(path), info, getdepth(path, true)) {
|
|
return fs.SkipDir
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WithFilter specifies the filter object to use to filter files while loading bundles
|
|
func (d *dirLoaderFS) WithFilter(filter filter.LoaderFilter) DirectoryLoader {
|
|
d.filter = filter
|
|
return d
|
|
}
|
|
|
|
// WithPathFormat specifies how a path is formatted in a Descriptor
|
|
func (d *dirLoaderFS) WithPathFormat(pathFormat PathFormat) DirectoryLoader {
|
|
d.pathFormat = pathFormat
|
|
return d
|
|
}
|
|
|
|
// NextFile iterates to the next file in the directory tree
|
|
// and returns a file Descriptor for the file.
|
|
func (d *dirLoaderFS) NextFile() (*Descriptor, error) {
|
|
d.Lock()
|
|
defer d.Unlock()
|
|
|
|
if d.files == nil {
|
|
err := fs.WalkDir(d.filesystem, d.root, d.walkDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list files: %w", err)
|
|
}
|
|
}
|
|
|
|
// If done reading files then just return io.EOF
|
|
// errors for each NextFile() call
|
|
if d.idx >= len(d.files) {
|
|
return nil, io.EOF
|
|
}
|
|
|
|
fileName := d.files[d.idx]
|
|
d.idx++
|
|
|
|
fh, err := d.filesystem.Open(fileName)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open file %s: %w", fileName, err)
|
|
}
|
|
|
|
cleanedPath := formatPath(fileName, d.root, d.pathFormat)
|
|
f := newDescriptor(cleanedPath, cleanedPath, fh).withCloser(fh)
|
|
return f, nil
|
|
}
|