mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 01:01:35 +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 (
|
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 that explicitly.
|
|
||||||
return nil, errors.New(string(out))
|
|
||||||
}
|
}
|
||||||
return out, err
|
return stdout.Bytes(), nil
|
||||||
}
|
}
|
||||||
@@ -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 {
|
||||||
os.Stderr.WriteString(err.Error() + "\n")
|
if _, ok := err.(*exec.ExitError); !ok {
|
||||||
|
os.Stderr.WriteString(err.Error() + "\n")
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user