Skip to content

Commit

Permalink
feat: 新增代码规范检查
Browse files Browse the repository at this point in the history
  • Loading branch information
SugarMGP committed Dec 17, 2024
1 parent 5e97253 commit 3aa8156
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

# 定义触发条件
on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2
args: '--config .golangci.yml'
114 changes: 114 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# golangci-lint 配置文件

run:
concurrency: 4 # 并行执行的 linter 数量,默认为可用 CPU 数量
deadline: 5m # 分析的最大超时时间,默认为 5 分钟
timeout: 10m # 超时时间
issues-exit-code: 1 # 如果发现至少一个问题,退出码为 1
tests: true # 包含测试文件进行分析
modules-download-mode: readonly # 防止分析过程中修改 go.mod 文件

output:
formats: # 设置多个输出格式
- format: colored-line-number # 使用颜色化的行号格式
sort-results: true # 按文件名和行号排序结果
print-issued-lines: true # 输出包含问题的代码行
print-linter-name: true # 输出 linter 名称

linters-settings:
errcheck:
check-type-assertions: true # 检查类型断言的错误处理
check-blank: false # 检查将错误分配给空标识符的情况
exclude-functions: # 不检查这些函数
- fmt:.*
- io/ioutil:^Read.*
revive:
# 是否启用所有可用的规则
enable-all-rules: true
# 默认失败置信度
confidence: 0.1
rules:
- name: var-naming # 变量命名规则
severity: warning
disabled: true
- name: line-length-limit # 行长度限制
severity: warning
disabled: false
exclude: [ "" ]
arguments: [ 120 ]
- name: add-constant # 使用命名常量而非魔法数字
severity: warning
disabled: true
- name: package-comments # 包注释
severity: warning
disabled: true
- name: import-alias-naming # 导入别名命名规则
severity: warning
disabled: true
- name: get-return # 使用get必须有返回值
severity: warning
disabled: true
- name: max-public-structs # 最大公共结构体数目
severity: warning
disabled: true
- name: argument-limit # 函数参数限制
severity: warning
disabled: true
- name: function-length # 函数长度限制
severity: warning
disabled: true
- name: cyclomatic # 圈复杂度
severity: warning
disabled: true
- name: cognitive-complexity # 认知复杂度
severity: warning
disabled: false
arguments: [ 10 ]
gofmt:
simplify: true # 使用 gofmt 的 `-s` 选项简化代码
gocyclo:
min-complexity: 10 # 触发报告的最小代码复杂度
dupl:
threshold: 100 # 触发报告的最小令牌数
goconst:
min-len: 3 # 字符串常量的最小长度
min-occurrences: 3 # 触发报告的最小出现次数
gosec:
severity: medium # 安全问题的最小严重性
unused:
check-exported: true # 报告未使用的导出标识符
unparam:
check-exported: false # 检查导出的函数
nakedret:
max-func-lines: 0 # 允许带裸返回的函数的最大行数
staticcheck: # 使用 staticcheck 替代 govet
checks:
- SA1000 # 使用了某些无效操作
- SA1001 # 无效的时间格式或解析
- SA1012 # 非法的 waitGroup 使用
prealloc:
simple: true # 在简单循环中报告预分配建议
range-loops: true # 在 range 循环中报告预分配建议
for-loops: true # 在 for 循环中报告预分配建议

linters:
disable-all: true # 禁用所有 linter
enable:
- errcheck # 检查未处理的错误
- staticcheck # 静态代码分析工具,检测潜在的错误和反模式
- gofmt # 检查代码格式是否符合 gofmt 规范
- revive # 替代 golint 的代码风格检查工具
- gocyclo # 检测代码的圈复杂度
- dupl # 检查重复代码
- goconst # 检查重复的常量
- gosec # 检查安全问题
- unused # 检查未使用的代码
- unparam # 检查未使用的函数参数
- prealloc # 检查预分配建议
- gci # 检查并排序导入语句

issues:
exclude-use-default: false # 禁用默认排除模式
max-per-linter: 0 # 每个 linter 的最大问题数量,设置为 0 禁用限制
max-same-issues: 0 # 同一问题的最大数量,设置为 0 禁用限制
new: false # 仅显示新问题,默认为 false
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@
```
go get github.com/zjutjh/WeJH-SDK
```

### 代码规范检查

需要安装 [gci](https://github.com/daixiang0/gci)[golangci-lint](https://golangci-lint.run/)

```
gofmt -w .
gci write . -s standard -s default
golangci-lint run --config .golangci.yml
```
1 change: 1 addition & 0 deletions fetch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:all
package sdk

import (
Expand Down
2 changes: 1 addition & 1 deletion zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func getFileCore(encoder zapcore.Encoder, cfg *InfoConfig) zapcore.Core {
MaxAge: cfg.LogMaxAge,
Compress: cfg.LogCompress,
}
allLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
allLevel := zap.LevelEnablerFunc(func(_ zapcore.Level) bool {
return true // 记录所有级别
})
return zapcore.NewCore(encoder, zapcore.AddSync(allWriter), allLevel)
Expand Down

0 comments on commit 3aa8156

Please sign in to comment.