-
Notifications
You must be signed in to change notification settings - Fork 8
/
tmplrun.go
84 lines (75 loc) · 2.08 KB
/
tmplrun.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// package tmplrun provides a Runner for templated go programs. It is commonly used
// by Go Atlas providers to compile ad-hoc programs that emit the desired SQL schema for
// data models defined for Go ORMs.
package tmplrun
import (
"bytes"
"errors"
"fmt"
"go/format"
"os"
"os/exec"
"strings"
"text/template"
"time"
)
// Runner is a go template runner. It accepts a go template and data, and runs the
// rendered template as a go program.
type Runner struct {
name string
tmpl *template.Template
}
// New returns a new Runner.
func New(name string, tmpl *template.Template) *Runner {
return &Runner{name: name, tmpl: tmpl}
}
// Run runs the template and returns the output.
func (r *Runner) Run(data interface{}) (string, error) {
var buf bytes.Buffer
if err := r.tmpl.Execute(&buf, data); err != nil {
return "", err
}
source, err := format.Source(buf.Bytes())
if err != nil {
return "", err
}
return r.gorun(source)
}
func (r *Runner) gorun(src []byte) (string, error) {
dir := fmt.Sprintf(".%s", r.name)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", err
}
target := fmt.Sprintf("%s/%s.go", dir, r.filename(r.name))
if err := os.WriteFile(target, src, 0644); err != nil {
return "", fmt.Errorf("%s: write file %s: %w", r.name, target, err)
}
defer os.RemoveAll(dir)
return gorun(target)
}
// run 'go run' command and return its output.
func gorun(target string) (string, error) {
s, err := gocmd("run", target)
if err != nil {
return "", fmt.Errorf("gormschema: %s", err)
}
return s, nil
}
// goCmd runs a go command and returns its output.
func gocmd(command, target string) (string, error) {
args := []string{command}
args = append(args, target)
cmd := exec.Command("go", args...)
stderr := bytes.NewBuffer(nil)
stdout := bytes.NewBuffer(nil)
cmd.Stderr = stderr
cmd.Stdout = stdout
if err := cmd.Run(); err != nil {
return "", errors.New(stderr.String())
}
return stdout.String(), nil
}
func (r *Runner) filename(pkg string) string {
name := strings.ReplaceAll(pkg, "/", "_")
return fmt.Sprintf("%s_%s_%d", r.name, name, time.Now().Unix())
}