(cmd *exec.Cmd, pathPrefix string, envars []string, shouldPipeStdErr bool)
| 53 | } |
| 54 | |
| 55 | func ExecuteCommand(cmd *exec.Cmd, pathPrefix string, envars []string, shouldPipeStdErr bool) error { |
| 56 | |
| 57 | cmd.Dir = pathPrefix |
| 58 | if !filepath.IsAbs(pathPrefix) { |
| 59 | dir := GetCwd() |
| 60 | cmd.Dir = path.Join(dir, pathPrefix) |
| 61 | } |
| 62 | |
| 63 | stdoutPipe, _ := cmd.StdoutPipe() |
| 64 | stderrPipe, _ := cmd.StderrPipe() |
| 65 | |
| 66 | var errStdout, errStderr error |
| 67 | errContent := new(bytes.Buffer) |
| 68 | |
| 69 | cmd.Env = os.Environ() |
| 70 | if envars != nil { |
| 71 | cmd.Env = append(os.Environ(), envars...) |
| 72 | } |
| 73 | |
| 74 | err := cmd.Start() |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | go func() { |
| 80 | _, errStdout = io.Copy(os.Stdout, stdoutPipe) |
| 81 | }() |
| 82 | go func() { |
| 83 | stderrStreams := []io.Writer{errContent} |
| 84 | if shouldPipeStdErr { |
| 85 | stderrStreams = append(stderrStreams, os.Stderr) |
| 86 | } |
| 87 | stdErr := io.MultiWriter(stderrStreams...) |
| 88 | _, errStderr = io.Copy(stdErr, stderrPipe) |
| 89 | }() |
| 90 | |
| 91 | err = cmd.Wait() |
| 92 | if err != nil { |
| 93 | // Detecting and returning the makefile error to cmd |
| 94 | // Passing alone makefile stderr as error message, otherwise it just says "exit status 2" |
| 95 | if exitError, ok := err.(*exec.ExitError); ok { |
| 96 | ws := exitError.Sys().(syscall.WaitStatus) |
| 97 | exitCode := ws.ExitStatus() |
| 98 | if exitCode == 2 { |
| 99 | stderrOut := errContent.String() |
| 100 | isMissingTarget, _ := regexp.MatchString("No rule to make target", stderrOut) |
| 101 | if isMissingTarget { |
| 102 | return errors.New("Module missing mandatory targets, this is likely an issue with the module itself.") |
| 103 | } |
| 104 | return errors.New(stderrOut) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return errors.New(errContent.String()) |
| 109 | } |
| 110 | |
| 111 | if errStdout != nil { |
| 112 | log.Printf("Failed to capture stdout: %v\n", errStdout) |
no test coverage detected