syft/syft/source/filesource/file_source_provider.go
Keith Zantow 7ac4d91f43
chore: reformat (#3754)
Signed-off-by: Keith Zantow <kzantow@gmail.com>
2025-03-21 06:13:35 -04:00

59 lines
1.3 KiB
Go

package filesource
import (
"context"
"crypto"
"fmt"
"github.com/spf13/afero"
"github.com/anchore/go-homedir"
"github.com/anchore/syft/syft/source"
)
func NewSourceProvider(path string, exclude source.ExcludeConfig, digestAlgorithms []crypto.Hash, alias source.Alias) source.Provider {
return &fileSourceProvider{
path: path,
exclude: exclude,
digestAlgorithms: digestAlgorithms,
alias: alias,
}
}
type fileSourceProvider struct {
path string
exclude source.ExcludeConfig
digestAlgorithms []crypto.Hash
alias source.Alias
}
func (p fileSourceProvider) Name() string {
return "local-file"
}
func (p fileSourceProvider) Provide(_ context.Context) (source.Source, error) {
location, err := homedir.Expand(p.path)
if err != nil {
return nil, fmt.Errorf("unable to expand potential directory path: %w", err)
}
fs := afero.NewOsFs()
fileMeta, err := fs.Stat(location)
if err != nil {
return nil, fmt.Errorf("unable to stat location: %w", err)
}
if fileMeta.IsDir() {
return nil, fmt.Errorf("not a file source: %s", p.path)
}
return New(
Config{
Path: location,
Exclude: p.exclude,
DigestAlgorithms: p.digestAlgorithms,
Alias: p.alias,
},
)
}