- func GetCommandResult(cmd *exec.Cmd) ([]byte, int, error) {
- output, err := cmd.CombinedOutput()
- if err != nil {
- if e2, ok := err.(*exec.ExitError); ok {
- if status, ok := e2.Sys().(syscall.WaitStatus); ok && runtime.GOOS == "windows" {
- s := reflect.ValueOf(&status).Elem()
- for i := 0; i < s.NumField(); i++ {
- if s.Type().Field(i).Name == "ExitCode" {
- // win32
- if exitCode, ok := s.Field(i).Interface().(int); ok {
- return output, exitCode, nil
- }
- }
- }
- } else {
- // Linux/everything else
- match := regexp.MustCompile(`exit status (\d+)`).FindStringSubmatch(err.Error())
- if len(match) == 0 {
- panic("exotic operating system detected. ABORT! ABORT!")
- }
- exitCode, _ := strconv.Atoi(match[1])
- return output, exitCode, nil
- }
- }
- fmt.Println(err)
- }
- return output, 0, nil
- }
cross-platform Popen ft Golang
should be renamed to WillGiveYouBrainCancer