diff --git a/internal/journal/journal.go b/internal/journal/journal.go index f1e32764..d5233178 100644 --- a/internal/journal/journal.go +++ b/internal/journal/journal.go @@ -52,18 +52,18 @@ func logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (io.Rea } // follow synchronously follows the io.Reader, writing each new journal entry to channel. -func follow(ctx context.Context, reader io.Reader, outCh chan api.LogEntry) error { +// It stops when the reader is exhausted or the context is cancelled. +func follow(ctx context.Context, reader io.Reader, outCh chan api.LogEntry) { scanner := bufio.NewScanner(reader) for scanner.Scan() { select { case outCh <- entry(scanner.Bytes()): case <-ctx.Done(): - return nil + return } } if err := scanner.Err(); err != nil { outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)} } - return nil } diff --git a/internal/journal/logs.go b/internal/journal/logs.go index 6fb45e38..4a742259 100644 --- a/internal/journal/logs.go +++ b/internal/journal/logs.go @@ -1,7 +1,6 @@ package journal import ( - "bufio" "bytes" "context" "fmt" @@ -16,7 +15,7 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan // Hard code unit check for now switch unit { case "uncloud": - case "corrosion": + case "uncloud-corrosion": case "docker": default: return nil, fmt.Errorf("journal logs: invalid unit: %s", unit) @@ -29,32 +28,10 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan outCh := make(chan api.LogEntry) - switch opts.Follow { - case false: - - go func() { - defer close(outCh) - - scanner := bufio.NewScanner(reader) - for scanner.Scan() { - outCh <- entry(scanner.Bytes()) - } - - if err := scanner.Err(); err != nil { - outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)} - } - }() - - case true: - go func() { - defer close(outCh) - - err := follow(ctx, reader, outCh) - if err != nil { - outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)} - } - }() - } + go func() { + defer close(outCh) + follow(ctx, reader, outCh) + }() return outCh, nil } diff --git a/internal/journal/logs_test.go b/internal/journal/logs_test.go index 7364cb88..4274f55a 100644 --- a/internal/journal/logs_test.go +++ b/internal/journal/logs_test.go @@ -12,8 +12,6 @@ import ( ) func TestLogs(t *testing.T) { - t.Parallel() - commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd { return exec.CommandContext(ctx, "/usr/bin/tail", "testdata/logs") } @@ -29,8 +27,7 @@ func TestLogs(t *testing.T) { for range ch { i++ } - assert.Equal(t, i, 6) - cancel() + assert.Equal(t, 6, i) commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd { return exec.CommandContext(ctx, "/usr/bin/tail", "-f", "testdata/logs") @@ -47,5 +44,6 @@ func TestLogs(t *testing.T) { for range ch { i++ } - assert.Equal(t, i, 6) // still six is hardbeats are not written here. + // Still six because heartbeats are not written here and Tail is ignored as the command is overridden. + assert.Equal(t, 6, i) }