Skip to content

Commit

Permalink
Refactor code to move item and secret processing to separate functions
Browse files Browse the repository at this point in the history
(cherry picked from commit 3bc1589)
  • Loading branch information
Baruch Odem committed Feb 14, 2024
1 parent 9839c0e commit 9a996bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
22 changes: 2 additions & 20 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,10 @@ func preRun(cmd *cobra.Command, args []string) error {
}

channels.WaitGroup.Add(1)
go func() {
defer channels.WaitGroup.Done()

wgItems := &sync.WaitGroup{}
for item := range channels.Items {
report.TotalItemsScanned++
wgItems.Add(1)
go secrets.Detect(item, secretsChan, wgItems, ignoreVar)
}
wgItems.Wait()
close(secretsChan)
}()
go processItems(secrets)

channels.WaitGroup.Add(1)
go func() {
defer channels.WaitGroup.Done()
for secret := range secretsChan {
report.TotalSecretsFound++
report.Results[secret.ID] = append(report.Results[secret.ID], secret)

}
}()
go processSecrets()

return nil
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"sync"

"github.com/checkmarx/2ms/secrets"
)

func processItems(detector *secrets.Secrets) {
defer channels.WaitGroup.Done()

wgItems := &sync.WaitGroup{}
for item := range channels.Items {
report.TotalItemsScanned++
wgItems.Add(1)
go detector.Detect(item, secretsChan, wgItems, ignoreVar)
}
wgItems.Wait()
close(secretsChan)
}

func processSecrets() {
defer channels.WaitGroup.Done()

for secret := range secretsChan {
report.TotalSecretsFound++
report.Results[secret.ID] = append(report.Results[secret.ID], secret)
}
}

0 comments on commit 9a996bd

Please sign in to comment.