Skip to content

Commit

Permalink
Adding the ability to exclude some files or file extensions in FileSy…
Browse files Browse the repository at this point in the history
…stem Source

Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed Aug 28, 2023
1 parent 27f66b3 commit 7df0378
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions pkg/migration/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
gopath "path"
"path/filepath"

"github.com/pkg/errors"
Expand All @@ -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
Expand All @@ -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{
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7df0378

Please sign in to comment.