Skip to content

Commit

Permalink
fix: exclude list may contain glob entries (#62)
Browse files Browse the repository at this point in the history
* fix: input list may contain glob entries

When the input/exclude list is very large, it may be more convenient to
specify the entries as globs

Signed-off-by: Ramkumar Chinchani <[email protected]>

* fix: ignore paths that don't exist

Signed-off-by: Ramkumar Chinchani <[email protected]>

---------

Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha authored Feb 16, 2024
1 parent b4fae2e commit a72ddbb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
22 changes: 20 additions & 2 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func BuildPackageFromDir(input string, kdoc *k8spdx.Document, kpkg *k8spdx.Packa

fhandle, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}

return err
}
defer fhandle.Close()
Expand Down Expand Up @@ -332,9 +336,23 @@ func BuildPackage(name, author, organization, license,
for _, ipath := range inputPaths {
pinfo, err := os.Lstat(ipath)
if err != nil {
log.Error().Err(err).Str("path", ipath).Msg("unable to stat path")
// check if glob
ifiles, err1 := filepath.Glob(ipath)
if err1 != nil || len(ifiles) == 0 {
log.Error().Err(err).Str("path", ipath).Msg("unable to stat path")

return err
return err
}

for _, ifile := range ifiles {
log.Info().Str("file", ifile).Str("package", pkgname).Msg("adding file to package")

if err := BuildPackageFromFile(ifile, kpkg, license); err != nil {
return err
}
}

continue
}

if pinfo.IsDir() {
Expand Down
13 changes: 11 additions & 2 deletions pkg/fs/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ type Inventory struct {
}

func isExcluded(path string, exclude []string) bool {
for _, e := range exclude {
if path == e {
for _, excl := range exclude {
if path == excl {
return true
}

// exclude entry could be a glob
files, _ := filepath.Glob(excl)

for _, fil := range files {
if path == fil {
return true
}
}
}

return false
Expand Down
34 changes: 34 additions & 0 deletions test/build.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load helpers

function setup() {
common_setup
}

function teardown() {
common_teardown
}

@test "build workflow" {
# create folders and files
mkdir -p $BATS_TEST_TMPDIR/dir1/
truncate -s 1k $BATS_TEST_TMPDIR/dir1/file1
truncate -s 1k $BATS_TEST_TMPDIR/dir1/file2

# directory scan
${TOPDIR}/bin/stacker-bom-linux-amd64 build --name dir1 --pkgname pkg1 --pkgversion 1.0.0 --license MIT --path $BATS_TEST_TMPDIR/dir1 -o $BATS_TEST_TMPDIR/dir1.json
[ -f $BATS_TEST_TMPDIR/dir1.json ]
cat $BATS_TEST_TMPDIR/dir1.json | jq .
rm $BATS_TEST_TMPDIR/dir1.json

# directory scan with glob
${TOPDIR}/bin/stacker-bom-linux-amd64 build --name dir1 --pkgname pkg1 --pkgversion 1.0.0 --license MIT --path $BATS_TEST_TMPDIR/dir1/.* -o $BATS_TEST_TMPDIR/dir1.json
[ -f $BATS_TEST_TMPDIR/dir1.json ]
cat $BATS_TEST_TMPDIR/dir1.json | jq .
rm $BATS_TEST_TMPDIR/dir1.json

# file scan with glob
${TOPDIR}/bin/stacker-bom-linux-amd64 build --name dir1 --pkgname pkg1 --pkgversion 1.0.0 --license MIT --path "$BATS_TEST_TMPDIR/dir1/file*" -o $BATS_TEST_TMPDIR/dir1.json
[ -f $BATS_TEST_TMPDIR/dir1.json ]
cat $BATS_TEST_TMPDIR/dir1.json | jq .
rm $BATS_TEST_TMPDIR/dir1.json
}
File renamed without changes.

0 comments on commit a72ddbb

Please sign in to comment.