Skip to content

Commit

Permalink
allow custom template
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiacobghiula committed May 6, 2024
1 parent 2d508d3 commit 041f59c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
27 changes: 17 additions & 10 deletions cmd/avrogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,35 @@ func realMain(args []string, stdout, stderr io.Writer) int {
}
formatted, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
_, _ = fmt.Fprintf(stderr, "Error: generated code \n%s\n could not be formatted: %v\n", buf.String(), err)
_ = writeOut(cfg.Out, stdout, buf.Bytes())
_, _ = fmt.Fprintf(stderr, "Error: generated code could not be formatted: %v\n", err)
return 3
}

err = writeOut(cfg.Out, stdout, formatted)
if err != nil {
_, _ = fmt.Fprintf(stderr, "Error: %v\n", err)
return 4
}
return 0
}

func writeOut(filename string, stdout io.Writer, bytes []byte) error {
writer := stdout
if cfg.Out != "" {
file, err := os.Create(cfg.Out)
if filename != "" {
file, err := os.Create(filepath.Clean(filename))
if err != nil {
_, _ = fmt.Fprintf(stderr, "Error: could not create output file: %v\n", err)
return 4
return fmt.Errorf("could not create output file: %w", err)
}
defer func() { _ = file.Close() }()

writer = file
}

if _, err := writer.Write(formatted); err != nil {
_, _ = fmt.Fprintf(stderr, "Error: could not write code: %v\n", err)
return 4
if _, err := writer.Write(bytes); err != nil {
return fmt.Errorf("could not write code: %w", err)
}

return 0
return nil
}

func validateOpts(nargs int, cfg config) error {
Expand Down
3 changes: 2 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func StructFromSchema(schema avro.Schema, w io.Writer, cfg Config) error {

formatted, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
return fmt.Errorf("generated code \n%s\n could not be formatted: %w", buf.String(), err)
_, _ = w.Write(buf.Bytes())
return fmt.Errorf("generated code could not be formatted: %w", err)
}

_, err = w.Write(formatted)
Expand Down

0 comments on commit 041f59c

Please sign in to comment.