Skip to content

Commit 9dfdb44

Browse files
committed
Enhance linting and development tool setup for Go-GenAI-Stack
- Updated .golangci.yml to streamline linting configuration, enabling essential linters and removing deprecated settings. - Introduced Makefile for backend to simplify common tasks such as linting, testing, and formatting. - Added lint and lint-fix scripts to automate code quality checks and fixes. - Updated setup-tools.sh to ensure all necessary development tools are installed and configured properly. These changes improve the development workflow, ensuring consistent code quality and easier maintenance.
1 parent 66f6c5c commit 9dfdb44

7 files changed

Lines changed: 360 additions & 112 deletions

File tree

.golangci.yml

Lines changed: 36 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,54 @@
1-
# golangci-lint 配置文件
2-
# 用于代码质量检查和 CI/CD
1+
# golangci-lint configuration for Go-GenAI-Stack
2+
# Docs: https://golangci-lint.run/usage/configuration/
3+
4+
version: v1.61
35

46
run:
57
timeout: 5m
6-
issues-exit-code: 1
78
tests: true
9+
modules-download-mode: readonly
810
skip-dirs:
911
- vendor
10-
- third_party
11-
- testdata
12-
skip-files:
12+
- .git
13+
- .github
14+
15+
issues:
16+
exclude-files:
1317
- ".*\\.pb\\.go$"
1418
- ".*_gen\\.go$"
15-
16-
output:
17-
format: colored-line-number
18-
print-issued-lines: true
19-
print-linter-name: true
20-
sort-results: true
21-
22-
linters:
23-
enable:
24-
- errcheck # 检查错误处理
25-
- gosimple # 简化代码
26-
- govet # Go 标准检查
27-
- ineffassign # 检查无效赋值
28-
- staticcheck # 静态分析
29-
- typecheck # 类型检查
30-
- unused # 检查未使用的常量、变量等
31-
- gofmt # 代码格式化
32-
- goimports # import 排序
33-
- misspell # 拼写检查
34-
- goconst # 检查可以提取为常量的重复字符串
35-
- dupl # 检查重复代码
36-
- gosec # 安全检查
37-
- bodyclose # 检查 HTTP body 是否关闭
38-
- noctx # 检查 HTTP 请求是否使用 context
39-
- errname # 检查错误命名
40-
- errorlint # 错误处理最佳实践
41-
- exportloopref # 检查循环变量导出问题
42-
disable:
43-
- exhaustive # 太严格
44-
- exhaustruct # 太严格
45-
- varnamelen # 太严格
46-
- gochecknoglobals # 在某些场景需要全局变量
19+
max-issues-per-linter: 50
20+
max-same-issues: 3
4721

4822
linters-settings:
49-
errcheck:
50-
check-type-assertions: true
51-
check-blank: true
52-
53-
govet:
54-
check-shadowing: true
55-
enable-all: true
56-
57-
gofmt:
58-
simplify: true
59-
6023
goimports:
6124
local-prefixes: github.com/erweixin/go-genai-stack
6225

63-
misspell:
64-
locale: US
65-
66-
goconst:
67-
min-len: 3
68-
min-occurrences: 3
69-
70-
dupl:
71-
threshold: 100
72-
73-
gosec:
74-
severity: medium
75-
confidence: medium
76-
excludes:
77-
- G104 # 审计未检查的错误
78-
- G204 # 审计命令执行
26+
gofmt:
27+
simplify: true
7928

80-
gocritic:
81-
enabled-tags:
82-
- diagnostic
83-
- style
84-
- performance
85-
disabled-checks:
86-
- paramTypeCombine
87-
- unnamedResult
29+
govet:
30+
enable-all: true
31+
disable:
32+
- shadow
33+
- fieldalignment # 已手动优化过
8834

89-
issues:
90-
exclude-use-default: false
91-
max-issues-per-linter: 0
92-
max-same-issues: 0
93-
94-
exclude-rules:
95-
# 排除测试文件的某些检查
96-
- path: _test\.go
97-
linters:
98-
- dupl
99-
- gosec
100-
- goconst
101-
102-
# 排除生成的文件
103-
- path: \.pb\.go$
104-
linters:
105-
- all
35+
linters:
36+
disable-all: true
37+
enable:
38+
# 代码格式 (自动修复)
39+
- gofmt # 代码格式化
40+
- goimports # 导入排序
10641

107-
# 排除特定错误消息
108-
- text: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*print(f|ln)?|os\\.(Un)?Setenv). is not checked"
109-
linters:
110-
- errcheck
42+
# 代码质量检查
43+
- errcheck # 检查未处理的错误
44+
- gosimple # 简化代码建议
45+
- govet # Go 官方检查工具
46+
- ineffassign # 检测无效赋值
47+
- staticcheck # 静态分析
48+
- unused # 未使用的代码
11149

112-
# 允许在 main 和 init 函数中使用 panic
113-
- text: "Use of panic detected"
114-
linters:
115-
- gosec
116-
path: cmd/
50+
# 可选检查
51+
- misspell # 拼写检查
11752

118-
# 允许在测试中使用 MD5(不用于加密)
119-
- text: "Use of weak cryptographic primitive"
120-
linters:
121-
- gosec
122-
path: _test\.go
123-
124-
severity:
125-
default-severity: warning
126-
rules:
127-
- linters:
128-
- dupl
129-
severity: info
130-
53+
# 注意: typecheck 被禁用,因为在某些测试文件中会产生误报
54+
# 实际的类型检查由 `go build` 和 `go test` 保证

backend/Makefile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.PHONY: help lint lint-fix test test-coverage fmt vet staticcheck tools
2+
3+
# 默认目标:显示帮助
4+
help:
5+
@echo "Go-GenAI-Stack Backend Makefile"
6+
@echo ""
7+
@echo "Usage:"
8+
@echo " make lint - Run all lint checks"
9+
@echo " make lint-fix - Auto-fix lint issues"
10+
@echo " make test - Run all tests"
11+
@echo " make test-coverage - Run tests with coverage"
12+
@echo " make fmt - Format code"
13+
@echo " make vet - Run go vet"
14+
@echo " make staticcheck - Run staticcheck"
15+
@echo " make tools - Install development tools"
16+
@echo " make check - Run all checks (lint + test)"
17+
18+
# 安装开发工具
19+
tools:
20+
@echo "🔧 Installing development tools..."
21+
@go install golang.org/x/tools/cmd/goimports@latest
22+
@go install honnef.co/go/tools/cmd/staticcheck@latest
23+
@go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
24+
@echo "✅ Tools installed"
25+
26+
# 代码格式化
27+
fmt:
28+
@echo "📐 Formatting code..."
29+
@gofmt -w .
30+
@if command -v goimports >/dev/null 2>&1; then \
31+
goimports -w .; \
32+
fi
33+
@echo "✅ Code formatted"
34+
35+
# Go vet 检查
36+
vet:
37+
@echo "🔍 Running go vet..."
38+
@go vet ./...
39+
@echo "✅ go vet passed"
40+
41+
# Staticcheck 检查
42+
staticcheck:
43+
@echo "🔍 Running staticcheck..."
44+
@if command -v staticcheck >/dev/null 2>&1; then \
45+
staticcheck ./...; \
46+
else \
47+
echo "⚠️ staticcheck not found, run 'make tools' first"; \
48+
exit 1; \
49+
fi
50+
@echo "✅ staticcheck passed"
51+
52+
# Lint 检查
53+
lint: fmt vet staticcheck
54+
@echo "🔍 Running golangci-lint..."
55+
@if command -v golangci-lint >/dev/null 2>&1; then \
56+
golangci-lint run --timeout=5m ./...; \
57+
else \
58+
echo "⚠️ golangci-lint not found"; \
59+
echo "Install with: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b \$$(go env GOPATH)/bin"; \
60+
exit 1; \
61+
fi
62+
@echo "✅ All lint checks passed"
63+
64+
# 自动修复 Lint 问题
65+
lint-fix:
66+
@echo "🔧 Auto-fixing lint issues..."
67+
@gofmt -w .
68+
@if command -v goimports >/dev/null 2>&1; then \
69+
goimports -w .; \
70+
fi
71+
@if command -v golangci-lint >/dev/null 2>&1; then \
72+
golangci-lint run --fix --timeout=5m ./...; \
73+
fi
74+
@go mod tidy
75+
@echo "✅ All fixes applied"
76+
77+
# 运行测试
78+
test:
79+
@echo "🧪 Running tests..."
80+
@go test -v -race ./...
81+
@echo "✅ Tests passed"
82+
83+
# 运行测试并生成覆盖率
84+
test-coverage:
85+
@echo "🧪 Running tests with coverage..."
86+
@go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
87+
@go tool cover -func=coverage.out | grep total
88+
@echo ""
89+
@echo "📊 Coverage report generated: coverage.out"
90+
@echo " View with: go tool cover -html=coverage.out"
91+
92+
# 运行所有检查
93+
check: lint test
94+
@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
95+
@echo "✅ All checks passed!"
96+
@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
97+

scripts/dev_all.sh

100644100755
File mode changed.

scripts/lint-fix.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Auto-fix lint issues for Go-GenAI-Stack
4+
# 自动修复代码问题
5+
6+
set -e
7+
8+
cd "$(dirname "$0")/.."
9+
10+
echo "🔧 Auto-fixing lint issues..."
11+
echo ""
12+
13+
cd backend
14+
15+
# 1. gofmt
16+
echo "1️⃣ Formatting code (gofmt)..."
17+
gofmt -w .
18+
echo "✅ Code formatted"
19+
echo ""
20+
21+
# 2. goimports
22+
echo "2️⃣ Fixing imports (goimports)..."
23+
if command -v goimports &> /dev/null; then
24+
goimports -w .
25+
echo "✅ Imports fixed"
26+
else
27+
echo "⚠️ goimports not found"
28+
echo "Install with: go install golang.org/x/tools/cmd/goimports@latest"
29+
fi
30+
echo ""
31+
32+
# 3. golangci-lint auto-fix
33+
echo "3️⃣ Running golangci-lint --fix..."
34+
if command -v golangci-lint &> /dev/null; then
35+
golangci-lint run --fix --timeout=5m ./... || true
36+
echo "✅ golangci-lint fixes applied"
37+
else
38+
echo "⚠️ golangci-lint not found"
39+
echo "Install with: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b \$(go env GOPATH)/bin"
40+
fi
41+
echo ""
42+
43+
# 4. go mod tidy
44+
echo "4️⃣ Tidying go.mod..."
45+
go mod tidy
46+
echo "✅ go.mod tidied"
47+
echo ""
48+
49+
cd ..
50+
51+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
52+
echo "✅ All fixes applied!"
53+
echo ""
54+
echo "📝 Next steps:"
55+
echo " 1. Review the changes: git diff"
56+
echo " 2. Run tests: cd backend && go test ./..."
57+
echo " 3. Commit: git add . && git commit"
58+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
59+

0 commit comments

Comments
 (0)