cataloger: add ability to recurse with doublestar

Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
Alfredo Deza 2020-07-20 15:53:45 -04:00
parent 942cd6eb18
commit 03bbcfa08d
3 changed files with 20 additions and 2 deletions

View File

@ -5,10 +5,10 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
"github.com/bmatcuk/doublestar"
)
type DirectoryResolver struct {
@ -51,7 +51,7 @@ func (s DirectoryResolver) FilesByGlob(patterns ...string) ([]file.Reference, er
for _, pattern := range patterns {
pathPattern := path.Join(s.Path, pattern)
matches, err := filepath.Glob(pathPattern)
matches, err := doublestar.Glob(pathPattern)
if err != nil {
return result, err
}

View File

@ -138,6 +138,7 @@ func TestDirectoryResolver_FilesByGlobMultiple(t *testing.T) {
t.Run("finds multiple matching files", func(t *testing.T) {
resolver := DirectoryResolver{"test-fixtures"}
refs, err := resolver.FilesByGlob("image-symlinks/file*")
if err != nil {
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
}
@ -149,6 +150,22 @@ func TestDirectoryResolver_FilesByGlobMultiple(t *testing.T) {
})
}
func TestDirectoryResolver_FilesByGlobRecursive(t *testing.T) {
t.Run("finds multiple matching files", func(t *testing.T) {
resolver := DirectoryResolver{"test-fixtures"}
refs, err := resolver.FilesByGlob("**/*.txt")
if err != nil {
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
}
if len(refs) != 4 {
t.Errorf("unexpected number of refs: %d != 4", len(refs))
}
})
}
func TestDirectoryResolver_FilesByGlobSingle(t *testing.T) {
t.Run("finds multiple matching files", func(t *testing.T) {
resolver := DirectoryResolver{"test-fixtures"}