mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 08:53:15 +01:00
scope: use a generic scope struct instead of specific to images
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
145bb95a2d
commit
b35a412467
@ -2,127 +2,78 @@ package scope
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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/file"
|
||||||
"github.com/anchore/stereoscope/pkg/image"
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DirectoryScope struct {
|
type ImageSource struct {
|
||||||
Option Option
|
Img *image.Image
|
||||||
|
}
|
||||||
|
|
||||||
|
type DirSource struct {
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s DirectoryScope) String() string {
|
type Scope struct {
|
||||||
return fmt.Sprintf("dir://%s", s.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
Option Option
|
Option Option
|
||||||
resolver FileResolver
|
resolver Resolver
|
||||||
Image *image.Image
|
ImgSrc ImageSource
|
||||||
|
DirSrc DirSource
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirScope(path string, option Option) (DirectoryScope, error) {
|
func NewScopeFromDir(path string, option Option) (Scope, error) {
|
||||||
return DirectoryScope{
|
return Scope{
|
||||||
Option: option,
|
Option: option,
|
||||||
|
resolver: &resolvers.DirectoryResolver{
|
||||||
Path: path,
|
Path: path,
|
||||||
|
},
|
||||||
|
DirSrc: DirSource{
|
||||||
|
Path: path,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewImageScope(img *image.Image, option Option) (ImageScope, error) {
|
func NewScopeFromImage(img *image.Image, option Option) (Scope, error) {
|
||||||
if img == nil {
|
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 {
|
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,
|
Option: option,
|
||||||
resolver: resolver,
|
resolver: resolver,
|
||||||
Image: img,
|
ImgSrc: ImageSource{
|
||||||
|
Img: img,
|
||||||
|
},
|
||||||
}, nil
|
}, 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...)
|
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...)
|
return s.resolver.FilesByGlob(patterns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ImageScope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
func (s Scope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
return s.Image.MultipleFileContentsByRef(f...)
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user