Переглянути джерело

integcli: consolidate cmd exit code processing

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
unclejack 11 роки тому
батько
коміт
97b50e0f29
1 змінених файлів з 11 додано та 27 видалено
  1. 11 27
      integration-cli/utils.go

+ 11 - 27
integration-cli/utils.go

@@ -28,9 +28,7 @@ func getExitCode(err error) (int, error) {
 	return exitCode, fmt.Errorf("failed to get exit code")
 }
 
-func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
-	exitCode = 0
-	out, err := cmd.CombinedOutput()
+func processExitCode(err error) (exitCode int) {
 	if err != nil {
 		var exiterr error
 		if exitCode, exiterr = getExitCode(err); exiterr != nil {
@@ -39,6 +37,13 @@ func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error
 			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
 }
@@ -51,15 +56,8 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex
 	cmd.Stderr = &stderrBuffer
 	cmd.Stdout = &stdoutBuffer
 	err = cmd.Run()
+	exitCode = processExitCode(err)
 
-	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
-		}
-	}
 	stdout = stdoutBuffer.String()
 	stderr = stderrBuffer.String()
 	return
@@ -68,28 +66,14 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex
 func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
 	exitCode = 0
 	err = cmd.Run()
-	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
-		}
-	}
+	exitCode = processExitCode(err)
 	return
 }
 
 func startCommand(cmd *exec.Cmd) (exitCode int, err error) {
 	exitCode = 0
 	err = cmd.Start()
-	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
-		}
-	}
+	exitCode = processExitCode(err)
 	return
 }