/
app.go
60 lines (51 loc) · 1.25 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package app
import (
"bufio"
"bytes"
"fmt"
"go/format"
"os"
"path/filepath"
"text/template"
"github.com/balabanovds/go-scratch/internal/core"
)
type App struct {
outDir string
}
func New(outDir string) *App {
return &App{
outDir: outDir,
}
}
func (a *App) Generate(data []core.Data) error {
for _, d := range data {
if err := a.processTemplate(d.TemplateFile, d.OutFile, d.Data); err != nil {
return err
}
}
return nil
}
func (a *App) processTemplate(filename string, outfile string, data any) error {
tmpl := template.Must(template.New("").ParseFiles(filename))
var processed bytes.Buffer
err := tmpl.ExecuteTemplate(&processed, filename, data)
if err != nil {
return fmt.Errorf("unable to parse data into template: %w", err)
}
formatted, err := format.Source(processed.Bytes())
if err != nil {
return fmt.Errorf("could not format processed template: %w", err)
}
outputPath := filepath.Join(a.outDir, outfile)
fmt.Println("Writing file: ", outputPath)
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("unable to create file %s: %w", outputPath, err)
}
w := bufio.NewWriter(f)
_, err = w.WriteString(string(formatted))
if err != nil {
return fmt.Errorf("failed to write string: %w", err)
}
return w.Flush()
}