mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
Fix mock resolver interface implementation
Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
parent
d475e6280a
commit
c2799b35d8
@ -2,7 +2,7 @@ package source
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/anchore/syft/internal/file"
|
"github.com/anchore/syft/internal/file"
|
||||||
@ -35,19 +35,19 @@ func (r MockResolver) String() string {
|
|||||||
|
|
||||||
// FileContentsByLocation fetches file contents for a single location. If the
|
// FileContentsByLocation fetches file contents for a single location. If the
|
||||||
// path does not exist, an error is returned.
|
// path does not exist, an error is returned.
|
||||||
func (r MockResolver) FileContentsByLocation(location Location) (string, error) {
|
func (r MockResolver) FileContentsByLocation(location Location) (io.ReadCloser, error) {
|
||||||
for _, l := range r.Locations {
|
for _, l := range r.Locations {
|
||||||
if l == location {
|
if l == location {
|
||||||
return stringContent(location.Path)
|
return os.Open(location.Path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("no file for location: %v", location)
|
return nil, fmt.Errorf("no file for location: %v", location)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultipleFileContentsByLocation returns the file contents for all specified Locations.
|
// MultipleFileContentsByLocation returns the file contents for all specified Locations.
|
||||||
func (r MockResolver) MultipleFileContentsByLocation(locations []Location) (map[Location]string, error) {
|
func (r MockResolver) MultipleFileContentsByLocation(locations []Location) (map[Location]io.ReadCloser, error) {
|
||||||
results := make(map[Location]string)
|
results := make(map[Location]io.ReadCloser)
|
||||||
for _, l := range locations {
|
for _, l := range locations {
|
||||||
contents, err := r.FileContentsByLocation(l)
|
contents, err := r.FileContentsByLocation(l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -100,17 +100,3 @@ func (r MockResolver) RelativeFileByPath(_ Location, path string) *Location {
|
|||||||
|
|
||||||
return &paths[0]
|
return &paths[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringContent(path string) (string, error) {
|
|
||||||
reader, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(reader)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(b), nil
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user