Skip to content

Commit

Permalink
refactor: extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed Apr 2, 2024
1 parent fd1d495 commit 8e3e97f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/iac/ignore/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Ignorer represents a function that checks if the rule should be ignored.
type Ignorer func(resultMeta types.Metadata, param any) bool
type Ignorer func(resultMeta types.Metadata, ignoredParam any) bool

type Rules []Rule

Expand Down
38 changes: 22 additions & 16 deletions pkg/iac/scanners/terraform/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,8 @@ func (e *Executor) Execute(modules terraform.Modules) (scan.Results, Metrics, er
}

ignorers := map[string]ignore.Ignorer{
"ws": func(_ types.Metadata, param any) bool {
ws, ok := param.(string)
if !ok {
return false
}

return ignore.MatchPattern(e.workspaceName, ws)
},
"ignore": func(resultMeta types.Metadata, param any) bool {
params, ok := param.(map[string]string)
if !ok {
return false
}

return ignoreByParams(params, modules, &resultMeta)
},
"ws": workspaceIgnorer(e.workspaceName),
"ignore": attributeIgnorer(modules),
}

results.Ignore(ignores, ignorers)
Expand Down Expand Up @@ -268,3 +254,23 @@ func ignoreByParams(params map[string]string, modules terraform.Modules, m *type
}
return true
}

func workspaceIgnorer(ws string) ignore.Ignorer {
return func(_ types.Metadata, param any) bool {
ignoredWorkspace, ok := param.(string)
if !ok {
return false
}
return ignore.MatchPattern(ws, ignoredWorkspace)
}
}

func attributeIgnorer(modules terraform.Modules) ignore.Ignorer {
return func(resultMeta types.Metadata, param any) bool {
params, ok := param.(map[string]string)
if !ok {
return false
}
return ignoreByParams(params, modules, &resultMeta)
}
}

0 comments on commit 8e3e97f

Please sign in to comment.