Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to exclude some files or file extensions in FileSystem Source #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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