From 13c1d7c99084e9df5275e7e695f9b6ae55ebb0ff Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Fri, 22 Nov 2024 20:23:17 -0500 Subject: [PATCH] fix: read and send container logs line-by-line This ensures that each line is sent unbroken which is important for when the logs are in a specific format that will eventually be parsed --- main.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 3c96064..c2d8e3e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "context" "flag" "io" @@ -66,11 +67,17 @@ func containerStart(ctx context.Context, logger *slog.Logger, client *client.Cli readCloser, err := client.ContainerLogs(ctx, containerID, opts) if err != nil { logger.Error("unable to fetch container logs", "err", err) + return err } + go func() { - _, err = io.Copy(reconn, readCloser) - if err != nil { - logger.Error("cannot write to pipe topic", "err", err) + scanner := bufio.NewScanner(readCloser) + for scanner.Scan() { + line := scanner.Text() + _, err = reconn.Write([]byte(line + "\n")) + if err != nil { + logger.Error("cannot write to pipe topic", "err", err) + } } }()