Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: Extract write to function
  • Loading branch information
erezrokah committed Dec 15, 2022
commit 80729dccc93398f6b9dbb830291a674c79aeb370
39 changes: 22 additions & 17 deletions plugins/source/azuredevops/codegen/recipes/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ type Resource struct {
Table *codegen.TableDefinition
}

func writeTemplateContentToFile(dir string, filePath string, buff bytes.Buffer) error {
outputPath := path.Join(dir, "../..", filePath)
outputDir := path.Dir(outputPath)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create directory %s: %w", outputDir, err)
}

content := buff.Bytes()
formattedContent, err := format.Source(content)
if err != nil {
fmt.Printf("failed to format source: %s: %v\n", filePath, err)
} else {
content = formattedContent
}

if err := os.WriteFile(outputPath, content, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filePath, err)
}
return nil
}

func renderTemplate(name string, filePath string, data interface{}) error {
_, filename, _, ok := runtime.Caller(0)
if !ok {
Expand All @@ -48,24 +69,8 @@ func renderTemplate(name string, filePath string, data interface{}) error {
if err := tpl.Execute(&buff, data); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
outputPath := path.Join(dir, "../..", filePath)
outputDir := path.Dir(outputPath)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create directory %s: %w", outputDir, err)
}

content := buff.Bytes()
formattedContent, err := format.Source(buff.Bytes())
if err != nil {
fmt.Printf("failed to format source: %s: %v\n", outputPath, err)
} else {
content = formattedContent
}

if err := os.WriteFile(filePath, content, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", outputPath, err)
}
return nil
return writeTemplateContentToFile(dir, filePath, buff)
}

func (resource *Resource) generate() error {
Expand Down