From 742aba2d6900ae651733ccd484925cd8e8c08390 Mon Sep 17 00:00:00 2001 From: Baptiste Canton Date: Sat, 22 Aug 2026 17:54:30 +0200 Subject: [PATCH] chore: go fix (Go 1.27 modernizers) Mechanical rewrite by 'go fix ./...', no behaviour changes: - 17 files: drop the obsolete '// +build' lines kept alongside '//go:build' (only needed for Go < 1.17) - log: continueOnFatal int32 + atomic.Load/Store -> atomic.Int32 - stringutils: hand-rolled loop -> slices.Contains - jsonIndent: negative clamp -> max() - sse, wormhole, googleai: strings.HasPrefix+slicing -> strings.CutPrefix - assorted 'for i := 0; i < n; i++' -> 'for range n' Verified: builds green for default, libcurl/crappy/keystore, nohl/fileonly and plugins/gcp/aws tag sets; go test ./... passes; golangci-lint v2 still reports 0 issues. Co-Authored-By: Claude Fable 5 --- pkg/color/color_test.go | 2 +- pkg/completion/hl.go | 1 - pkg/highlighter/0interface.go | 1 - pkg/highlighter/chroma.go | 1 - pkg/lockable/flock.go | 1 - pkg/log/log.go | 8 ++++---- pkg/miniclaude/messagesapi.go | 2 +- pkg/miniclaude/types.go | 2 +- pkg/mutators/pipeline/pipeline.go | 4 ++-- pkg/mutators/single/googleai.go | 7 ++++--- pkg/mutators/single/jsonIndent.go | 5 +---- pkg/mutators/single/jsonpath.go | 2 +- pkg/mutators/single/ndjson.go | 2 +- pkg/openers/crng.go | 1 - pkg/openers/crng_internal_test.go | 1 - pkg/openers/crng_test.go | 1 - pkg/openers/echo.go | 1 - pkg/openers/echo_test.go | 1 - pkg/openers/gemini.go | 1 - pkg/openers/http.go | 1 - pkg/openers/http_test.go | 5 ++--- pkg/openers/mc.go | 1 - pkg/openers/nc.go | 1 - pkg/openers/prng.go | 1 - pkg/openers/prng_internal_test.go | 1 - pkg/openers/prng_test.go | 1 - pkg/openers/sse.go | 5 ++--- pkg/openers/wormhole.go | 9 ++++----- pkg/pcg/pcg32_test.go | 2 +- pkg/secretprovider/keyring_mock.go | 1 - pkg/stringutils/stringInSlice.go | 9 +++------ pkg/utils/test_helpers.go | 2 +- 32 files changed, 29 insertions(+), 54 deletions(-) diff --git a/pkg/color/color_test.go b/pkg/color/color_test.go index 0272d07c5..17d52f640 100644 --- a/pkg/color/color_test.go +++ b/pkg/color/color_test.go @@ -22,7 +22,7 @@ func testNext(t *testing.T, c color.Color) { t.Helper() t.Run("donotpanicplease", func(t *testing.T) { source := "hi" - for i := 0; i < 100; i++ { + for range 100 { c = c.Next() s := c.Sprint(source) if len(s) <= len(source) { diff --git a/pkg/completion/hl.go b/pkg/completion/hl.go index 77b6f65f0..1c2aa01d4 100644 --- a/pkg/completion/hl.go +++ b/pkg/completion/hl.go @@ -1,5 +1,4 @@ //go:build !nohl -// +build !nohl package completion diff --git a/pkg/highlighter/0interface.go b/pkg/highlighter/0interface.go index 0021f70f6..50eaab5aa 100644 --- a/pkg/highlighter/0interface.go +++ b/pkg/highlighter/0interface.go @@ -1,5 +1,4 @@ //go:build !nohl -// +build !nohl package highlighter diff --git a/pkg/highlighter/chroma.go b/pkg/highlighter/chroma.go index 62bafb01d..92c834914 100644 --- a/pkg/highlighter/chroma.go +++ b/pkg/highlighter/chroma.go @@ -1,5 +1,4 @@ //go:build !nohl -// +build !nohl package highlighter diff --git a/pkg/lockable/flock.go b/pkg/lockable/flock.go index 40eecfa01..3ca23b4db 100644 --- a/pkg/lockable/flock.go +++ b/pkg/lockable/flock.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package lockable diff --git a/pkg/log/log.go b/pkg/log/log.go index c2086abe7..7e8f4fd89 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -15,7 +15,7 @@ var ( Stderr = &Logger{Logger: log.New(os.Stderr, "", flags)} DebugIsDiscard int32 - continueOnFatal int32 + continueOnFatal atomic.Int32 ) func init() { @@ -66,14 +66,14 @@ func Println(v ...any) { func Fatal(v ...any) { Stderr.Output(2, fmt.Sprint(v...)) - if atomic.LoadInt32(&continueOnFatal) <= 0 { + if continueOnFatal.Load() <= 0 { os.Exit(1) } } func Fatalf(format string, v ...any) { Stderr.Output(2, fmt.Sprintf(format, v...)) - if atomic.LoadInt32(&continueOnFatal) <= 0 { + if continueOnFatal.Load() <= 0 { os.Exit(1) } } @@ -91,5 +91,5 @@ func Pp(data any) string { } func SetContinueOnFatal() { - atomic.StoreInt32(&continueOnFatal, 1) + continueOnFatal.Store(1) } diff --git a/pkg/miniclaude/messagesapi.go b/pkg/miniclaude/messagesapi.go index 09d1e0469..97ba3a4dd 100644 --- a/pkg/miniclaude/messagesapi.go +++ b/pkg/miniclaude/messagesapi.go @@ -107,7 +107,7 @@ stream: if eventName == "error" { log.Printf("error: %s", eventData) } - var d map[string]interface{} + var d map[string]any err = json.Unmarshal([]byte(eventData), &d) if err != nil { log.Debugf("error unmarshalling: %s\n%s\n", err, eventData) diff --git a/pkg/miniclaude/types.go b/pkg/miniclaude/types.go index 2a3169472..45c26f632 100644 --- a/pkg/miniclaude/types.go +++ b/pkg/miniclaude/types.go @@ -15,7 +15,7 @@ type Request struct { type MessagesResponse struct { ID string `json:"id"` Type string `json:"type"` // always "message" - Error ErrorBlock `json:"error,omitempty"` + Error ErrorBlock `json:"error"` Role string `json:"role"` // always "assistant" Content []ContentBlock `json:"content"` Model string `json:"model"` diff --git a/pkg/mutators/pipeline/pipeline.go b/pkg/mutators/pipeline/pipeline.go index eee88f3e5..fc131df35 100644 --- a/pkg/mutators/pipeline/pipeline.go +++ b/pkg/mutators/pipeline/pipeline.go @@ -26,8 +26,8 @@ func NewPipeline(description string, out io.WriteCloser, in io.ReadCloser) error globalPipeline.mu.Unlock() return errors.New("empty pipeline requested") } - list := strings.Split(description, mutators.StageSeparator) - for _, m := range list { + list := strings.SplitSeq(description, mutators.StageSeparator) + for m := range list { log.Debugf("creating %v\n", m) mutator, err := mutators.New(m) if err != nil { diff --git a/pkg/mutators/single/googleai.go b/pkg/mutators/single/googleai.go index 704208c81..e560c6e57 100644 --- a/pkg/mutators/single/googleai.go +++ b/pkg/mutators/single/googleai.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "strings" "github.com/batmac/ccat/pkg/log" "github.com/google/generative-ai-go/genai" @@ -55,15 +56,15 @@ func googleai(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) { return 0, err } - p := "" + var p strings.Builder for i, c := range resp.Candidates[0].Content.Parts { log.Debugln("part ", i, ": ", c) if t, ok := c.(genai.Text); ok { - p += string(t) + p.WriteString(string(t)) } } - n, err := io.WriteString(w, p) + n, err := io.WriteString(w, p.String()) if err != nil { return 0, err } diff --git a/pkg/mutators/single/jsonIndent.go b/pkg/mutators/single/jsonIndent.go index 8da0911b1..eb33a7337 100644 --- a/pkg/mutators/single/jsonIndent.go +++ b/pkg/mutators/single/jsonIndent.go @@ -14,10 +14,7 @@ func init() { } func jsonIndent(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) { - indent := cfgInt(config) - if indent < 0 { - indent = 0 - } + indent := max(cfgInt(config), 0) j, err := io.ReadAll(r) // NOT streamable if err != nil { return 0, err diff --git a/pkg/mutators/single/jsonpath.go b/pkg/mutators/single/jsonpath.go index fa18730ce..d61a224dd 100644 --- a/pkg/mutators/single/jsonpath.go +++ b/pkg/mutators/single/jsonpath.go @@ -26,7 +26,7 @@ func jSONPath(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) { return 0, err } - var v interface{} + var v any if err := json.Unmarshal(buf, &v); err != nil { return 0, err } diff --git a/pkg/mutators/single/ndjson.go b/pkg/mutators/single/ndjson.go index 03f269891..ba9f07695 100644 --- a/pkg/mutators/single/ndjson.go +++ b/pkg/mutators/single/ndjson.go @@ -47,7 +47,7 @@ func NDJSONIndent(w io.WriteCloser, r io.ReadCloser, arg any) (int64, error) { } // Parse and pretty-print the JSON line - var jsonObj interface{} + var jsonObj any if err := json.Unmarshal([]byte(line), &jsonObj); err != nil { // If it's not valid JSON, write the line as-is if _, err := fmt.Fprintln(w, line); err != nil { diff --git a/pkg/openers/crng.go b/pkg/openers/crng.go index 07a0dca31..77da34bae 100644 --- a/pkg/openers/crng.go +++ b/pkg/openers/crng.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/crng_internal_test.go b/pkg/openers/crng_internal_test.go index 23d9d12d9..043e5fa5e 100644 --- a/pkg/openers/crng_internal_test.go +++ b/pkg/openers/crng_internal_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/crng_test.go b/pkg/openers/crng_test.go index 308571bca..00108886d 100644 --- a/pkg/openers/crng_test.go +++ b/pkg/openers/crng_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers_test diff --git a/pkg/openers/echo.go b/pkg/openers/echo.go index 13b37e871..d8f497d14 100644 --- a/pkg/openers/echo.go +++ b/pkg/openers/echo.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/echo_test.go b/pkg/openers/echo_test.go index 5e12bf6fc..c3df0a1a2 100644 --- a/pkg/openers/echo_test.go +++ b/pkg/openers/echo_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers_test diff --git a/pkg/openers/gemini.go b/pkg/openers/gemini.go index 2d26851c0..108f9f55b 100644 --- a/pkg/openers/gemini.go +++ b/pkg/openers/gemini.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/http.go b/pkg/openers/http.go index ab5371e31..0fe17a61b 100644 --- a/pkg/openers/http.go +++ b/pkg/openers/http.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/http_test.go b/pkg/openers/http_test.go index da0725e8e..8160e01e4 100644 --- a/pkg/openers/http_test.go +++ b/pkg/openers/http_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers_test @@ -29,7 +28,7 @@ func init() { go func() { // find an available port - for i := 0; i < 100; i++ { + for range 100 { m.Lock() portTest = 10000 + rand.Intn(55000) //#nosec G404 m.Unlock() @@ -38,7 +37,7 @@ func init() { }() go func() { // find an available port - for i := 0; i < 100; i++ { + for range 100 { m.Lock() insecurePortTest = 10000 + rand.Intn(55000) //#nosec G404 m.Unlock() diff --git a/pkg/openers/mc.go b/pkg/openers/mc.go index 30143577c..f7d7a0e39 100644 --- a/pkg/openers/mc.go +++ b/pkg/openers/mc.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/nc.go b/pkg/openers/nc.go index 16a51dbbe..b51cfcb39 100644 --- a/pkg/openers/nc.go +++ b/pkg/openers/nc.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/prng.go b/pkg/openers/prng.go index c80d1bfe0..ef058e1ef 100644 --- a/pkg/openers/prng.go +++ b/pkg/openers/prng.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/prng_internal_test.go b/pkg/openers/prng_internal_test.go index 0b9c91685..3de76d5c5 100644 --- a/pkg/openers/prng_internal_test.go +++ b/pkg/openers/prng_internal_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers diff --git a/pkg/openers/prng_test.go b/pkg/openers/prng_test.go index b0e8191c8..55f0f3adb 100644 --- a/pkg/openers/prng_test.go +++ b/pkg/openers/prng_test.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers_test diff --git a/pkg/openers/sse.go b/pkg/openers/sse.go index 5ed31587c..9e19b2d01 100644 --- a/pkg/openers/sse.go +++ b/pkg/openers/sse.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers @@ -110,8 +109,8 @@ func (s *sseReadCloser) Read(p []byte) (int, error) { } // Only output data lines - if strings.HasPrefix(line, "data:") { - eventData := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if after, ok := strings.CutPrefix(line, "data:"); ok { + eventData := strings.TrimSpace(after) s.buf.WriteString(eventData) s.buf.WriteString("\n") return s.buf.Read(p) diff --git a/pkg/openers/wormhole.go b/pkg/openers/wormhole.go index 7697a6376..1ee91f6be 100644 --- a/pkg/openers/wormhole.go +++ b/pkg/openers/wormhole.go @@ -1,5 +1,4 @@ //go:build !fileonly -// +build !fileonly package openers @@ -39,10 +38,10 @@ func (f wormholeOpener) Description() string { } func (f wormholeOpener) Open(code string, _ bool) (io.ReadCloser, error) { - if strings.HasPrefix(code, "wormhole://") { - code = strings.TrimPrefix(code, "wormhole://") - } else if strings.HasPrefix(code, "wh://") { - code = strings.TrimPrefix(code, "wh://") + if after, ok := strings.CutPrefix(code, "wormhole://"); ok { + code = after + } else if after, ok := strings.CutPrefix(code, "wh://"); ok { + code = after } var c wormhole.Client diff --git a/pkg/pcg/pcg32_test.go b/pkg/pcg/pcg32_test.go index 9ed4cd379..529903612 100644 --- a/pkg/pcg/pcg32_test.go +++ b/pkg/pcg/pcg32_test.go @@ -44,7 +44,7 @@ func TestPCG32Output(t *testing.T) { generated := make(map[uint32]struct{}) var firstValue uint32 allSame := true - for i := 0; i < count; i++ { + for i := range count { v := rng.Next() generated[v] = struct{}{} if i == 0 { diff --git a/pkg/secretprovider/keyring_mock.go b/pkg/secretprovider/keyring_mock.go index aaa9593ed..aeb5ea39f 100644 --- a/pkg/secretprovider/keyring_mock.go +++ b/pkg/secretprovider/keyring_mock.go @@ -1,5 +1,4 @@ //go:build !keystore -// +build !keystore package secretprovider diff --git a/pkg/stringutils/stringInSlice.go b/pkg/stringutils/stringInSlice.go index dbb586c90..35c911089 100644 --- a/pkg/stringutils/stringInSlice.go +++ b/pkg/stringutils/stringInSlice.go @@ -1,10 +1,7 @@ package stringutils +import "slices" + func IsStringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false + return slices.Contains(list, a) } diff --git a/pkg/utils/test_helpers.go b/pkg/utils/test_helpers.go index f95ac0573..c70175929 100644 --- a/pkg/utils/test_helpers.go +++ b/pkg/utils/test_helpers.go @@ -5,7 +5,7 @@ import "testing" func CheckBytesRandomness(t *testing.T, data []byte) { t.Helper() acc := uint8(0) - for i := 0; i < len(data); i++ { + for i := range data { acc |= data[i] } if acc != 0xFF {