Created
July 21, 2014 10:04
-
-
Save tmtk75/587e61de7bab0a4b73b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // This provides a way to see exit status | |
| // | |
| package main | |
| import ( | |
| "fmt" | |
| "os/exec" | |
| "syscall" | |
| ) | |
| type ExitError struct { | |
| Err error | |
| status int | |
| } | |
| func (self *ExitError) Error() string { | |
| return fmt.Sprintf("%s:%v", self.Err.Error(), self.ExitStatus()) | |
| } | |
| func (self *ExitError) ExitStatus() int { | |
| if exiterr, ok := self.Err.(*exec.ExitError); ok { | |
| if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { | |
| return status.ExitStatus() | |
| } | |
| } | |
| return self.status | |
| } | |
| func Exec(path string, body []byte) ([]byte, *ExitError) { | |
| path, err := exec.LookPath(path) | |
| if err != nil { | |
| if _, ok := err.(*exec.Error); ok { | |
| return nil, &ExitError{err, -1} | |
| } else { | |
| panic(err) | |
| } | |
| } | |
| cmd := exec.Command(path) | |
| stdin, err := cmd.StdinPipe() | |
| if err != nil { | |
| return nil, &ExitError{err, -2} | |
| } | |
| stdin.Write(body) | |
| stdin.Close() | |
| out, err := cmd.Output() | |
| if err != nil { | |
| return nil, &ExitError{err, -3} | |
| } | |
| return out, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment