[lxc-devel] [lxd/master] add exit code to RunCommand funcs error output

s3rj1k on Github lxc-bot at linuxcontainers.org
Tue Oct 30 10:46:08 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 348 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181030/fffb59ba/attachment.bin>
-------------- next part --------------
From bb34986ad9cbaa4eebc4de8b9e311be130f4acce Mon Sep 17 00:00:00 2001
From: s3rj1k <evasive.gyron at gmail.com>
Date: Tue, 30 Oct 2018 12:44:56 +0200
Subject: [PATCH] add exit code to RunCommand funcs error output

Signed-off-by: s3rj1k <evasive.gyron at gmail.com>
---
 shared/util.go | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 85706702f5..86e7b2b4d9 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -24,6 +24,7 @@ import (
 	"runtime"
 	"strconv"
 	"strings"
+	"syscall"
 	"time"
 
 	"github.com/lxc/lxd/shared/cancel"
@@ -918,10 +919,22 @@ func (e RunError) Error() string {
 func RunCommand(name string, arg ...string) (string, error) {
 	output, err := exec.Command(name, arg...).CombinedOutput()
 	if err != nil {
+
+		var exitCode = 255
+
+		exitError, ok := err.(*exec.ExitError)
+		if ok {
+			status, ok := exitError.Sys().(syscall.WaitStatus)
+			if ok {
+				exitCode = status.ExitStatus()
+			}
+		}
+
 		err := RunError{
-			msg: fmt.Sprintf("Failed to run: %s %s: %s", name, strings.Join(arg, " "), strings.TrimSpace(string(output))),
+			msg: fmt.Sprintf("Failed to run with ExitCode=%d: %s %s: %s", exitCode, name, strings.Join(arg, " "), strings.TrimSpace(string(output))),
 			Err: err,
 		}
+
 		return string(output), err
 	}
 
@@ -944,8 +957,19 @@ func RunCommandWithFds(stdin io.Reader, stdout io.Writer, name string, arg ...st
 
 	err := cmd.Run()
 	if err != nil {
+
+		var exitCode = 255
+
+		exitError, ok := err.(*exec.ExitError)
+		if ok {
+			status, ok := exitError.Sys().(syscall.WaitStatus)
+			if ok {
+				exitCode = status.ExitStatus()
+			}
+		}
+
 		err := RunError{
-			msg: fmt.Sprintf("Failed to run: %s %s: %s", name, strings.Join(arg, " "),
+			msg: fmt.Sprintf("Failed to run with ExitCode=%d: %s %s: %s", exitCode, name, strings.Join(arg, " "),
 				strings.TrimSpace(buffer.String())),
 			Err: err,
 		}


More information about the lxc-devel mailing list