From 7ed6733fb731e6ec9c4f616b50afb91c5c427456 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 21 Jan 2025 00:21:16 +0700 Subject: [PATCH] cmd/cli: better error if internal log is not available --- cmd/cli/commands.go | 10 ++++++---- cmd/cli/log_writer.go | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/cli/commands.go b/cmd/cli/commands.go index 4e32a7d..8713ea5 100644 --- a/cmd/cli/commands.go +++ b/cmd/cli/commands.go @@ -31,6 +31,10 @@ import ( ) func initLogCmd() *cobra.Command { + warnRuntimeLoggingNotEnabled := func() { + mainLog.Load().Warn().Msg("runtime debug logging is not enabled") + mainLog.Load().Warn().Msg(`ctrld may be running without "--cd" flag or logging is already enabled`) + } logSendCmd := &cobra.Command{ Use: "send", Short: "Send runtime debug logs to ControlD", @@ -54,8 +58,7 @@ func initLogCmd() *cobra.Command { mainLog.Load().Warn().Msg("runtime logs could only be sent once per minute") return case http.StatusMovedPermanently: - mainLog.Load().Warn().Msg("runtime debugs log is not enabled") - mainLog.Load().Warn().Msg(`ctrld may be run without "--cd" flag or logging is already enabled`) + warnRuntimeLoggingNotEnabled() return } var logs logSentResponse @@ -92,8 +95,7 @@ func initLogCmd() *cobra.Command { switch resp.StatusCode { case http.StatusMovedPermanently: - mainLog.Load().Warn().Msg("runtime debugs log is not enabled") - mainLog.Load().Warn().Msg(`ctrld may be run without "--cd" flag or logging is already enabled`) + warnRuntimeLoggingNotEnabled() return case http.StatusBadRequest: mainLog.Load().Warn().Msg("runtime debugs log is not available") diff --git a/cmd/cli/log_writer.go b/cmd/cli/log_writer.go index c146f4e..92e0e63 100644 --- a/cmd/cli/log_writer.go +++ b/cmd/cli/log_writer.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "strings" "sync" "time" @@ -165,7 +166,7 @@ func (p *prog) logReader() (*logReader, error) { return lr, nil } if p.cfg.Service.LogPath == "" { - return nil, nil + return &logReader{r: io.NopCloser(strings.NewReader(""))}, nil } f, err := os.Open(normalizeLogFilePath(p.cfg.Service.LogPath)) if err != nil {