Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
simar7 committed Nov 23, 2024
1 parent e35e099 commit 4d4da05
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions pkg/iac/scanners/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,29 @@ func (p *Parser) ParseFS(ctx context.Context, dir string) error {
}
sort.Strings(paths)
for _, path := range paths {
if err := p.ParseFile(ctx, path); err != nil {
if p.stopOnHCLError {
return err
}
var diags hcl.Diagnostics
if errors.As(err, &diags) {
errc := p.showParseErrors(p.moduleFS, path, diags)
if errc == nil {
continue
}
p.logger.Error("Failed to get the causes of the parsing error", log.Err(errc))
}
p.logger.Error("Error parsing file", log.FilePath(path), log.Err(err))
if err := p.ParseFile(ctx, path); err == nil {
continue
}

if p.stopOnHCLError {
return err
}
var diags hcl.Diagnostics
if errors.As(err, &diags) {
errc := p.showParseErrors(p.moduleFS, path, diags)
if errc == nil {
continue
}
p.logger.Error("Failed to get the causes of the parsing error", log.Err(errc))
}
p.logger.Error("Error parsing file", log.FilePath(path), log.Err(err))
}

return nil
}

func (p *Parser) showParseErrors(fsys fs.FS, path string, diags hcl.Diagnostics) error {
file, err := fsys.Open(path)
func (p *Parser) showParseErrors(fsys fs.FS, filePath string, diags hcl.Diagnostics) error {
file, err := fsys.Open(filePath)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
Expand All @@ -196,7 +197,7 @@ func (p *Parser) showParseErrors(fsys fs.FS, path string, diags hcl.Diagnostics)
}

cause := strings.Join(lines, "\n")
p.logger.Error("Error parsing file", log.FilePath(path),
p.logger.Error("Error parsing file", log.FilePath(filePath),
log.String("cause", cause), log.Err(diag))
}
}
Expand Down

0 comments on commit 4d4da05

Please sign in to comment.