Skip to content

Commit

Permalink
Print error on formatter failure (#235)
Browse files Browse the repository at this point in the history
It'd necessary to know why code formatters fail, so we now print the
full output from configured tooling in case of error. Some formatters
use stderr and some use stdout, so we join both of them in that message.
  • Loading branch information
nfx authored Dec 8, 2022
1 parent 1d260ec commit 8946524
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion openapi/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"flag"
Expand Down Expand Up @@ -152,11 +153,22 @@ func (r *Render) Run() error {
formatter = strings.ReplaceAll(formatter, "$FILENAMES",
strings.Join(filenames, " "))
split := strings.Split(formatter, " ")

// create pipe to forward stdout and stderr to same fd,
// so that it's clear why formatter failed.
reader, writer := io.Pipe()
out := bytes.NewBuffer([]byte{})
go io.Copy(out, reader)
defer reader.Close()
defer writer.Close()

cmd := exec.Command(split[0], split[1:]...)
cmd.Dir = r.ctx.Target
cmd.Stdout = writer
cmd.Stderr = writer
err := cmd.Run()
if err != nil {
return fmt.Errorf("%s: %w", formatter, err)
return fmt.Errorf("%s:\n%s", formatter, out.Bytes())
}
}
return nil
Expand Down

0 comments on commit 8946524

Please sign in to comment.