Skip to content

Commit 7e0baee

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
linting
1 parent 8fc88e1 commit 7e0baee

File tree

7 files changed

+81
-35
lines changed

7 files changed

+81
-35
lines changed

.golangci.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ linters:
152152

153153
nakedret:
154154
# Default: 30
155-
max-func-lines: 4
155+
max-func-lines: 7
156156

157157
nestif:
158-
min-complexity: 12
158+
min-complexity: 15
159159

160160
nolintlint:
161161
# Exclude following linters from requiring an explanation.
@@ -171,17 +171,11 @@ linters:
171171
rules:
172172
- name: add-constant
173173
severity: warning
174-
disabled: false
175-
exclude: [""]
176-
arguments:
177-
- max-lit-count: "5"
178-
allow-strs: '"","\n","error","type","message"'
179-
allow-ints: "0,1,2,3,24,30,60,100,365,0o600,0o700,0o750,0o755"
180-
allow-floats: "0.0,0.,1.0,1.,2.0,2."
174+
disabled: true
181175
- name: cognitive-complexity
182-
arguments: [55]
176+
disabled: true # prefer maintidx
183177
- name: cyclomatic
184-
arguments: [60]
178+
disabled: true # prefer maintidx
185179
- name: function-length
186180
arguments: [150, 225]
187181
- name: line-length-limit
@@ -213,8 +207,14 @@ linters:
213207
os-temp-dir: true
214208

215209
varnamelen:
216-
max-distance: 50
217-
min-name-length: 1
210+
max-distance: 75
211+
min-name-length: 2
212+
check-receivers: false
213+
ignore-names:
214+
- r
215+
- w
216+
- f
217+
- err
218218

219219
exclusions:
220220
# Default: []

Makefile

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ shellcheck-fix: $(SHELLCHECK_BIN)
6969
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
7070

7171
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
72-
GOLANGCI_LINT_VERSION ?= v2.3.1
72+
GOLANGCI_LINT_VERSION ?= v2.5.0
7373
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
7474
$(GOLANGCI_LINT_BIN):
7575
mkdir -p $(LINT_ROOT)/out/linters
@@ -85,10 +85,59 @@ FIXERS += golangci-lint-fix
8585
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
8686
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
8787

88+
YAMLLINT_VERSION ?= 1.37.1
89+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
90+
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
91+
$(YAMLLINT_BIN):
92+
mkdir -p $(LINT_ROOT)/out/linters
93+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
94+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
95+
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
96+
97+
LINTERS += yamllint-lint
98+
yamllint-lint: $(YAMLLINT_BIN)
99+
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
100+
101+
BIOME_VERSION ?= 2.2.6
102+
BIOME_BIN := $(LINT_ROOT)/out/linters/biome-$(BIOME_VERSION)-$(LINT_ARCH)
103+
BIOME_CONFIG := $(LINT_ROOT)/biome.json
104+
105+
# Map architecture names for Biome downloads
106+
BIOME_ARCH := $(LINT_ARCH)
107+
ifeq ($(LINT_ARCH),x86_64)
108+
BIOME_ARCH := x64
109+
endif
110+
111+
$(BIOME_BIN):
112+
mkdir -p $(LINT_ROOT)/out/linters
113+
rm -rf $(LINT_ROOT)/out/linters/biome-*
114+
curl -sSfL -o $@ https://github.com/biomejs/biome/releases/download/%40biomejs%2Fbiome%40$(BIOME_VERSION)/biome-$(LINT_OS_LOWER)-$(BIOME_ARCH) \
115+
|| echo "Unable to fetch biome for $(LINT_OS_LOWER)/$(BIOME_ARCH), falling back to local install"
116+
test -f $@ || printf "#!/usr/bin/env biome\n" > $@
117+
chmod u+x $@
118+
119+
LINTERS += biome-lint
120+
biome-lint: $(BIOME_BIN)
121+
$(BIOME_BIN) check --config-path=$(BIOME_CONFIG) .
122+
123+
FIXERS += biome-fix
124+
biome-fix: $(BIOME_BIN)
125+
$(BIOME_BIN) check --write --config-path=$(BIOME_CONFIG) .
126+
88127
.PHONY: _lint $(LINTERS)
89-
_lint: $(LINTERS)
128+
_lint:
129+
@exit_code=0; \
130+
for target in $(LINTERS); do \
131+
$(MAKE) $$target || exit_code=1; \
132+
done; \
133+
exit $$exit_code
90134

91135
.PHONY: fix $(FIXERS)
92-
fix: $(FIXERS)
136+
fix:
137+
@exit_code=0; \
138+
for target in $(FIXERS); do \
139+
$(MAKE) $$target || exit_code=1; \
140+
done; \
141+
exit $$exit_code
93142

94143
# END: lint-install .

cmd/server/main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func main() {
8787

8888
// CORS support removed - WebSocket clients should handle auth via Authorization header
8989

90-
h := srv.NewHub()
91-
go h.Run(ctx)
90+
hub := srv.NewHub()
91+
go hub.Run(ctx)
9292

9393
// Create security components
9494
rateLimiter := security.NewRateLimiter(*rateLimit, time.Minute)
@@ -117,7 +117,7 @@ func main() {
117117
log.Println("Registered health check handler at /")
118118

119119
// Webhook handler - exact match
120-
webhookHandler := webhook.NewHandler(h, webhookSecretValue, allowedEventTypes)
120+
webhookHandler := webhook.NewHandler(hub, webhookSecretValue, allowedEventTypes)
121121
mux.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
122122
ip := security.ClientIP(r)
123123
startTime := time.Now()
@@ -147,7 +147,7 @@ func main() {
147147
log.Println("Registered webhook handler at /webhook")
148148

149149
// WebSocket handler - exact match
150-
wsHandler := srv.NewWebSocketHandler(h, connLimiter, allowedEventTypes)
150+
wsHandler := srv.NewWebSocketHandler(hub, connLimiter, allowedEventTypes)
151151
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
152152
startTime := time.Now()
153153
ip := security.ClientIP(r)
@@ -195,7 +195,6 @@ func main() {
195195

196196
// Pre-validate authentication before WebSocket upgrade
197197
authHeader := r.Header.Get("Authorization")
198-
//nolint:nestif // Auth validation logic is necessarily complex for detailed error reporting
199198
if !wsHandler.PreValidateAuth(r) {
200199
// Determine specific failure reason for better debugging
201200
reason := "missing"
@@ -280,7 +279,7 @@ func main() {
280279
cancel()
281280

282281
// Stop accepting new connections
283-
h.Stop()
282+
hub.Stop()
284283

285284
// Stop the rate limiter cleanup routine
286285
rateLimiter.Stop()
@@ -298,7 +297,7 @@ func main() {
298297
}
299298

300299
// Wait for hub to finish
301-
h.Wait()
300+
hub.Wait()
302301

303302
close(done)
304303
}()

pkg/client/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Package client provides a robust WebSocket client for connecting to webhook sprinkler servers.
2-
// It handles automatic reconnection, ping/pong keep-alive, and comprehensive logging.
31
package client
42

53
import (

pkg/logger/logger.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
// Fields represents structured log fields.
1313
type Fields map[string]any
1414

15-
// WithFields adds structured context to log messages.
16-
func WithFields(fields Fields, format string, args ...any) {
15+
// WithFieldsf adds structured context to log messages with printf-style formatting.
16+
func WithFieldsf(fields Fields, format string, args ...any) {
1717
// Sort keys for consistent output
1818
keys := make([]string, 0, len(fields))
1919
for k := range fields {
@@ -36,7 +36,7 @@ func WithFields(fields Fields, format string, args ...any) {
3636

3737
// Info logs an info message with optional fields.
3838
func Info(msg string, fields Fields) {
39-
WithFields(fields, "%s", msg)
39+
WithFieldsf(fields, "%s", msg)
4040
}
4141

4242
// Error logs an error message with optional fields.
@@ -45,10 +45,10 @@ func Error(msg string, err error, fields Fields) {
4545
fields = Fields{}
4646
}
4747
fields["error"] = err.Error()
48-
WithFields(fields, "ERROR: %s", msg)
48+
WithFieldsf(fields, "ERROR: %s", msg)
4949
}
5050

5151
// Warn logs a warning message with optional fields.
5252
func Warn(msg string, fields Fields) {
53-
WithFields(fields, "WARNING: %s", msg)
53+
WithFieldsf(fields, "WARNING: %s", msg)
5454
}

pkg/logger/logger_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestFieldsWithSpecialCharacters(t *testing.T) {
126126

127127
fields := Fields{
128128
"path": "/etc/passwd",
129-
"query": "SELECT * FROM users",
129+
"query": "SELECT id, name FROM users",
130130
"url": "https://example.com?foo=bar&baz=qux",
131131
}
132132

@@ -136,7 +136,7 @@ func TestFieldsWithSpecialCharacters(t *testing.T) {
136136
if !strings.Contains(output, "path=/etc/passwd") {
137137
t.Error("path field not preserved correctly")
138138
}
139-
if !strings.Contains(output, "query=SELECT * FROM users") {
139+
if !strings.Contains(output, "query=SELECT id, name FROM users") {
140140
t.Error("query field not preserved correctly")
141141
}
142142
if !strings.Contains(output, "url=https://example.com?foo=bar&baz=qux") {
@@ -166,14 +166,14 @@ func TestFieldsWithNilValues(t *testing.T) {
166166
}
167167
}
168168

169-
// TestWithFieldsFormatting tests the WithFields function with format strings
169+
// TestWithFieldsFormatting tests the WithFieldsf function with format strings
170170
func TestWithFieldsFormatting(t *testing.T) {
171171
var buf bytes.Buffer
172172
log.SetOutput(&buf)
173173
defer log.SetOutput(nil)
174174

175175
fields := Fields{"user": "alice"}
176-
WithFields(fields, "User %s logged in at %d", "bob", 12345)
176+
WithFieldsf(fields, "User %s logged in at %d", "bob", 12345)
177177

178178
output := buf.String()
179179
if !strings.Contains(output, "User bob logged in at 12345") {

pkg/srv/subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *Subscription) Validate() error {
8989

9090
// Basic validation - should be a GitHub PR URL
9191
// Format: https://github.com/owner/repo/pull/number
92-
if !strings.HasPrefix(prURL, "https://github.com/") && !strings.HasPrefix(prURL, "http://github.com/") {
92+
if !strings.HasPrefix(prURL, "https://github.com/") {
9393
return errors.New("invalid PR URL format")
9494
}
9595

0 commit comments

Comments
 (0)