44 "bytes"
55 "fmt"
66 "os"
7+ gopath "path"
78 "path/filepath"
89
910 "github.com/pkg/errors"
2021
2122// FileSystemSource is a source implementation to read resources from filesystem
2223type 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
3956func 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
84115func (fs * FileSystemSource ) HasNext () (bool , error ) {
85116 return fs .index < len (fs .items ), nil
0 commit comments