piping cli help output leads to error, fix it by ignoring syscall.EPIPE

without this fix - template tries to write to /dev/stdout even after its closed leading to a crash as below

~~~
panic: template: help:21:20: executing "help" at < https://s3.amazonaw...>: write /dev/stdout: broken pipe
~~~

Ignore syscall.EPIPE since its an expected result
This commit is contained in:
Harshavardhana
2015-08-19 11:27:50 -07:00
parent 56705ec1c6
commit fe594e1b26
2 changed files with 17 additions and 10 deletions
+9 -2
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"strings"
"syscall"
"io/ioutil"
"text/tabwriter"
@@ -135,8 +136,14 @@ func (a *App) Run(arguments []string) (err error) {
w := tabwriter.NewWriter(a.Writer, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
err := t.Execute(w, data)
if err != nil {
panic(err)
switch e := err.(type) {
case *os.PathError:
if e.Err == syscall.EPIPE {
break
}
if err != nil {
panic(err)
}
}
w.Flush()
}