github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/source/directorysource/directory_source_provider.go (about) 1 package directorysource 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/mitchellh/go-homedir" 8 "github.com/spf13/afero" 9 10 "github.com/anchore/syft/syft/source" 11 ) 12 13 func NewSourceProvider(path string, exclude source.ExcludeConfig, alias source.Alias, basePath string) source.Provider { 14 return &directorySourceProvider{ 15 path: path, 16 basePath: basePath, 17 exclude: exclude, 18 alias: alias, 19 } 20 } 21 22 type directorySourceProvider struct { 23 path string 24 basePath string 25 exclude source.ExcludeConfig 26 alias source.Alias 27 } 28 29 func (l directorySourceProvider) Name() string { 30 return "local-directory" 31 } 32 33 func (l directorySourceProvider) Provide(_ context.Context) (source.Source, error) { 34 location, err := homedir.Expand(l.path) 35 if err != nil { 36 return nil, fmt.Errorf("unable to expand potential directory path: %w", err) 37 } 38 39 fs := afero.NewOsFs() 40 fileMeta, err := fs.Stat(location) 41 if err != nil { 42 return nil, fmt.Errorf("unable to stat location: %w", err) 43 } 44 45 if !fileMeta.IsDir() { 46 return nil, fmt.Errorf("not a directory source: %s", l.path) 47 } 48 49 return New( 50 Config{ 51 Path: location, 52 Base: basePath(l.basePath, location), 53 Exclude: l.exclude, 54 Alias: l.alias, 55 }, 56 ) 57 } 58 59 // FIXME why is the base always being set instead of left as empty string? 60 func basePath(base, location string) string { 61 if base == "" { 62 base = location 63 } 64 return base 65 }