Remediate all lint findings under the canonical golangci-lint config

Fix every finding surfaced by the canonical .golangci.yml with
golangci-lint v2.12.2 (refs #61), behavior-preserving throughout:

- err113: dynamic errors replaced with package-level sentinels and %w
  wrapping; direct comparisons converted to errors.Is
- goprintffuncname: printf-style helpers renamed with an f suffix
  (ui.Writer message methods, cli.ReportErrorf, database.Fatalf,
  vaultik stdoutf) and all call sites updated
- revive: stuttering type names renamed (blob.Handler, blob.WithReader,
  blob.ChunkPosition, storage.URL, storage.Info), doc comments added,
  unused parameters blanked, package comments added
- contextcheck/noctx: ctx threaded through blob.Packer
  (AddChunk/Flush/FinalizeBlob/PackChunks) and scanner call sites;
  context-aware exec and sql variants used
- funlen/cyclop/gocognit/nestif/dupl: oversized or duplicated
  functions split into focused helpers across production and test code
- paralleltest/tparallel/thelper/usetesting/testpackage: tests
  parallelized where safe (global log.Initialize kept in the serial
  phase), helpers marked, t.TempDir adopted, external test packages
  where only exported API is used
- gosec: integer conversions clamped or justified, header timeouts
  added, remaining findings suppressed with per-site justifications
- mnd/goconst/lll/wsl_v5/nlreturn/noinlineerr/errcheck and other
  mechanical findings fixed directly

Remove the deprecated log.LogOptions alias (callers migrated to
log.Options). make check is green.
This commit is contained in:
2026-08-07 18:51:21 +00:00
parent 6cf9211407
commit 7ae470e530
121 changed files with 8344 additions and 5406 deletions

View File

@@ -4,14 +4,14 @@
//
// Message classes (see Writer methods):
//
// - Begin — operation start, left-aligned, marker "》" (white)
// - Complete— operation completion, left-aligned, marker "》" (green)
// - Info — left-aligned neutral status, marker "》" (white)
// - Notice — left-aligned important note, marker "》" (cyan)
// - Warning — left-aligned warning, full word "Warning: " (orange/yellow)
// - Error — left-aligned error, full word "ERROR: " (red)
// - Progress— indented heartbeat / per-item update, marker " 》" (white)
// - Banner — application banner line, left-aligned, no marker
// - Beginf — operation start, left-aligned, marker "》" (white)
// - Completef— operation completion, left-aligned, marker "》" (green)
// - Infof — left-aligned neutral status, marker "》" (white)
// - Noticef — left-aligned important note, marker "》" (cyan)
// - Warningf — left-aligned warning, full word "Warning: " (orange/yellow)
// - Errorf — left-aligned error, full word "ERROR: " (red)
// - Progressf— indented heartbeat / per-item update, marker " 》" (white)
// - Bannerf — application banner line, left-aligned, no marker
//
// Value formatters (Hex, Size, Duration, Time, Path, Snapshot, Speed,
// Count, Percent) return ANSI-colored strings the caller composes into
@@ -45,6 +45,19 @@ const (
// Marker is the chevron prefix used for all non-error/warning lines.
const Marker = "》"
const (
// hexAbbrevLen is the number of hash characters Hex keeps before "...".
hexAbbrevLen = 12
// bitsPerByte converts bytes/sec into bits/sec for Speed.
bitsPerByte = 8
// SI thresholds for Speed's unit selection, in bits/sec.
gigabit = 1e9
megabit = 1e6
kilobit = 1e3
)
// Writer formats and emits user-facing messages with optional ANSI color.
// It also counts warnings and errors emitted so the caller can summarize at
// the end of an operation ("Finished successfully." vs "Finished with
@@ -103,15 +116,6 @@ func shouldColor(w io.Writer) bool {
return term.IsTerminal(int(f.Fd()))
}
// paint wraps s in the given ANSI color when color is enabled.
func (w *Writer) paint(color, s string) string {
if !w.color {
return s
}
return color + s + ansiReset
}
// ───────────────────────── message methods ─────────────────────────
// Beginf prints an operation-start line, left-aligned with a white marker.
@@ -208,17 +212,6 @@ func (w *Writer) Bannerf(format string, args ...any) {
_, _ = fmt.Fprintln(w.out, body)
}
// emit writes "<prefix> <body>\n" with the prefix painted in prefixColor
// and the body optionally painted in bodyColor (empty = no body color).
func (w *Writer) emit(prefixColor, prefix, bodyColor, format string, args []any) {
body := fmt.Sprintf(format, args...)
if bodyColor != "" {
body = w.paint(bodyColor, body)
}
_, _ = fmt.Fprintln(w.out, w.paint(prefixColor, prefix)+" "+body)
}
// ───────────────────────── value formatters ─────────────────────────
//
// These return ANSI-colored strings the caller composes into a message
@@ -228,8 +221,8 @@ func (w *Writer) emit(prefixColor, prefix, bodyColor, format string, args []any)
// Long hashes are abbreviated to first 12 chars with "...".
func (w *Writer) Hex(s string) string {
short := s
if len(s) > 12 {
short = s[:12] + "..."
if len(s) > hexAbbrevLen {
short = s[:hexAbbrevLen] + "..."
}
return w.paint(ansiCyan, short)
@@ -247,7 +240,7 @@ func (w *Writer) Path(p string) string {
// Size colorizes a byte count using humanize.Bytes.
func (w *Writer) Size(bytes int64) string {
return w.paint(ansiMagenta, humanize.Bytes(uint64(bytes)))
return w.paint(ansiMagenta, humanize.Bytes(uint64(bytes))) //nolint:gosec // G115: >=0
}
// Speed colorizes a network transfer rate. Input is bytes/sec; output is
@@ -258,17 +251,17 @@ func (w *Writer) Speed(bytesPerSec float64) string {
return w.paint(ansiMagenta, "N/A")
}
bitsPerSec := bytesPerSec * 8
bitsPerSec := bytesPerSec * bitsPerByte
var s string
switch {
case bitsPerSec >= 1e9:
s = fmt.Sprintf("%.1f Gbit/sec", bitsPerSec/1e9)
case bitsPerSec >= 1e6:
s = fmt.Sprintf("%.0f Mbit/sec", bitsPerSec/1e6)
case bitsPerSec >= 1e3:
s = fmt.Sprintf("%.0f Kbit/sec", bitsPerSec/1e3)
case bitsPerSec >= gigabit:
s = fmt.Sprintf("%.1f Gbit/sec", bitsPerSec/gigabit)
case bitsPerSec >= megabit:
s = fmt.Sprintf("%.0f Mbit/sec", bitsPerSec/megabit)
case bitsPerSec >= kilobit:
s = fmt.Sprintf("%.0f Kbit/sec", bitsPerSec/kilobit)
default:
s = fmt.Sprintf("%.0f bit/sec", bitsPerSec)
}
@@ -286,7 +279,7 @@ func (w *Writer) Duration(d time.Duration) string {
// "YYYY-MM-DD HH:MM:SS". No timezone is included — values are
// displayed in the process's local zone.
func (w *Writer) Time(t time.Time) string {
t = t.Local()
t = t.Local() //nolint:gosmopolitan // local-time display is intentional
now := time.Now()
if t.Year() == now.Year() && t.YearDay() == now.YearDay() {
@@ -305,3 +298,25 @@ func (w *Writer) Count(n int) string {
func (w *Writer) Percent(p float64) string {
return w.paint(ansiMagenta, fmt.Sprintf("%.1f%%", p))
}
// ───────────────────────── internal helpers ─────────────────────────
// paint wraps s in the given ANSI color when color is enabled.
func (w *Writer) paint(color, s string) string {
if !w.color {
return s
}
return color + s + ansiReset
}
// emit writes "<prefix> <body>\n" with the prefix painted in prefixColor
// and the body optionally painted in bodyColor (empty = no body color).
func (w *Writer) emit(prefixColor, prefix, bodyColor, format string, args []any) {
body := fmt.Sprintf(format, args...)
if bodyColor != "" {
body = w.paint(bodyColor, body)
}
_, _ = fmt.Fprintln(w.out, w.paint(prefixColor, prefix)+" "+body)
}

View File

@@ -1,37 +1,45 @@
package ui
package ui_test
import (
"bytes"
"strings"
"testing"
"time"
"sneak.berlin/go/vaultik/internal/ui"
)
func newTestWriter(color bool) (*Writer, *bytes.Buffer) {
func newTestWriter(color bool) (*ui.Writer, *bytes.Buffer) {
buf := &bytes.Buffer{}
return NewWithColor(buf, color), buf
return ui.NewWithColor(buf, color), buf
}
func TestMessageMethodsPlain(t *testing.T) {
t.Parallel()
tests := []struct {
method string
fn func(*Writer)
fn func(*ui.Writer)
want string
}{
{"Begin", func(w *Writer) { w.Beginf("starting %s", "thing") }, "》 starting thing\n"},
{"Complete", func(w *Writer) { w.Completef("done %s", "thing") }, "》 done thing\n"},
{"Info", func(w *Writer) { w.Infof("status") }, "》 status\n"},
{"Notice", func(w *Writer) { w.Noticef("note") }, "》 note\n"},
{"Warning", func(w *Writer) { w.Warningf("oops") }, "⚠️ Warning: oops\n"},
{"Error", func(w *Writer) { w.Errorf("boom") }, "🛑 ERROR: boom\n"},
{"Progress", func(w *Writer) { w.Progressf("p") }, " 》 p\n"},
{"Detail", func(w *Writer) { w.Detailf("d") }, " 》 d\n"},
{"Banner", func(w *Writer) { w.Bannerf("hello") }, "hello\n"}, // plain mode, no bold
{"Begin", func(w *ui.Writer) { w.Beginf("starting %s", "thing") },
"》 starting thing\n"},
{"Complete", func(w *ui.Writer) { w.Completef("done %s", "thing") },
"》 done thing\n"},
{"Info", func(w *ui.Writer) { w.Infof("status") }, "》 status\n"},
{"Notice", func(w *ui.Writer) { w.Noticef("note") }, "》 note\n"},
{"Warning", func(w *ui.Writer) { w.Warningf("oops") }, "⚠️ Warning: oops\n"},
{"Error", func(w *ui.Writer) { w.Errorf("boom") }, "🛑 ERROR: boom\n"},
{"Progress", func(w *ui.Writer) { w.Progressf("p") }, " 》 p\n"},
{"Detail", func(w *ui.Writer) { w.Detailf("d") }, " 》 d\n"},
{"Banner", func(w *ui.Writer) { w.Bannerf("hello") }, "hello\n"}, // plain, no bold
}
for _, tt := range tests {
t.Run(tt.method, func(t *testing.T) {
t.Parallel()
w, buf := newTestWriter(false)
tt.fn(w)
@@ -43,6 +51,8 @@ func TestMessageMethodsPlain(t *testing.T) {
}
func TestWarningErrorCounters(t *testing.T) {
t.Parallel()
w, _ := newTestWriter(false)
if w.WarningCount() != 0 || w.ErrorCount() != 0 {
t.Fatalf("expected fresh writer to have zero counts")
@@ -63,6 +73,8 @@ func TestWarningErrorCounters(t *testing.T) {
}
func TestColorOutputContainsANSI(t *testing.T) {
t.Parallel()
w, buf := newTestWriter(true)
w.Errorf("boom")
@@ -77,6 +89,8 @@ func TestColorOutputContainsANSI(t *testing.T) {
}
func TestBannerBoldWhenColor(t *testing.T) {
t.Parallel()
w, buf := newTestWriter(true)
w.Bannerf("hello")
@@ -87,6 +101,8 @@ func TestBannerBoldWhenColor(t *testing.T) {
}
func TestValueFormattersPlain(t *testing.T) {
t.Parallel()
w, _ := newTestWriter(false)
if got := w.Hex("0123456789abcdef0123"); got != "0123456789ab..." {
@@ -127,18 +143,26 @@ func TestValueFormattersPlain(t *testing.T) {
}
// Time format: today → HH:MM:SS, other day → YYYY-MM-DD HH:MM:SS.
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 14, 30, 45, 0, time.Local)
// These construct local-zone times on purpose: Writer.Time displays
// in the process's local zone.
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(),
14, 30, 45, 0, time.Local) //nolint:gosmopolitan // local display
if got := w.Time(today); got != "14:30:45" {
t.Errorf("Time today: got %q, want 14:30:45", got)
}
other := time.Date(2030, 1, 2, 3, 4, 5, 0, time.Local)
other := time.Date(2030, 1, 2, 3, 4, 5, 0,
time.Local) //nolint:gosmopolitan // local display
if got := w.Time(other); got != "2030-01-02 03:04:05" {
t.Errorf("Time other day: got %q", got)
}
}
func TestValueFormattersColored(t *testing.T) {
t.Parallel()
w, _ := newTestWriter(true)
hex := w.Hex("0123456789abcdef0123")