Skip to content

Commit

Permalink
Merge pull request #163 from mvdan/gen-go-gofmt
Browse files Browse the repository at this point in the history
schema/gen/go: apply gofmt automatically
  • Loading branch information
warpfork authored Apr 19, 2021
2 parents 2359e69 + 4bb2f09 commit 246262e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 0 additions & 1 deletion node/gendemo/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
// The code generation is triggered by `go:generate` comments in the `doc.go` file.

//go:generate go run gen.go
//go:generate gofmt -w .

package gendemo
1 change: 0 additions & 1 deletion schema/dmt/gen_trigger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//go:generate go run gen.go
//go:generate gofmt -w .

package schemadmt
12 changes: 11 additions & 1 deletion schema/gen/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gengo
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -150,7 +151,16 @@ func withFile(filename string, fn func(io.Writer)) {
// more atomicity via the single write.
buf := new(bytes.Buffer)
fn(buf)
if err := ioutil.WriteFile(filename, buf.Bytes(), 0666); err != nil {

src := buf.Bytes()
// Format the source before writing, just like gofmt would.
// This also prevents us from writing invalid syntax to disk.
src, err := format.Source(src)
if err != nil {
panic(err)
}

if err := ioutil.WriteFile(filename, src, 0666); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit 246262e

Please sign in to comment.