抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,102 @@
|
||||
// Command verify 检查所有平台的生产依赖方向及 Go 格式,不依赖消费项目。
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// main 执行指定格式或架构检查,发现违规以非零状态退出。
|
||||
func main() {
|
||||
formatCheck := flag.Bool("format", false, "检查 Go 格式")
|
||||
architecture := flag.Bool("architecture", false, "检查独立库依赖方向")
|
||||
docs := flag.Bool("docs", false, "检查接入指南能力清单和相对链接")
|
||||
flag.Parse()
|
||||
if !*formatCheck && !*architecture && !*docs {
|
||||
fmt.Fprintln(os.Stderr, "specify -format, -architecture or -docs")
|
||||
os.Exit(2)
|
||||
}
|
||||
if *docs {
|
||||
if err := checkDocs("."); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if err := run(*formatCheck, *architecture); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run 扫描全仓 Go 文件,不按当前 GOOS 或构建标签过滤。
|
||||
// 入参:formatCheck 决定检查格式,architecture 决定检查生产 import。
|
||||
// 返回:第一个解析、格式或架构违规,附文件和必要位置。
|
||||
func run(formatCheck bool, architecture bool) error {
|
||||
const module = "git.zhangdeman.cn/github/ginframework"
|
||||
return filepath.WalkDir(".", func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if entry.IsDir() {
|
||||
if entry.Name() == ".git" || entry.Name() == "vendor" {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(path, ".go") {
|
||||
return nil
|
||||
}
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if formatCheck {
|
||||
formatted, err := format.Source(content)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if !bytes.Equal(content, formatted) {
|
||||
return fmt.Errorf("%s: run gofmt", path)
|
||||
}
|
||||
}
|
||||
if !architecture || strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
fileSet := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileSet, path, content, parser.ImportsOnly)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
directory := filepath.ToSlash(filepath.Dir(path))
|
||||
for _, spec := range file.Imports {
|
||||
imported, err := strconv.Unquote(spec.Path.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
invalid := strings.Contains(imported, "model_dispatch") || (strings.HasPrefix(imported, "git.zhangdeman.cn/") && !strings.HasPrefix(imported, module+"/"))
|
||||
switch {
|
||||
case directory == "diagnostic" || directory == "fault":
|
||||
invalid = invalid || strings.Contains(strings.Split(imported, "/")[0], ".")
|
||||
case strings.HasPrefix(directory, "logger"):
|
||||
invalid = invalid || (strings.HasPrefix(imported, module+"/") && !strings.HasPrefix(imported, module+"/logger/") && imported != module+"/diagnostic")
|
||||
case strings.HasPrefix(directory, "database") || strings.HasPrefix(directory, "redis") || directory == "httpclient":
|
||||
invalid = invalid || strings.HasPrefix(imported, "github.com/gin-gonic/") || imported == module+"/http" || strings.HasPrefix(imported, module+"/http/")
|
||||
case directory == "config":
|
||||
invalid = invalid || strings.HasPrefix(imported, module+"/http") || strings.HasPrefix(imported, module+"/database") || strings.HasPrefix(imported, module+"/redis")
|
||||
}
|
||||
if invalid {
|
||||
return fmt.Errorf("%s: forbidden import chain %s -> %s", fileSet.Position(spec.Pos()), directory, imported)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||