Skip to content

Commit

Permalink
lint: replace cyclop, gocyclo with revive; basic pkg/hubtest helper (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jun 7, 2024
1 parent dd6cf2b commit 13fb252
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 71 deletions.
24 changes: 13 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,6 +221,13 @@ linters:
- structcheck
- varcheck

#
# Redundant
#

- gocyclo # revive
- cyclop # revive

#
# Disabled until fixed for go 1.22
#
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -503,3 +501,7 @@ issues:
- revive
path: cmd/crowdsec-cli/copyfile.go

- linters:
- revive
path: pkg/hubtest/hubtest_item.go
text: "cyclomatic: .*RunWithLogFile"
8 changes: 2 additions & 6 deletions pkg/hubtest/appsecrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 16 additions & 36 deletions pkg/hubtest/hubtest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions pkg/hubtest/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/hubtest/postoverflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/hubtest/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 13fb252

Please sign in to comment.