Skip to content

Commit 7df0378

Browse files
committed
Adding the ability to exclude some files or file extensions in FileSystem Source
Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
1 parent 27f66b3 commit 7df0378

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

pkg/migration/filesystem.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7+
gopath "path"
78
"path/filepath"
89

910
"github.com/pkg/errors"
@@ -20,9 +21,11 @@ var (
2021

2122
// FileSystemSource is a source implementation to read resources from filesystem
2223
type FileSystemSource struct {
23-
index int
24-
items []UnstructuredWithMetadata
25-
afero afero.Afero
24+
index int
25+
items []UnstructuredWithMetadata
26+
afero afero.Afero
27+
excludedFiles map[string]struct{}
28+
excludedExtensions map[string]struct{}
2629
}
2730

2831
// FileSystemSourceOption allows you to configure FileSystemSource
@@ -35,6 +38,20 @@ func FsWithFileSystem(f afero.Fs) FileSystemSourceOption {
3538
}
3639
}
3740

41+
// WithExcludedFiles configures the excludedFiles.
42+
func WithExcludedFiles(ef map[string]struct{}) FileSystemSourceOption {
43+
return func(fs *FileSystemSource) {
44+
fs.excludedFiles = ef
45+
}
46+
}
47+
48+
// WithExcludedExtensions configures the excludedExtensions.
49+
func WithExcludedExtensions(ee map[string]struct{}) FileSystemSourceOption {
50+
return func(fs *FileSystemSource) {
51+
fs.excludedExtensions = ee
52+
}
53+
}
54+
3855
// NewFileSystemSource returns a FileSystemSource
3956
func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSystemSource, error) {
4057
fs := &FileSystemSource{
@@ -53,6 +70,10 @@ func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSyste
5370
return nil
5471
}
5572

73+
if isExcluded(path, fs.excludedFiles, fs.excludedExtensions) {
74+
return nil
75+
}
76+
5677
data, err := fs.afero.ReadFile(path)
5778
if err != nil {
5879
return errors.Wrap(err, "cannot read source file")
@@ -80,6 +101,16 @@ func NewFileSystemSource(dir string, opts ...FileSystemSourceOption) (*FileSyste
80101
return fs, nil
81102
}
82103

104+
func isExcluded(path string, excludedFiles, excludedExtensions map[string]struct{}) bool {
105+
if _, ok := excludedFiles[path]; ok {
106+
return true
107+
}
108+
if _, ok := excludedExtensions[gopath.Ext(path)]; ok {
109+
return true
110+
}
111+
return false
112+
}
113+
83114
// HasNext checks the next item
84115
func (fs *FileSystemSource) HasNext() (bool, error) {
85116
return fs.index < len(fs.items), nil

0 commit comments

Comments
 (0)