From c7a7e8431cb6c5ddadb772bc20fe61e38e40927d Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Sat, 9 May 2026 02:21:53 +0000 Subject: [PATCH] lint: fix errcheck for fmt.Fprintf/Fprint across multiple files Add lint.Fprint helper for unchecked fmt.Fprint calls. Use lint.Fprintf/Fprint in api/event.go, api/image.go, rmo/root.go. Add explicit error check for fmt.Fprintf in middleware/terminal.go cW. --- api/event.go | 3 ++- api/image.go | 4 ++-- lint/error.go | 8 ++++++++ middleware/terminal.go | 7 +++++-- rmo/root.go | 6 +++--- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/api/event.go b/api/event.go index e5af4948..b742b1fd 100644 --- a/api/event.go +++ b/api/event.go @@ -6,6 +6,7 @@ import ( "net/http" "time" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/event" "github.com/Gleipnir-Technology/nidus-sync/version" @@ -135,7 +136,7 @@ func streamEvents(w http.ResponseWriter, r *http.Request, u platform.User) { return } - fmt.Fprintf(w, "data: %s\n\n", body) + lint.Fprintf(w, "data: %s\n\n", body) w.(http.Flusher).Flush() // Keep the connection open with a ticker sending periodic events diff --git a/api/image.go b/api/image.go index 477f141f..68bedf32 100644 --- a/api/image.go +++ b/api/image.go @@ -2,11 +2,11 @@ package api import ( "encoding/json" - "fmt" "io/ioutil" "net/http" "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/file" "github.com/aarondl/opt/omit" @@ -80,5 +80,5 @@ func apiImageContentPost(w http.ResponseWriter, r *http.Request, u platform.User } w.WriteHeader(http.StatusOK) log.Printf("Saved image file %s\n", imageUUID) - fmt.Fprintf(w, "PNG uploaded successfully") + lint.Fprintf(w, "PNG uploaded successfully") } diff --git a/lint/error.go b/lint/error.go index 37e634ee..d7ea2440 100644 --- a/lint/error.go +++ b/lint/error.go @@ -51,3 +51,11 @@ func Write(w io.Writer, p []byte) { log.Error().Err(err).Msg("write failed") } } + +// Fprint writes a string to w, logging any error. +func Fprint(w io.Writer, a ...any) { + _, err := fmt.Fprint(w, a...) + if err != nil { + log.Error().Err(err).Msg("fprint failed") + } +} diff --git a/middleware/terminal.go b/middleware/terminal.go index 4686e4b7..1bf5f655 100644 --- a/middleware/terminal.go +++ b/middleware/terminal.go @@ -59,9 +59,12 @@ func cW(w io.Writer, useColor bool, color []byte, s string, args ...interface{}) return fmt.Errorf("write color: %w", err) } } - fmt.Fprintf(w, s, args...) + _, err := fmt.Fprintf(w, s, args...) + if err != nil { + return fmt.Errorf("fprintf: %w", err) + } if IsTTY && useColor { - _, err := w.Write(reset) + _, err = w.Write(reset) if err != nil { return fmt.Errorf("write color: %w", err) } diff --git a/rmo/root.go b/rmo/root.go index 407b0782..4b7589af 100644 --- a/rmo/root.go +++ b/rmo/root.go @@ -1,11 +1,11 @@ package rmo import ( - "fmt" "net/http" "github.com/Gleipnir-Technology/nidus-sync/config" "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/lint" "github.com/Gleipnir-Technology/nidus-sync/html" "github.com/rs/zerolog/log" ) @@ -76,8 +76,8 @@ func getRootDistrict(w http.ResponseWriter, r *http.Request) { } func getRobots(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "User-agent: *\n") - fmt.Fprint(w, "Allow: /\n") + lint.Fprint(w, "User-agent: *\n") + lint.Fprint(w, "Allow: /\n") } func getTerms(w http.ResponseWriter, r *http.Request) { html.RenderOrError(