Cleanup / audit
This commit is contained in:
19
template.go
19
template.go
@@ -1,9 +1,11 @@
|
||||
package sqlgen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
@@ -22,13 +24,9 @@ func render(driver, schemaPath, outputPath string) (err error) {
|
||||
}
|
||||
|
||||
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 {
|
||||
buf := &bytes.Buffer{}
|
||||
err = tmpl.Execute(buf, struct {
|
||||
PackageName string
|
||||
Schema *schema
|
||||
}{filepath.Base(filepath.Dir(outputPath)), sch})
|
||||
@@ -36,5 +34,10 @@ func render(driver, schemaPath, outputPath string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
return exec.Command("gofmt", "-w", outputPath).Run()
|
||||
formatted, err := format.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("formatting generated code: %w", err)
|
||||
}
|
||||
|
||||
return os.WriteFile(outputPath, formatted, 0o644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user