From 5b5cd44d680ff040c0ea88785114ac8e6fba77a3 Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Wed, 22 Apr 2026 11:32:23 +1000 Subject: [PATCH] refactor(logs): encapsulate printing errors in PrintEntry --- cmd/uncloud/machine/logs.go | 4 ---- cmd/uncloud/service/logs.go | 4 ---- internal/cli/logs/formatter.go | 12 ++++++++---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cmd/uncloud/machine/logs.go b/cmd/uncloud/machine/logs.go index 2676fb43..36e2b800 100644 --- a/cmd/uncloud/machine/logs.go +++ b/cmd/uncloud/machine/logs.go @@ -125,10 +125,6 @@ func runLogs(ctx context.Context, uncli *cli.CLI, units []string, opts logs.Opti // Print merged logs. for entry := range stream { - if entry.Err != nil { - formatter.PrintError(entry) - continue - } formatter.PrintEntry(entry) } diff --git a/cmd/uncloud/service/logs.go b/cmd/uncloud/service/logs.go index 32f44afb..7249b475 100644 --- a/cmd/uncloud/service/logs.go +++ b/cmd/uncloud/service/logs.go @@ -180,10 +180,6 @@ func runLogs(ctx context.Context, uncli *cli.CLI, args []string, opts logs.Optio // Print merged logs. for entry := range stream { - if entry.Err != nil { - formatter.PrintError(entry) - continue - } formatter.PrintEntry(entry) } diff --git a/internal/cli/logs/formatter.go b/internal/cli/logs/formatter.go index 3b1bd078..87f3e4fc 100644 --- a/internal/cli/logs/formatter.go +++ b/internal/cli/logs/formatter.go @@ -107,6 +107,10 @@ func (f *Formatter) formatService(serviceName, containerID string) string { // PrintEntry prints a single log entry with proper formatting. func (f *Formatter) PrintEntry(entry api.ServiceLogEntry) { + if entry.Err != nil { + f.printError(entry) + return + } if entry.Stream != api.LogStreamStdout && entry.Stream != api.LogStreamStderr { return } @@ -136,11 +140,11 @@ func (f *Formatter) PrintEntry(entry api.ServiceLogEntry) { } } -// PrintError prints an error entry (e.g., stalled stream warning). -func (f *Formatter) PrintError(entry api.ServiceLogEntry) { +// printError prints an error entry (e.g., stalled stream warning). +func (f *Formatter) printError(entry api.ServiceLogEntry) { if entry.Metadata.ServiceName == "" { msg := fmt.Sprintf("ERROR: %v", entry.Err) - style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.BrightRed) // Bold bright red. + style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.BrightRed) fmt.Fprintln(os.Stderr, style.Render(msg)) return } @@ -163,7 +167,7 @@ func (f *Formatter) PrintError(entry api.ServiceLogEntry) { msg += fmt.Sprintf(": %v", entry.Err) } - style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("11")) // Bold bright yellow. + style := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.BrightYellow) fmt.Fprintln(os.Stderr, style.Render(msg)) }