From 6ce2a31785d3e447fee2e8e96d023da43a36dec4 Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Wed, 23 Oct 2024 12:26:49 +0200 Subject: [PATCH] Save unformatted generated code if formatting fails --- pkg/generate/generator.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/generate/generator.go b/pkg/generate/generator.go index e1a118d..3475713 100644 --- a/pkg/generate/generator.go +++ b/pkg/generate/generator.go @@ -2,6 +2,7 @@ package generate import ( "bytes" + "errors" "fmt" "go/format" "io" @@ -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.