From 66c5647c687e722dbbfc52e295ddf6613c283654 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 19 Dec 2024 14:59:38 +1000 Subject: [PATCH] Custom directory ignore --- cmd/deplist/deplist.go | 9 ++++++++- deplist.go | 25 ++++++++++++++++++++----- deplist_test.go | 2 +- internal/utils/utils.go | 16 ---------------- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/cmd/deplist/deplist.go b/cmd/deplist/deplist.go index c57d3f3..68b30f3 100644 --- a/cmd/deplist/deplist.go +++ b/cmd/deplist/deplist.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "strings" "github.com/RedHatProductSecurity/deplist" purl "github.com/mcoops/packageurl-go" @@ -12,6 +13,7 @@ import ( func main() { deptypePtr := flag.Int("deptype", -1, "golang, nodejs, python etc") debugPtr := flag.Bool("debug", false, "debug logging (default false)") + ignorePtr := flag.String("ignore", "", "comma separated list of directory names to ignore (default '')") flag.Parse() @@ -19,6 +21,11 @@ func main() { log.SetLevel(log.DebugLevel) } + var ignoreDirs []string + if ignorePtr != nil { + ignoreDirs = strings.Split(*ignorePtr, ",") + } + if flag.Args() == nil || len(flag.Args()) == 0 { fmt.Println("No path to scan was specified, i.e. deplist /tmp/files/") return @@ -26,7 +33,7 @@ func main() { path := flag.Args()[0] - deps, _, err := deplist.GetDeps(path) + deps, _, err := deplist.GetDeps(path, ignoreDirs) if err != nil { fmt.Println(err.Error()) } diff --git a/deplist.go b/deplist.go index 5d09061..a39c0fc 100644 --- a/deplist.go +++ b/deplist.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "github.com/RedHatProductSecurity/deplist/internal/scan" @@ -61,7 +62,17 @@ func addPackagesToDeps(discovered Discovered, pkgs map[string]string, lang Bitma return discovered } -func getDeps(fullPath string) ([]Dependency, Bitmask, error) { +var defaultIgnore []string = []string{ + "node_modules", + "vendor", + "scripts", + "docs", + "test", + "tests", + ".git", +} + +func getDeps(fullPath string, ignoreDirs []string) ([]Dependency, Bitmask, error) { var discovered Discovered // special var so we don't double handle both repos with both // a Gemfile and Gemfile.lock @@ -78,6 +89,9 @@ func getDeps(fullPath string) ([]Dependency, Bitmask, error) { rubyPath := filepath.Join(fullPath, "Gemfile") // Later we translate Gemfile.lock -> Gemfile to handle both cases pythonPath := filepath.Join(fullPath, "requirements.txt") + ignoreDirs = append(ignoreDirs, defaultIgnore...) + log.Debugf("directories ignored: %s", ignoreDirs) + // point at the parent repo, but can't assume where the indicators will be err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -87,7 +101,8 @@ func getDeps(fullPath string) ([]Dependency, Bitmask, error) { if info.IsDir() { // prevent walking down the vendors, docs, etc - if utils.BelongsToIgnoreList(info.Name()) { + if slices.Contains(ignoreDirs, info.Name()) { + log.Debugf("Skipping '%s', directory name '%s' in ignore list", path, info.Name()) return filepath.SkipDir } } else { @@ -293,13 +308,13 @@ func findBaseDir(fullPath string) (string, error) { } // GetDeps scans a given repository and returns all dependencies found in a DependencyList struct. -func GetDeps(fullPath string) ([]Dependency, Bitmask, error) { +func GetDeps(fullPath string, ignoreDirs []string) ([]Dependency, Bitmask, error) { fullPath, err := findBaseDir(fullPath) if err != nil { return nil, 0, err } - deps, foundTypes, err := getDeps(fullPath) + deps, foundTypes, err := getDeps(fullPath, ignoreDirs) if err != nil { return deps, foundTypes, err } @@ -309,7 +324,7 @@ func GetDeps(fullPath string) ([]Dependency, Bitmask, error) { fullPath = filepath.Join(fullPath, "src") if _, err := os.Stat(fullPath); err != nil { log.Debugf("No deps found, trying %s", fullPath) - deps, foundTypes, _ = getDeps(fullPath) + deps, foundTypes, _ = getDeps(fullPath, ignoreDirs) } } diff --git a/deplist_test.go b/deplist_test.go index 1d38523..2f7717c 100644 --- a/deplist_test.go +++ b/deplist_test.go @@ -352,7 +352,7 @@ func depToKey(pkg Dependency) string { func TestGetDeps(t *testing.T) { want := BuildWant() - got, gotBitmask, err := GetDeps("test/testRepo") + got, gotBitmask, err := GetDeps("test/testRepo", []string{}) if err != nil { t.Errorf("GetDeps failed: %s", err) return diff --git a/internal/utils/utils.go b/internal/utils/utils.go index ae8b1d7..728f2c1 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -5,22 +5,6 @@ import ( "path/filepath" ) -// BelongsToIgnoreList is fastest way we can do a string compare on a list -func BelongsToIgnoreList(needle string) bool { - switch needle { - case - "node_modules", - "vendor", - "scripts", - "docs", - "test", - "tests", - ".git": - return true - } - return false -} - func CharIsDigit(c string) bool { if len(c) == 0 { return false