Handle clang-format's stderr more nicely.

This commit is contained in:
Nigel Tao committed 2017-05-26 11:23:19 +10:00
1 parent ddba103284
commit 7e18d9b549
2 files changed
+10 -12

No files matched your search

+7 -12
View File
@@ -7,8 +7,8 @@ package cgen
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
) )
@@ -34,18 +34,13 @@ func (g *Generator) WritePostamble(w *bytes.Buffer) error {
} }
func (g *Generator) Format(rawSource []byte) ([]byte, error) { func (g *Generator) Format(rawSource []byte) ([]byte, error) {
stdout := &bytes.Buffer{}
cmd := exec.Command("clang-format", "-style=Chromium") cmd := exec.Command("clang-format", "-style=Chromium")
cmd.Stdin = bytes.NewReader(rawSource) cmd.Stdin = bytes.NewReader(rawSource)
out, err := cmd.CombinedOutput() cmd.Stdout = stdout
if _, ok := err.(*exec.ExitError); ok && len(out) > 0 { cmd.Stderr = os.Stderr
// Go error messages usually don't end in "\n". The caller, who sees if err := cmd.Run(); err != nil {
// the error we return here, usually adds the "\n" themselves. return nil, err
if out[len(out)-1] == '\n' {
out = out[:len(out)-1]
} }
// The exec.ExitError's Error message doesn't include stderr, so we return stdout.Bytes(), nil
// return that explicitly.
return nil, errors.New(string(out))
}
return out, err
} }
+3
View File
@@ -8,6 +8,7 @@ import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"github.com/google/puffs/lang/ast" "github.com/google/puffs/lang/ast"
"github.com/google/puffs/lang/check" "github.com/google/puffs/lang/check"
@@ -28,7 +29,9 @@ type Generator interface {
func Main(g Generator) { func Main(g Generator) {
if err := main1(g); err != nil { if err := main1(g); err != nil {
if _, ok := err.(*exec.ExitError); !ok {
os.Stderr.WriteString(err.Error() + "\n") os.Stderr.WriteString(err.Error() + "\n")
}
os.Exit(1) os.Exit(1)
} }
} }