Skip to content

Commit

Permalink
Support not formatting generated file.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim authored and dave committed Oct 13, 2022
1 parent 0affa7c commit 290fbca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions jen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type File struct {
comments []string
headers []string
cgoPreamble []string
// NoFormat can be set to true to disable formatting of the generated source.
NoFormat bool
// If you're worried about generated package aliases conflicting with local variable names, you
// can set a prefix here. Package foo becomes {prefix}_foo.
PackagePrefix string
Expand Down
12 changes: 8 additions & 4 deletions jen/jen.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ func (f *File) Render(w io.Writer) error {
if _, err := source.Write(body.Bytes()); err != nil {
return err
}
formatted, err := format.Source(source.Bytes())
if err != nil {
return fmt.Errorf("Error %s while formatting source:\n%s", err, source.String())
finalSource := source.Bytes()
if !f.NoFormat {
formatted, err := format.Source(source.Bytes())
if err != nil {
return fmt.Errorf("Error %s while formatting source:\n%s", err, source.String())
}
finalSource = formatted
}
if _, err := w.Write(formatted); err != nil {
if _, err := w.Write(finalSource); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 290fbca

Please sign in to comment.