From c0f2cc45e506d123b7478750d874495a1716743d Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 23 May 2023 15:45:14 +0300 Subject: [PATCH] fix(logging): fix n format logging --- dockerfiles/testing/Dockerfile.first | 1 - pkg/docker/build.go | 54 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/dockerfiles/testing/Dockerfile.first b/dockerfiles/testing/Dockerfile.first index 4b8a1b6..f2f7ef7 100644 --- a/dockerfiles/testing/Dockerfile.first +++ b/dockerfiles/testing/Dockerfile.first @@ -2,6 +2,5 @@ ARG BASE_IMAGE=ubuntu:focal FROM ${BASE_IMAGE} RUN apt-get update && apt-get install -y ascii -RUN echoe "im error" CMD [ "sleep", "infinity" ] \ No newline at end of file diff --git a/pkg/docker/build.go b/pkg/docker/build.go index bb707d7..081a9be 100644 --- a/pkg/docker/build.go +++ b/pkg/docker/build.go @@ -28,6 +28,10 @@ type ErrorDetail struct { Message string `json:"message"` } +type BuildLog struct { + Stream string `json:"stream"` +} + func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.Step, lc api.LaunchConfig) error { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { @@ -103,28 +107,6 @@ func Build(ctx context.Context, dfName, dfPath, buildContext string, step api.St return nil } -func printBuildLogs(dst io.Writer, rd io.Reader) error { - var lastLine string - - scanner := bufio.NewScanner(rd) - for scanner.Scan() { - lastLine = scanner.Text() - fmt.Fprintln(dst, lastLine) - } - - errLine := &ErrorLine{} - json.Unmarshal([]byte(lastLine), errLine) - if errLine.Error != "" { - return errors.New(errLine.Error) - } - - if err := scanner.Err(); err != nil { - return err - } - - return nil -} - func BuildMultiplatform(ctx context.Context, dfName, dfPath, buildContext, baseImage string, step api.Step, lc api.LaunchConfig) error { // docker buildx rm multiarch_builder || true @@ -221,3 +203,31 @@ func BuildMultiplatform(ctx context.Context, dfName, dfPath, buildContext, baseI return nil } + +func printBuildLogs(dst io.Writer, rd io.Reader) error { + var lastLine string + + scanner := bufio.NewScanner(rd) + for scanner.Scan() { + lastLine = scanner.Text() + + buildLogLine := &BuildLog{} + json.Unmarshal([]byte(lastLine), buildLogLine) + if buildLogLine.Stream != "" { + fmt.Fprint(dst, buildLogLine.Stream) + } + + errLine := &ErrorLine{} + json.Unmarshal([]byte(lastLine), errLine) + if errLine.Error != "" { + fmt.Fprint(dst, errLine.Error) + return errors.New(errLine.Error) + } + } + + if err := scanner.Err(); err != nil { + return err + } + + return nil +}