From 9a996bdd15c213d9ab52a6a7f6ce37fa9c3c8ac7 Mon Sep 17 00:00:00 2001 From: Baruch Odem Date: Wed, 14 Feb 2024 11:28:59 +0200 Subject: [PATCH] Refactor code to move item and secret processing to separate functions (cherry picked from commit 3bc1589ff71829a4c677b2c3422c80157d22f752) --- cmd/main.go | 22 ++-------------------- cmd/workers.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 cmd/workers.go diff --git a/cmd/main.go b/cmd/main.go index 827483f7..f266cced 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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 } diff --git a/cmd/workers.go b/cmd/workers.go new file mode 100644 index 00000000..c5ef1770 --- /dev/null +++ b/cmd/workers.go @@ -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) + } +}