Skip to content

Commit

Permalink
gofmt the generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil committed Aug 7, 2024
1 parent c5d8a2a commit 14f1242
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
r.ExternalName = config.IdentifierFromProvider
r.ShortGroup = "{{ .ShortGroup }}"
r.Kind = "{{ .Kind }}"
{{ range $key, $value := .References }}
r.References["{{$key}}"] = config.Reference{
Type: "{{$value}}",
}
{{ end }}
})
{{ end }}
61 changes: 42 additions & 19 deletions config/tools/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -21,7 +23,12 @@ const ProviderGoFilePath = "config/provider.go"
const ExternalNameGoFilePath = "config/external_name.go"

func parseTemplates() (*template.Template, error) {
return template.ParseFiles("config_templates.tmpl")
templatePath := "config/config_templates.tmpl"
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
log.Fatalf("Failed to parse templates: %v", err)
}
return tmpl, err
}

func generateConfigFile(resourceConfig tools.ResourceConfig) error {
Expand Down Expand Up @@ -50,27 +57,27 @@ func createNewConfigFile(filePath string, resourceConfig tools.ResourceConfig) e
return err
}

file, err := os.Create(filePath)
var buf bytes.Buffer
templates := []string{"initialConfigTemplate", "resourceConfigTemplate"}
for _, tmplName := range templates {
if err = tmpl.ExecuteTemplate(&buf, tmplName, resourceConfig); err != nil {
return err
}
}

formattedCode, err := format.Source(buf.Bytes())
if err != nil {
return err
return fmt.Errorf("failed to format generated code: %w", err)
}

cleanFilePath := filepath.Clean(filePath)
if !strings.HasPrefix(cleanFilePath, "config/") {
return fmt.Errorf("invalid file path: %s", cleanFilePath)
}

defer func() {
if cerr := file.Close(); cerr != nil && err == nil {
err = cerr
}
}()

templates := []string{"initialConfigTemplate", "resourceConfigTemplate"}
for _, tmplName := range templates {
if err = tmpl.ExecuteTemplate(file, tmplName, resourceConfig); err != nil {
return err
}
err = os.WriteFile(filePath, formattedCode, 0644)
if err != nil {
return err
}

return nil
Expand All @@ -90,16 +97,23 @@ func updateExistingConfigFile(content []byte, filePath string, resourceConfig to
}

var tmplOutput bytes.Buffer
if err := tmpl.ExecuteTemplate(&tmplOutput, "updateConfigTemplate", resourceConfig); err != nil {
if err := tmpl.ExecuteTemplate(&tmplOutput, "updateExistingConfigTemplate", resourceConfig); err != nil {
return err
}

insertionPoint := strings.LastIndex(contentStr, "}")
if insertionPoint == -1 {
return errors.New("failed to find insertion point in config.go")
}

updatedContent := contentStr[:insertionPoint] + tmplOutput.String() + contentStr[insertionPoint:]
return os.WriteFile(filePath, []byte(updatedContent), 0600)

formattedCode, err := format.Source([]byte(updatedContent))
if err != nil {
return fmt.Errorf("failed to format updated code: %w", err)
}

return os.WriteFile(filePath, formattedCode, 0600)
}

func updateProviderGo(resourceConfig tools.ResourceConfig) error {
Expand Down Expand Up @@ -134,7 +148,12 @@ func updateProviderGo(resourceConfig tools.ResourceConfig) error {
// Insert the new line at the correct position
updatedContent := contentStr[:actualInsertionPoint] + newLine + contentStr[actualInsertionPoint:]

return os.WriteFile(ProviderGoFilePath, []byte(updatedContent), 0600)
formattedCode, err := format.Source([]byte(updatedContent))
if err != nil {
return fmt.Errorf("failed to format provider.go: %w", err)
}

return os.WriteFile(ProviderGoFilePath, formattedCode, 0600)
}

func updateExternalNameGo(resourceConfig tools.ResourceConfig) error {
Expand All @@ -158,16 +177,20 @@ func updateExternalNameGo(resourceConfig tools.ResourceConfig) error {
// Insert the new line at the correct position
updatedContent := contentStr[:insertionPoint] + newLine + contentStr[insertionPoint:]

formattedCode, err := format.Source([]byte(updatedContent))
if err != nil {
return fmt.Errorf("failed to format external_name.go: %w", err)
}

// Write the updated content back to the file
return os.WriteFile(ExternalNameGoFilePath, []byte(updatedContent), 0600)
return os.WriteFile(ExternalNameGoFilePath, formattedCode, 0600)
}

return nil
}

func main() {
jsonData, err := io.ReadAll(os.Stdin)

if err != nil {
fmt.Printf("Error reading JSON input: %v\n", err)
return
Expand Down

0 comments on commit 14f1242

Please sign in to comment.