mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Handle clang-format's stderr more nicely.
This commit is contained in:
1 parent
ddba103284
commit
7e18d9b549
2 files changed
+11
-13
No files matched your search
@@ -7,8 +7,8 @@ package cgen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
@@ -34,18 +34,13 @@ func (g *Generator) WritePostamble(w *bytes.Buffer) error {
|
||||
}
|
||||
|
||||
func (g *Generator) Format(rawSource []byte) ([]byte, error) {
|
||||
stdout := &bytes.Buffer{}
|
||||
cmd := exec.Command("clang-format", "-style=Chromium")
|
||||
cmd.Stdin = bytes.NewReader(rawSource)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if _, ok := err.(*exec.ExitError); ok && len(out) > 0 {
|
||||
// Go error messages usually don't end in "\n". The caller, who sees
|
||||
// the error we return here, usually adds the "\n" themselves.
|
||||
if out[len(out)-1] == '\n' {
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
// The exec.ExitError's Error message doesn't include stderr, so we
|
||||
// return that explicitly.
|
||||
return nil, errors.New(string(out))
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, err
|
||||
return stdout.Bytes(), nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/google/puffs/lang/ast"
|
||||
"github.com/google/puffs/lang/check"
|
||||
@@ -28,7 +29,9 @@ type Generator interface {
|
||||
|
||||
func Main(g Generator) {
|
||||
if err := main1(g); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
if _, ok := err.(*exec.ExitError); !ok {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user