mirror of https://github.com/docker/docs.git
46 lines
969 B
Go
46 lines
969 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
func getExitCode(err error) (int, error) {
|
|
exitCode := 0
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
if procExit := exiterr.Sys().(syscall.WaitStatus); ok {
|
|
return procExit.ExitStatus(), nil
|
|
}
|
|
}
|
|
return exitCode, fmt.Errorf("failed to get exit code")
|
|
}
|
|
|
|
func processExitCode(err error) (exitCode int) {
|
|
if err != nil {
|
|
var exiterr error
|
|
if exitCode, exiterr = getExitCode(err); exiterr != nil {
|
|
// TODO: Fix this so we check the error's text.
|
|
// we've failed to retrieve exit code, so we set it to 127
|
|
exitCode = 127
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
|
|
exitCode = 0
|
|
out, err := cmd.CombinedOutput()
|
|
exitCode = processExitCode(err)
|
|
output = string(out)
|
|
return
|
|
|
|
}
|
|
|
|
func stripTrailingCharacters(target string) string {
|
|
target = strings.Trim(target, "\n")
|
|
target = strings.Trim(target, " ")
|
|
return target
|
|
}
|