From 7df03782262b59a2d888109ede9876474227e3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Tue, 29 Aug 2023 01:38:59 +0200 Subject: [PATCH] Adding the ability to exclude some files or file extensions in FileSystem Source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- pkg/migration/filesystem.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) 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