mirror of
https://github.com/securego/gosec.git
synced 2025-11-23 22:15:04 +02:00
Scan the go packages path recursively starting from a root folder
This is replacing the gotool.ImportPaths which seems to have some troubles with Go modules. Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
committed by
Grant Murphy
parent
85221996b6
commit
85eb8a52ab
30
helpers.go
30
helpers.go
@@ -23,6 +23,7 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -357,3 +358,32 @@ func FindVarIdentities(n *ast.BinaryExpr, c *Context) ([]*ast.Ident, bool) {
|
||||
// if nil or error, return false
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// PackagePaths returns a slice with all packages path at given root directory
|
||||
func PackagePaths(root string, exclude *regexp.Regexp) ([]string, error) {
|
||||
if strings.HasSuffix(root, "...") {
|
||||
root = root[0 : len(root)-3]
|
||||
} else {
|
||||
return []string{root}, nil
|
||||
}
|
||||
paths := map[string]bool{}
|
||||
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
|
||||
if filepath.Ext(path) == ".go" {
|
||||
path = filepath.Dir(path)
|
||||
if exclude != nil && exclude.MatchString(path) {
|
||||
return nil
|
||||
}
|
||||
paths[path] = true
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
result := []string{}
|
||||
for path := range paths {
|
||||
result = append(result, path)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user