diff --git a/leaks.go b/leaks.go index 677182be..5fb992be 100644 --- a/leaks.go +++ b/leaks.go @@ -39,7 +39,6 @@ func start(opts *Options, repoUrl string) { report := getLeaks(repoName) cleanup(repoName) - reportJson, _ := json.MarshalIndent(report, "", "\t") err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644) } @@ -56,29 +55,35 @@ func cleanup(repoName string) { func getLeaks(repoName string) []LeakElem { var ( - out []byte - err error - wg sync.WaitGroup - concurrent = 100 - semaphoreChan = make(chan struct{}, concurrent) - gitLeaks = make(chan LeakElem) + out []byte + err error + commitWG sync.WaitGroup + gitLeakReceiverWG sync.WaitGroup + concurrent = 100 + semaphoreChan = make(chan struct{}, concurrent) + gitLeaks = make(chan LeakElem) + report []LeakElem ) + go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) { + for gitLeak := range gitLeaks { + fmt.Println(gitLeak) + report = append(report, gitLeak) + gitLeakReceiverWG.Done() + } + }(&commitWG, &gitLeakReceiverWG) + out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output() if err != nil { log.Fatalf("error retrieving commits%v\n", err) } commits := bytes.Split(out, []byte("\n")) - for j, currCommitB := range commits { + commitWG.Add(len(commits)) + for _, currCommitB := range commits { currCommit := string(currCommitB) - if j == len(commits)-2 { - break - } - - wg.Add(1) - go func(currCommit string, repoName string) { - defer wg.Done() + go func(currCommit string, repoName string, commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) { + defer commitWG.Done() var leakPrs bool if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { @@ -101,19 +106,14 @@ func getLeaks(repoName string) []LeakElem { for _, line := range lines { leakPrs = checkEntropy(line) if leakPrs { + gitLeakReceiverWG.Add(1) gitLeaks <- LeakElem{line, currCommit} } } - - }(currCommit, repoName) + }(currCommit, repoName, &commitWG, &gitLeakReceiverWG) } - go func() { - for gitLeak := range gitLeaks { - fmt.Println(gitLeak) - report = append(report, gitLeak) - } - }() - wg.Wait() + commitWG.Wait() + gitLeakReceiverWG.Wait() return report } diff --git a/main.go b/main.go index 8d8ebcde..ceef175c 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ var ( appRoot string regexes map[string]*regexp.Regexp assignRegex *regexp.Regexp - report []LeakElem ) func init() {