diff --git a/pkg/migration/filesystem.go b/pkg/migration/filesystem.go index b79a612b..77ab3d0e 100644 --- a/pkg/migration/filesystem.go +++ b/pkg/migration/filesystem.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "os" + gopath "path" "path/filepath" "github.com/pkg/errors" @@ -20,9 +21,11 @@ var ( // FileSystemSource is a source implementation to read resources from filesystem type FileSystemSource struct { - index int - items []UnstructuredWithMetadata - afero afero.Afero + index int + items []UnstructuredWithMetadata + afero afero.Afero + excludedFiles map[string]struct{} + excludedExtensions map[string]struct{} } // FileSystemSourceOption allows you to configure FileSystemSource @@ -35,6 +38,20 @@ func FsWithFileSystem(f afero.Fs) FileSystemSourceOption { } } +// WithExcludedFiles configures the excludedFiles. +func WithExcludedFiles(ef map[string]struct{}) FileSystemSourceOption { + return func(fs *FileSystemSource) { + fs.excludedFiles = ef + } +} + +// WithExcludedExtensions configures the excludedExtensions. +func WithExcludedExtensions(ee map[string]struct{}) FileSystemSourceOption { + return func(fs *FileSystemSource) { + fs.excludedExtensions = ee + } +} + // NewFileSystemSource returns a FileSystemSource func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSystemSource, error) { fs := &FileSystemSource{ @@ -53,6 +70,10 @@ func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSyste return nil } + if isExcluded(path, fs.excludedFiles, fs.excludedExtensions) { + return nil + } + data, err := fs.afero.ReadFile(path) if err != nil { return errors.Wrap(err, "cannot read source file") @@ -80,6 +101,16 @@ func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSyste return fs, nil } +func isExcluded(path string, excludedFiles, excludedExtensions map[string]struct{}) bool { + if _, ok := excludedFiles[path]; ok { + return true + } + if _, ok := excludedExtensions[gopath.Ext(path)]; ok { + return true + } + return false +} + // HasNext checks the next item func (fs *FileSystemSource) HasNext() (bool, error) { return fs.index < len(fs.items), nil