Skip to content

Commit

Permalink
Save unformatted generated code if formatting fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Oct 23, 2024
1 parent 73bffab commit 6ce2a31
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"bytes"
"errors"
"fmt"
"go/format"
"io"
Expand Down Expand Up @@ -149,14 +150,16 @@ func (c *Creator) processTemplate(templateName, filePath string) error {

// writeFormattedContentToFile formats the content and writes it to a file.
func (c *Creator) writeFormattedContentToFile(filePath, content string) error {
formattedCode, err := format.Source([]byte(content))
if err != nil {
log.Printf("provided content: %s", content)
return fmt.Errorf("error formatting code: %w", err)
var formattedCode []byte
var formatErr error
formattedCode, formatErr = format.Source([]byte(content))
if formatErr != nil {
log.Printf("Failed to format target path: %s", filePath)
formattedCode = []byte(content)
}
formattedBuf := bytes.NewBuffer(formattedCode)

return c.createFileAndWriteContent(filePath, formattedBuf)
return errors.Join(formatErr, c.createFileAndWriteContent(filePath, formattedBuf))
}

// createTerraformProviderFilePath returns a file path for a Terraform provider based on the provided suffix.
Expand Down

0 comments on commit 6ce2a31

Please sign in to comment.