ci(lint): golangci-lint v2 入门禁并归零
- .golangci.yml:standard 集 - errcheck(存量 38 处裸忽略待专项),staticcheck 关纯风格/quickfix - 清理 3 个无引用死函数;SanitizedJSON 不可达分支收敛为接口分支 + nil 指针防护(原死分支中的 nil 检查从未执行) - CI backend job 增加 golangci-lint-action@v6
This commit is contained in:
1 parent
a69142fbfe
commit
c33ea9d0a8
5 files changed
+20
-58
No files matched your search
@@ -27,6 +27,10 @@ jobs:
|
||||
- name: go vet
|
||||
working-directory: backend
|
||||
run: go vet ./...
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v6
|
||||
with:
|
||||
working-directory: backend
|
||||
- name: go test
|
||||
working-directory: backend
|
||||
run: go test ./...
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# golangci-lint v2 配置 — 与 CI 门禁(.gitea/workflows/ci.yml)配套
|
||||
# 取舍:errcheck 存量 38 处裸忽略待专项清理后开启;staticcheck 关闭纯风格/quickfix 类
|
||||
version: "2"
|
||||
|
||||
formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
|
||||
linters:
|
||||
default: standard
|
||||
disable:
|
||||
- errcheck
|
||||
settings:
|
||||
staticcheck:
|
||||
checks: ["all", "-ST1000", "-ST1003", "-ST1005", "-ST1020", "-ST1021", "-ST1022", "-QF*", "-S1*"]
|
||||
@@ -38,17 +38,5 @@ func Metrics() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// statusClass 给日志/看板用的状态分类(保留工具函数)
|
||||
func statusClass(code int) string {
|
||||
switch {
|
||||
case code < 300:
|
||||
return "2xx"
|
||||
case code < 400:
|
||||
return "3xx"
|
||||
case code < 500:
|
||||
return "4xx"
|
||||
default:
|
||||
return "5xx"
|
||||
}
|
||||
}
|
||||
|
||||
var _ = strconv.Itoa
|
||||
@@ -10,35 +10,8 @@ type Sanitizer interface {
|
||||
// 使用接口替代类型断言,更优雅且可扩展。
|
||||
func SanitizedJSON(v any) any {
|
||||
switch val := v.(type) {
|
||||
// Single objects: 尝试Sanitizer接口
|
||||
// Single objects: 实现 Sanitizer 接口的类型统一走这里(nil 指针安全返回 nil)
|
||||
case Sanitizer:
|
||||
return val.Sanitized()
|
||||
case *User:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
return val.Sanitized()
|
||||
case *Cluster:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
return val.Sanitized()
|
||||
case *Host:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
return val.Sanitized()
|
||||
case *StorageConfig:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
return val.Sanitized()
|
||||
case *Collector:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
return val.Sanitized()
|
||||
case *LokiDeployment:
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -264,23 +263,6 @@ func DecryptCred(encrypted string) (string, error) {
|
||||
// ==================== 通用工具函数 ====================
|
||||
|
||||
// writeTempFile 安全地创建临时文件,返回文件路径和清理函数
|
||||
func writeTempFile(pattern, content string, perm os.FileMode) (string, func(), error) {
|
||||
f, err := os.CreateTemp("", pattern)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("create temp file failed: %w", err)
|
||||
}
|
||||
if _, err := f.WriteString(content); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return "", nil, fmt.Errorf("write temp file failed: %w", err)
|
||||
}
|
||||
f.Close()
|
||||
if perm > 0 {
|
||||
os.Chmod(f.Name(), perm)
|
||||
}
|
||||
cleanup := func() { os.Remove(f.Name()) }
|
||||
return f.Name(), cleanup, nil
|
||||
}
|
||||
|
||||
// indentYAML 对 YAML 内容进行缩进(用于嵌入 ConfigMap)
|
||||
func indentYAML(s string, spaces int) string {
|
||||
|
||||
Reference in new issue
Block a user