From b35a4124675867625cb61c47cf13f22b1b0de76a Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Tue, 7 Jul 2020 17:05:11 -0400 Subject: [PATCH] scope: use a generic scope struct instead of specific to images Signed-off-by: Alfredo Deza --- imgbom/scope/scope.go | 131 +++++++++++++----------------------------- 1 file changed, 41 insertions(+), 90 deletions(-) diff --git a/imgbom/scope/scope.go b/imgbom/scope/scope.go index 1dba71a5f..1079249eb 100644 --- a/imgbom/scope/scope.go +++ b/imgbom/scope/scope.go @@ -2,127 +2,78 @@ package scope import ( "fmt" - "io/ioutil" - "os" - "path" - "path/filepath" - "github.com/anchore/imgbom/internal/log" + "github.com/anchore/imgbom/imgbom/scope/resolvers" "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/stereoscope/pkg/image" ) -type DirectoryScope struct { - Option Option - Path string +type ImageSource struct { + Img *image.Image } -func (s DirectoryScope) String() string { - return fmt.Sprintf("dir://%s", s.Path) +type DirSource struct { + Path string } -func (s DirectoryScope) FilesByPath(userPaths ...file.Path) ([]file.Reference, error) { - var references = make([]file.Reference, 0) - - for _, userPath := range userPaths { - resolvedPath := path.Join(s.Path, string(userPath)) - _, err := os.Stat(resolvedPath) - if os.IsNotExist(err) { - continue - } else if err != nil { - log.Errorf("path (%s) is not valid: %v", resolvedPath, err) - } - filePath := file.Path(resolvedPath) - references = append(references, file.NewFileReference(filePath)) - } - - return references, nil -} - -func fileContents(path file.Path) ([]byte, error) { - contents, err := ioutil.ReadFile(string(path)) - - if err != nil { - return nil, err - } - return contents, nil -} - -func (s DirectoryScope) FilesByGlob(patterns ...string) ([]file.Reference, error) { - result := make([]file.Reference, 0) - - for _, pattern := range patterns { - pathPattern := path.Join(s.Path, pattern) - matches, err := filepath.Glob(pathPattern) - if err != nil { - return result, err - } - for _, match := range matches { - fileMeta, err := os.Stat(match) - if err != nil { - continue - } - if fileMeta.IsDir() { - continue - } - matchedPath := file.Path(match) - result = append(result, file.NewFileReference(matchedPath)) - } - } - - return result, nil -} - -func (s DirectoryScope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) { - refContents := make(map[file.Reference]string) - for _, fileRef := range f { - contents, err := fileContents(fileRef.Path) - if err != nil { - return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path) - } - refContents[fileRef] = string(contents) - } - return refContents, nil -} - -type ImageScope struct { +type Scope struct { Option Option - resolver FileResolver - Image *image.Image + resolver Resolver + ImgSrc ImageSource + DirSrc DirSource } -func NewDirScope(path string, option Option) (DirectoryScope, error) { - return DirectoryScope{ +func NewScopeFromDir(path string, option Option) (Scope, error) { + return Scope{ Option: option, - Path: path, + resolver: &resolvers.DirectoryResolver{ + Path: path, + }, + DirSrc: DirSource{ + Path: path, + }, }, nil } -func NewImageScope(img *image.Image, option Option) (ImageScope, error) { +func NewScopeFromImage(img *image.Image, option Option) (Scope, error) { if img == nil { - return ImageScope{}, fmt.Errorf("no image given") + return Scope{}, fmt.Errorf("no image given") } - resolver, err := getFileResolver(img, option) + resolver, err := getImageResolver(img, option) if err != nil { - return ImageScope{}, fmt.Errorf("could not determine file resolver: %w", err) + return Scope{}, fmt.Errorf("could not determine file resolver: %w", err) } - return ImageScope{ + return Scope{ Option: option, resolver: resolver, - Image: img, + ImgSrc: ImageSource{ + Img: img, + }, }, nil } -func (s ImageScope) FilesByPath(paths ...file.Path) ([]file.Reference, error) { +func (s Scope) FilesByPath(paths ...file.Path) ([]file.Reference, error) { return s.resolver.FilesByPath(paths...) } -func (s ImageScope) FilesByGlob(patterns ...string) ([]file.Reference, error) { +func (s Scope) FilesByGlob(patterns ...string) ([]file.Reference, error) { return s.resolver.FilesByGlob(patterns...) } -func (s ImageScope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) { - return s.Image.MultipleFileContentsByRef(f...) +func (s Scope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) { + return s.resolver.MultipleFileContentsByRef(f...) +} + +// return either a dir source or img source +func (s Scope) Source() interface{} { + if s.ImgSrc != (ImageSource{}) { + return s.ImgSrc + } + if s.DirSrc != (DirSource{}) { + return s.DirSrc + } + + return nil }