go/sqlgen/template.go
2024-11-30 07:35:54 +01:00

41 lines
726 B
Go

package sqlgen
import (
_ "embed"
"os"
"os/exec"
"path/filepath"
"text/template"
)
//go:embed gen.go.tmpl
var fileTemplate string
func render(driver, schemaPath, outputPath string) (err error) {
outputPath, err = filepath.Abs(outputPath)
if err != nil {
return err
}
sch, err := parsePath(driver, schemaPath)
if err != nil {
return err
}
tmpl := template.Must(template.New("").Parse(fileTemplate))
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()
}