aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorÖzgür Kesim <oec@codeblau.de>2025-08-15 11:23:11 +0200
committerÖzgür Kesim <oec@codeblau.de>2025-08-15 11:23:11 +0200
commit86d626fd8d98484a7e7f01c9d15f4a8100c8c909 (patch)
tree708b882bb25e64947e88d8af5f603d67fd818e4c /main.go
parent15f17c5c92b1421dfe08d292b475b2d91772d6ab (diff)
fixed recursion
Diffstat (limited to 'main.go')
-rw-r--r--main.go36
1 files changed, 32 insertions, 4 deletions
diff --git a/main.go b/main.go
index a8a4e4e..a0f522f 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,6 @@ import (
"go/parser"
"go/token"
"io/fs"
- "io/ioutil"
"log"
"os"
"path/filepath"
@@ -18,7 +17,8 @@ import (
)
var (
- file = flag.String("mod", "go.mod", "go.mod file")
+ dir = flag.String("d", ".", "directory with go.mod file")
+ recurse = flag.Bool("r", false, "recursively search for go.mod files")
cache = flag.String("modcache", getmodcache(), "location of go mod cache")
excheckm = flag.String("exempt", "golang.org", "domains exempt from the search, seperated by space")
@@ -54,20 +54,35 @@ func main() {
return
}
- data, err := ioutil.ReadFile(*file)
+ if !*recurse {
+ file := filepath.Join(*dir, "go.mod")
+ singleFile(file)
+ return
+ }
+
+ recursive(*dir)
+}
+
+func singleFile(file string) {
+ data, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
- mf, err := modfile.Parse(*file, data, nil)
+ mf, err := modfile.Parse(file, data, nil)
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(len(mf.Require))
+ pathcache := map[string]bool{}
for _, r := range mf.Require {
path := strings.Join(r.Syntax.Token, "@")
+ if _, done := pathcache[path]; done {
+ continue
+ }
+ pathcache[path] = true
go func() {
defer wg.Done()
checkPath(*cache + "/" + path)
@@ -75,7 +90,20 @@ func main() {
}
wg.Wait()
+}
+func recursive(dir string) {
+ filepath.WalkDir(dir, func(p string, info fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ } else if info.IsDir() {
+ return nil
+ } else if "go.mod" == filepath.Base(p) {
+ fmt.Printf("analyzing %s\n", p)
+ singleFile(p)
+ }
+ return nil
+ })
}
func isExempt(path string) bool {