diff --git a/v2/internal/runner/runner.go b/v2/internal/runner/runner.go index 4c805d3f..c4366b96 100644 --- a/v2/internal/runner/runner.go +++ b/v2/internal/runner/runner.go @@ -349,7 +349,7 @@ func (r *Runner) RunEnumeration() error { if err := store.ValidateTemplates(r.options.Templates, r.options.Workflows); err != nil { return err } - if stats.GetValue("syntax-warnings") == 0 && stats.GetValue("syntax-errors") == 0 { + if stats.GetValue(parsers.SyntaxErrorStats) == 0 && stats.GetValue(parsers.SyntaxWarningStats) == 0 { gologger.Info().Msgf("All templates validated successfully\n") } else { return errors.New("encountered errors while performing template validation") @@ -358,8 +358,8 @@ func (r *Runner) RunEnumeration() error { } // Display stats for any loaded templates syntax warnings or errors - stats.Display("syntax-warnings") - stats.Display("syntax-errors") + stats.Display(parsers.SyntaxWarningStats) + stats.Display(parsers.SyntaxErrorStats) builder := &strings.Builder{} if r.templatesConfig != nil && r.templatesConfig.NucleiLatestVersion != "" { diff --git a/v2/pkg/parsers/parser.go b/v2/pkg/parsers/parser.go index 105e879d..861d1a6d 100644 --- a/v2/pkg/parsers/parser.go +++ b/v2/pkg/parsers/parser.go @@ -92,12 +92,17 @@ var ( fieldErrorRegexp = regexp.MustCompile(`not found in`) ) +const ( + SyntaxWarningStats = "syntax-warnings" + SyntaxErrorStats = "syntax-errors" +) + func init() { parsedTemplatesCache = cache.New() - stats.NewEntry("syntax-warnings", "Got %d syntax warnings for the loaded templates") - stats.NewEntry("syntax-errors", "Got %d syntax errors for the loaded templates") + stats.NewEntry(SyntaxWarningStats, "Got %d syntax warnings for the loaded templates") + stats.NewEntry(SyntaxErrorStats, "Got %d syntax errors for the loaded templates") } // ParseTemplate parses a template and returns a *templates.Template structure @@ -122,10 +127,10 @@ func ParseTemplate(templatePath string) (*templates.Template, error) { if err != nil { errString := err.Error() if !fieldErrorRegexp.MatchString(errString) { - stats.Increment("syntax-errors") + stats.Increment(SyntaxErrorStats) return nil, err } - stats.Increment("syntax-warnings") + stats.Increment(SyntaxWarningStats) if ShouldValidate { gologger.Error().Msgf("Syntax warnings for template %s: %s", templatePath, err) } else { diff --git a/v2/pkg/utils/stats/stats.go b/v2/pkg/utils/stats/stats.go index 45916cb1..d25d1433 100644 --- a/v2/pkg/utils/stats/stats.go +++ b/v2/pkg/utils/stats/stats.go @@ -61,11 +61,10 @@ func (s *Storage) NewEntry(name, description string) { func (s *Storage) Increment(name string) { s.mutex.RLock() data, ok := s.data[name] + s.mutex.RUnlock() if !ok { - s.mutex.RUnlock() return } - s.mutex.RUnlock() atomic.AddInt64(&data.value, 1) } @@ -74,11 +73,10 @@ func (s *Storage) Increment(name string) { func (s *Storage) Display(name string) { s.mutex.RLock() data, ok := s.data[name] + s.mutex.RUnlock() if !ok { - s.mutex.RUnlock() return } - s.mutex.RUnlock() dataValue := atomic.LoadInt64(&data.value) if dataValue == 0 { @@ -91,11 +89,10 @@ func (s *Storage) Display(name string) { func (s *Storage) GetValue(name string) int64 { s.mutex.RLock() data, ok := s.data[name] + s.mutex.RUnlock() if !ok { - s.mutex.RUnlock() return 0 } - s.mutex.RUnlock() dataValue := atomic.LoadInt64(&data.value) return dataValue