1. func GetCommandResult(cmd *exec.Cmd) ([]byte, int, error) {
  2. output, err := cmd.CombinedOutput()
  3. if err != nil {
  4. if e2, ok := err.(*exec.ExitError); ok {
  5. if status, ok := e2.Sys().(syscall.WaitStatus); ok && runtime.GOOS == "windows" {
  6. s := reflect.ValueOf(&status).Elem()
  7. for i := 0; i < s.NumField(); i++ {
  8. if s.Type().Field(i).Name == "ExitCode" {
  9. // win32
  10. if exitCode, ok := s.Field(i).Interface().(int); ok {
  11. return output, exitCode, nil
  12. }
  13. }
  14. }
  15. } else {
  16. // Linux/everything else
  17. match := regexp.MustCompile(`exit status (\d+)`).FindStringSubmatch(err.Error())
  18. if len(match) == 0 {
  19. panic("exotic operating system detected. ABORT! ABORT!")
  20. }
  21. exitCode, _ := strconv.Atoi(match[1])
  22. return output, exitCode, nil
  23. }
  24. }
  25. fmt.Println(err)
  26. }
  27. return output, 0, nil
  28. }

cross-platform Popen ft Golang

should be renamed to WillGiveYouBrainCancer