From 9a61ca2e5b9ee0cfca7f519cfcc1174c81a8fc09 Mon Sep 17 00:00:00 2001 From: "tiny.x" <185120555@qq.com> Date: Thu, 29 Oct 2020 18:28:17 +0800 Subject: [PATCH] fixbug: Parse the docker CLI return message Signed-off-by: tiny.x <185120555@qq.com> --- exec/client.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/exec/client.go b/exec/client.go index 67443d4..8616036 100644 --- a/exec/client.go +++ b/exec/client.go @@ -17,9 +17,11 @@ package exec import ( + "bytes" "context" "errors" "fmt" + "github.com/docker/docker/pkg/stdcopy" "io/ioutil" "os" "strings" @@ -144,7 +146,7 @@ func (c *Client) executeAndRemove(config *container.Config, hostConfig *containe if removed { c.stopAndRemoveContainer(containerId, &timeout) } - return containerId, handleResponseResult(output), nil + return containerId, output, nil } // waitAndGetOutput returns the result @@ -227,12 +229,21 @@ func (c *Client) execContainerWithConf(containerId, command string, config types return "", err } defer resp.Close() - bytes, err := ioutil.ReadAll(resp.Reader) + stdout := new(bytes.Buffer) + stderr := new(bytes.Buffer) + _, err = stdcopy.StdCopy(stdout, stderr, resp.Reader) if err != nil { logrus.Warningf("Attach exec for container: %s, err: %s", containerId, err.Error()) return "", err } - return handleResponseResult(string(bytes)), nil + result := stdout.String() + errorMsg := stderr.String() + logrus.Debugf("execute result: %s, error msg: %s", result, errorMsg) + if errorMsg != "" { + return "", fmt.Errorf(errorMsg) + } else { + return result, nil + } } //StopContainer @@ -372,16 +383,3 @@ func getChaosBladeImageRef(repo, version string) string { } return fmt.Sprintf("%s:%s", repo, version) } - -// handleResponseResult removes the unused codes in the result -func handleResponseResult(result string) string { - // \u0001\u0000\u0000\u0000\u0000\u0000\u00008{\"code\":200,\"success\":true,\"result\":\"a6e458d76505f898\"}\n - // \u0001\u0000\u0000\u0000\u0000\u0000\u0000file not found\n - result = strings.TrimSpace(result) - index := strings.Index(result, "{") - if index > 0 { - result = result[index:] - } - logrus.Debugf("execute result: %s", result) - return result -}