From 13fb25257134090d14ec717f70c7d871d9ad730c Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:03:23 +0200 Subject: [PATCH] lint: replace cyclop, gocyclo with revive; basic pkg/hubtest helper (#3065) --- .golangci.yml | 24 +++++++++-------- pkg/hubtest/appsecrule.go | 8 ++---- pkg/hubtest/hubtest_item.go | 52 ++++++++++++------------------------- pkg/hubtest/parser.go | 8 ++---- pkg/hubtest/postoverflow.go | 8 ++---- pkg/hubtest/scenario.go | 8 ++---- 6 files changed, 37 insertions(+), 71 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 1ec386183e1..8feb9921175 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,10 +1,6 @@ # https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml linters-settings: - cyclop: - # lower this after refactoring - max-complexity: 45 - gci: sections: - standard @@ -20,10 +16,6 @@ linters-settings: # lower this after refactoring min-complexity: 128 - gocyclo: - # lower this after refactoring - min-complexity: 45 - funlen: # Checks the number of lines in a function. # If lower than 0, disable the check. @@ -133,7 +125,8 @@ linters-settings: - name: confusing-results disabled: true - name: cyclomatic - disabled: true + # lower this after refactoring + arguments: [45] - name: deep-exit disabled: true - name: defer @@ -228,6 +221,13 @@ linters: - structcheck - varcheck + # + # Redundant + # + + - gocyclo # revive + - cyclop # revive + # # Disabled until fixed for go 1.22 # @@ -243,7 +243,6 @@ linters: # - asciicheck # checks that all code identifiers does not have non-ASCII symbols in the name # - bidichk # Checks for dangerous unicode character sequences # - bodyclose # checks whether HTTP response body is closed successfully - # - cyclop # checks function and package cyclomatic complexity # - decorder # check declaration order and count of types, constants, variables and functions # - depguard # Go linter that checks if package imports are in a list of acceptable packages # - dupword # checks for duplicate words in the source code @@ -259,7 +258,6 @@ linters: # - gochecksumtype # Run exhaustiveness checks on Go "sum types" # - gocognit # Computes and checks the cognitive complexity of functions # - gocritic # Provides diagnostics that check for bugs, performance and style issues. - # - gocyclo # Computes and checks the cyclomatic complexity of functions # - goheader # Checks is file header matches to pattern # - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. # - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. @@ -503,3 +501,7 @@ issues: - revive path: cmd/crowdsec-cli/copyfile.go + - linters: + - revive + path: pkg/hubtest/hubtest_item.go + text: "cyclomatic: .*RunWithLogFile" diff --git a/pkg/hubtest/appsecrule.go b/pkg/hubtest/appsecrule.go index fb4ad78cc18..1c4416c2e9b 100644 --- a/pkg/hubtest/appsecrule.go +++ b/pkg/hubtest/appsecrule.go @@ -25,12 +25,8 @@ func (t *HubTestItem) installAppsecRuleItem(item *cwhub.Item) error { // runtime/appsec-rules/ itemTypeDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath) - if err := os.MkdirAll(hubDirAppsecRuleDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", hubDirAppsecRuleDest, err) - } - - if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) + if err := createDirs([]string{hubDirAppsecRuleDest, itemTypeDirDest}); err != nil { + return err } // runtime/hub/appsec-rules/crowdsecurity/rule.yaml diff --git a/pkg/hubtest/hubtest_item.go b/pkg/hubtest/hubtest_item.go index 4b105777952..5346fb0be50 100644 --- a/pkg/hubtest/hubtest_item.go +++ b/pkg/hubtest/hubtest_item.go @@ -380,6 +380,16 @@ func (t *HubTestItem) RunWithNucleiTemplate() error { return nil } +func createDirs(dirs []string) error { + for _, dir := range dirs { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + return fmt.Errorf("unable to create directory '%s': %w", dir, err) + } + } + + return nil +} + func (t *HubTestItem) RunWithLogFile() error { testPath := filepath.Join(t.HubTestPath, t.Name) if _, err := os.Stat(testPath); os.IsNotExist(err) { @@ -391,30 +401,15 @@ func (t *HubTestItem) RunWithLogFile() error { return fmt.Errorf("can't get current directory: %+v", err) } - // create runtime folder - if err = os.MkdirAll(t.RuntimePath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimePath, err) - } - - // create runtime data folder - if err = os.MkdirAll(t.RuntimeDataPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimeDataPath, err) - } - - // create runtime hub folder - if err = os.MkdirAll(t.RuntimeHubPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimeHubPath, err) + // create runtime, data, hub folders + if err = createDirs([]string{t.RuntimePath, t.RuntimeDataPath, t.RuntimeHubPath, t.ResultsPath}); err != nil { + return err } if err = Copy(t.HubIndexFile, filepath.Join(t.RuntimeHubPath, ".index.json")); err != nil { return fmt.Errorf("unable to copy .index.json file in '%s': %w", filepath.Join(t.RuntimeHubPath, ".index.json"), err) } - // create results folder - if err = os.MkdirAll(t.ResultsPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.ResultsPath, err) - } - // copy template config file to runtime folder if err = Copy(t.TemplateConfigPath, t.RuntimeConfigFilePath); err != nil { return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateConfigPath, t.RuntimeConfigFilePath, err) @@ -585,30 +580,15 @@ func (t *HubTestItem) Run() error { t.Success = false t.ErrorsList = make([]string, 0) - // create runtime folder - if err = os.MkdirAll(t.RuntimePath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimePath, err) - } - - // create runtime data folder - if err = os.MkdirAll(t.RuntimeDataPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimeDataPath, err) - } - - // create runtime hub folder - if err = os.MkdirAll(t.RuntimeHubPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimeHubPath, err) + // create runtime, data, hub, result folders + if err = createDirs([]string{t.RuntimePath, t.RuntimeDataPath, t.RuntimeHubPath, t.ResultsPath}); err != nil { + return err } if err = Copy(t.HubIndexFile, filepath.Join(t.RuntimeHubPath, ".index.json")); err != nil { return fmt.Errorf("unable to copy .index.json file in '%s': %w", filepath.Join(t.RuntimeHubPath, ".index.json"), err) } - // create results folder - if err = os.MkdirAll(t.ResultsPath, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %+v", t.ResultsPath, err) - } - // copy template config file to runtime folder if err = Copy(t.TemplateConfigPath, t.RuntimeConfigFilePath); err != nil { return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateConfigPath, t.RuntimeConfigFilePath, err) diff --git a/pkg/hubtest/parser.go b/pkg/hubtest/parser.go index d40301e3015..31ff459ca77 100644 --- a/pkg/hubtest/parser.go +++ b/pkg/hubtest/parser.go @@ -23,12 +23,8 @@ func (t *HubTestItem) installParserItem(item *cwhub.Item) error { // runtime/parsers/s00-raw/ itemTypeDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, item.Stage) - if err := os.MkdirAll(hubDirParserDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", hubDirParserDest, err) - } - - if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) + if err := createDirs([]string{hubDirParserDest, itemTypeDirDest}); err != nil { + return err } // runtime/hub/parsers/s00-raw/crowdsecurity/syslog-logs.yaml diff --git a/pkg/hubtest/postoverflow.go b/pkg/hubtest/postoverflow.go index 76a67b58b76..65fd0bfbc5d 100644 --- a/pkg/hubtest/postoverflow.go +++ b/pkg/hubtest/postoverflow.go @@ -23,12 +23,8 @@ func (t *HubTestItem) installPostoverflowItem(item *cwhub.Item) error { // runtime/postoverflows/s00-enrich itemTypeDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, item.Stage) - if err := os.MkdirAll(hubDirPostoverflowDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", hubDirPostoverflowDest, err) - } - - if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) + if err := createDirs([]string{hubDirPostoverflowDest, itemTypeDirDest}); err != nil { + return err } // runtime/hub/postoverflows/s00-enrich/crowdsecurity/rdns.yaml diff --git a/pkg/hubtest/scenario.go b/pkg/hubtest/scenario.go index 35ea465b7c0..7f61e48accf 100644 --- a/pkg/hubtest/scenario.go +++ b/pkg/hubtest/scenario.go @@ -22,12 +22,8 @@ func (t *HubTestItem) installScenarioItem(item *cwhub.Item) error { // runtime/parsers/scenarios/ itemTypeDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath) - if err := os.MkdirAll(hubDirScenarioDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", hubDirScenarioDest, err) - } - - if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil { - return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err) + if err := createDirs([]string{hubDirScenarioDest, itemTypeDirDest}); err != nil { + return err } // runtime/hub/scenarios/crowdsecurity/ssh-bf.yaml