Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(terraform): remove unused custom error #6303

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions pkg/iac/scanners/terraform/parser/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package parser

import (
"context"
"errors"
"fmt"
"io/fs"
"path"
Expand All @@ -14,15 +13,6 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/terraform"
)

type moduleLoadError struct {
source string
err error
}

func (m *moduleLoadError) Error() string {
return fmt.Sprintf("failed to load module '%s': %s", m.source, m.err)
}

type ModuleDefinition struct {
Name string
Path string
Expand All @@ -32,41 +22,23 @@ type ModuleDefinition struct {
External bool
}

// LoadModules reads all module blocks and loads the underlying modules, adding blocks to e.moduleBlocks
// loadModules reads all module blocks and loads them
func (e *evaluator) loadModules(ctx context.Context) []*ModuleDefinition {

blocks := e.blocks

var moduleDefinitions []*ModuleDefinition

expanded := e.expandBlocks(blocks.OfType("module"))

var loadErrors []*moduleLoadError
expanded := e.expandBlocks(e.blocks.OfType("module"))

for _, moduleBlock := range expanded {
if moduleBlock.Label() == "" {
continue
}
moduleDefinition, err := e.loadModule(ctx, moduleBlock)
if err != nil {
var loadErr *moduleLoadError
if errors.As(err, &loadErr) {
var found bool
for _, fm := range loadErrors {
if fm.source == loadErr.source {
found = true
break
}
}
if !found {
loadErrors = append(loadErrors, loadErr)
}
continue
}
e.debug.Log("Failed to load module '%s'. Maybe try 'terraform init'?", err)
e.debug.Log("Failed to load module %q. Maybe try 'terraform init'?", err)
continue
}
e.debug.Log("Loaded module '%s' from '%s'.", moduleDefinition.Name, moduleDefinition.Path)

e.debug.Log("Loaded module %q from %q.", moduleDefinition.Name, moduleDefinition.Path)
moduleDefinitions = append(moduleDefinitions, moduleDefinition)
}

Expand Down