-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
func RunCommand(name string, dir string, args ...string) (stderr string, exitCode int) { | ||
var errbuf bytes.Buffer | ||
cmd := exec.Command(name, args...) | ||
cmd.Dir = dir | ||
cmd.Stderr = &errbuf | ||
|
||
err := cmd.Run() | ||
stderr = errbuf.String() | ||
|
||
if err != nil { | ||
// try to get the exit code | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
ws := exitError.Sys().(syscall.WaitStatus) | ||
exitCode = ws.ExitStatus() | ||
} else { | ||
// This will happen (in OSX) if `name` is not available in $PATH, | ||
// in this situation, exit code could not be get, and stderr will be | ||
// empty string very likely, so we use the default fail code, and format err | ||
// to string and set to stderr | ||
exitCode = 1 | ||
if stderr == "" { | ||
stderr = err.Error() | ||
} | ||
} | ||
} else { | ||
// success, exitCode should be 0 if go is ok | ||
ws := cmd.ProcessState.Sys().(syscall.WaitStatus) | ||
exitCode = ws.ExitStatus() | ||
} | ||
return | ||
} |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ import ( | |
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"strconv" | ||
"strings" | ||
|