Skip to content

Commit

Permalink
fix(logging): fix n format logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tunahanertekin committed May 23, 2023
1 parent 517a669 commit c0f2cc4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
1 change: 0 additions & 1 deletion dockerfiles/testing/Dockerfile.first
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
54 changes: 32 additions & 22 deletions pkg/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit c0f2cc4

Please sign in to comment.