diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index 0824d00..29a68dd 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -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() @@ -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() { diff --git a/pkg/fs/inventory.go b/pkg/fs/inventory.go index c85ac39..7e9cc50 100644 --- a/pkg/fs/inventory.go +++ b/pkg/fs/inventory.go @@ -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 diff --git a/test/build.bats b/test/build.bats new file mode 100644 index 0000000..3a3f919 --- /dev/null +++ b/test/build.bats @@ -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 +} diff --git a/test/bom.bats b/test/distro.bats similarity index 100% rename from test/bom.bats rename to test/distro.bats