lint: replace cyclop, gocyclo with revive; basic pkg/hubtest helper (#3065)

This commit is contained in:
mmetc 2024-06-07 19:03:23 +02:00 committed by GitHub
parent dd6cf2b844
commit 13fb252571
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 37 additions and 71 deletions

View file

@ -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"

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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