diff options
-rw-r--r-- | goosebumps.go | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/goosebumps.go b/goosebumps.go index a0f522f..08b71b9 100644 --- a/goosebumps.go +++ b/goosebumps.go @@ -43,6 +43,24 @@ func getmodcache() string { } +type pathcache struct { + m sync.RWMutex + c map[string]bool +} + +func (p *pathcache) get(path string) (ok bool) { + p.m.RLock() + defer p.m.RUnlock() + _, ok = p.c[path] + return +} + +func (p *pathcache) add(path string) { + p.m.Lock() + defer p.m.Unlock() + p.c[path] = true +} + func main() { flag.Parse() @@ -54,16 +72,18 @@ func main() { return } + pc := &pathcache{ + c: map[string]bool{}, + } if !*recurse { file := filepath.Join(*dir, "go.mod") - singleFile(file) - return + singleFile(file, pc) + } else { + recursive(*dir, pc) } - - recursive(*dir) } -func singleFile(file string) { +func singleFile(file string, pc *pathcache) { data, err := os.ReadFile(file) if err != nil { log.Fatal(err) @@ -75,14 +95,13 @@ func singleFile(file string) { } 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 { + if done := pc.get(path); done { continue } - pathcache[path] = true + pc.add(path) + wg.Add(1) go func() { defer wg.Done() checkPath(*cache + "/" + path) @@ -92,7 +111,7 @@ func singleFile(file string) { wg.Wait() } -func recursive(dir string) { +func recursive(dir string, pc *pathcache) { filepath.WalkDir(dir, func(p string, info fs.DirEntry, err error) error { if err != nil { return err @@ -100,7 +119,7 @@ func recursive(dir string) { return nil } else if "go.mod" == filepath.Base(p) { fmt.Printf("analyzing %s\n", p) - singleFile(p) + singleFile(p, pc) } return nil }) |