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
  • Loading branch information
Baruch Odem committed Feb 14, 2024
1 parent 753c515 commit 3bc1589
Show file tree
Hide file tree
Showing 2 changed files with 36 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 @@ -133,28 +133,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
34 changes: 34 additions & 0 deletions cmd/workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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++
if validateVar {
validationChan <- secret
}
report.Results[secret.ID] = append(report.Results[secret.ID], secret)
}
close(validationChan)
}

0 comments on commit 3bc1589

Please sign in to comment.