Skip to content

Commit

Permalink
Custom directory ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
sfowl committed Dec 19, 2024
1 parent deac06f commit 66c5647
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
9 changes: 8 additions & 1 deletion cmd/deplist/deplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"strings"

"github.com/RedHatProductSecurity/deplist"
purl "github.com/mcoops/packageurl-go"
Expand All @@ -12,21 +13,27 @@ 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()

if *debugPtr == true {
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
}

path := flag.Args()[0]

deps, _, err := deplist.GetDeps(path)
deps, _, err := deplist.GetDeps(path, ignoreDirs)
if err != nil {
fmt.Println(err.Error())
}
Expand Down
25 changes: 20 additions & 5 deletions deplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"

"github.com/RedHatProductSecurity/deplist/internal/scan"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion deplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66c5647

Please sign in to comment.