2024-11-11 05:36:55 +00:00
|
|
|
package sqlgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2024-11-19 15:30:42 +00:00
|
|
|
//go:embed gen.go.tmpl
|
|
|
|
var fileTemplate string
|
2024-11-11 05:36:55 +00:00
|
|
|
|
2024-11-19 15:30:42 +00:00
|
|
|
func render(driver, schemaPath, outputPath string) error {
|
|
|
|
sch, err := parsePath(driver, schemaPath)
|
2024-11-11 05:36:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-19 15:30:42 +00:00
|
|
|
tmpl := template.Must(template.New("").Parse(fileTemplate))
|
2024-11-11 05:36:55 +00:00
|
|
|
fOut, err := os.Create(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fOut.Close()
|
|
|
|
|
|
|
|
err = tmpl.Execute(fOut, struct {
|
|
|
|
PackageName string
|
|
|
|
Schema *schema
|
|
|
|
}{filepath.Base(filepath.Dir(outputPath)), sch})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.Command("gofmt", "-w", outputPath).Run()
|
|
|
|
}
|