summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Sosedoff <dan.sosedoff@gmail.com>2019-03-02 04:16:43 +0100
committerDan Sosedoff <dan.sosedoff@gmail.com>2019-03-02 04:16:43 +0100
commit5330599c93470f755a5e034c14639f7626ccde95 (patch)
treeef8b42108fd19d564144928b340ebe2273b60bd6
parentskip integration test (diff)
downloadforgejo-act-5330599c93470f755a5e034c14639f7626ccde95.tar.xz
forgejo-act-5330599c93470f755a5e034c14639f7626ccde95.zip
Capture errors from docker log output
- Refactored logDockerResponse function to remove extra if-else nesting - logDockerResponse func now returns an error if error was detected from the log stream - logDockerResponse will check for msg.ErrorDetail.Message and bail if there's an error
-rw-r--r--Makefile3
-rw-r--r--container/docker_build.go3
-rw-r--r--container/docker_common.go45
3 files changed, 33 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 2866dcc..35fcfe2 100644
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,9 @@ export GITHUB_TOKEN = $(shell cat ~/.config/github/token)
default: check
+test:
+ go test -cover -short ./...
+
check:
$(ACT) -ra check
diff --git a/container/docker_build.go b/container/docker_build.go
index f885add..8fac5f4 100644
--- a/container/docker_build.go
+++ b/container/docker_build.go
@@ -51,7 +51,8 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
input.Logger.Debugf("Creating image from context dir '%s' with tag '%s'", input.ContextDir, input.ImageTag)
resp, err := cli.ImageBuild(input.Ctx, buildContext, options)
- input.logDockerResponse(resp.Body, err != nil)
+
+ err = input.logDockerResponse(resp.Body, err != nil)
if err != nil {
return err
}
diff --git a/container/docker_common.go b/container/docker_common.go
index 6665cdc..1a33eb0 100644
--- a/container/docker_common.go
+++ b/container/docker_common.go
@@ -4,6 +4,7 @@ import (
"bufio"
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"os"
@@ -64,42 +65,52 @@ func (i *DockerExecutorInput) writeLog(isError bool, format string, args ...inte
}
-func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) {
+func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) error {
if dockerResponse == nil {
- return
+ return nil
}
defer dockerResponse.Close()
scanner := bufio.NewScanner(dockerResponse)
msg := dockerMessage{}
+
for scanner.Scan() {
line := scanner.Bytes()
+
msg.ID = ""
msg.Stream = ""
msg.Error = ""
msg.ErrorDetail.Message = ""
msg.Status = ""
msg.Progress = ""
- if err := json.Unmarshal(line, &msg); err == nil {
- if msg.Error != "" {
- i.writeLog(isError, "%s", msg.Error)
- return
- }
- if msg.Status != "" {
- if msg.Progress != "" {
- i.writeLog(isError, "%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
- } else {
- i.writeLog(isError, "%s :: %s\n", msg.Status, msg.ID)
- }
- } else if msg.Stream != "" {
- i.writeLog(isError, msg.Stream)
+ if err := json.Unmarshal(line, &msg); err != nil {
+ i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
+ continue
+ }
+
+ if msg.Error != "" {
+ i.writeLog(isError, "%s", msg.Error)
+ return errors.New(msg.Error)
+ }
+
+ if msg.ErrorDetail.Message != "" {
+ i.writeLog(isError, "%s", msg.ErrorDetail.Message)
+ return errors.New(msg.Error)
+ }
+
+ if msg.Status != "" {
+ if msg.Progress != "" {
+ i.writeLog(isError, "%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
} else {
- i.writeLog(false, "Unable to handle line: %s", string(line))
+ i.writeLog(isError, "%s :: %s\n", msg.Status, msg.ID)
}
+ } else if msg.Stream != "" {
+ i.writeLog(isError, msg.Stream)
} else {
- i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
+ i.writeLog(false, "Unable to handle line: %s", string(line))
}
}
+ return nil
}