抽取 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,129 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
capabilityRow = regexp.MustCompile("^\\|\\s*`([^`]+)`\\s*\\|")
|
||||
markdownLink = regexp.MustCompile(`\]\(([^)]+)\)`)
|
||||
)
|
||||
|
||||
// checkDocs 核对接入指南公开包清单与仓库内 Markdown 相对链接。
|
||||
// 入参:root 为项目根目录,不按当前平台过滤生产源码。
|
||||
// 返回:缺少指南、能力清单漂移或失效相对链接时返回具体位置。
|
||||
func checkDocs(root string) error {
|
||||
guide := filepath.Join(root, "docs", "getting-started.md")
|
||||
content, err := os.ReadFile(guide)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read integration guide: %w", err)
|
||||
}
|
||||
text := string(content)
|
||||
const start = "<!-- capabilities:start -->"
|
||||
const end = "<!-- capabilities:end -->"
|
||||
first, last := strings.Index(text, start), strings.Index(text, end)
|
||||
if first < 0 || last < first+len(start) || strings.Count(text, start) != 1 || strings.Count(text, end) != 1 {
|
||||
return fmt.Errorf("%s: capability table markers are missing or ambiguous", guide)
|
||||
}
|
||||
declared := make(map[string]bool)
|
||||
for _, line := range strings.Split(text[first+len(start):last], "\n") {
|
||||
match := capabilityRow.FindStringSubmatch(strings.TrimSpace(line))
|
||||
if match == nil {
|
||||
continue
|
||||
}
|
||||
if declared[match[1]] {
|
||||
return fmt.Errorf("%s: duplicate capability %s", guide, match[1])
|
||||
}
|
||||
declared[match[1]] = true
|
||||
}
|
||||
actual := make(map[string]bool)
|
||||
err = filepath.WalkDir(root, 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, ".md") {
|
||||
if err := checkMarkdownLinks(root, path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
relative, err := filepath.Rel(root, filepath.Dir(path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
directory := filepath.ToSlash(relative)
|
||||
if directory == "internal" || strings.HasPrefix(directory, "internal/") || directory == "examples" || strings.HasPrefix(directory, "examples/") || directory == "script" || strings.HasPrefix(directory, "script/") {
|
||||
return nil
|
||||
}
|
||||
file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.PackageClauseOnly)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if file.Name.Name != "main" {
|
||||
actual[directory] = true
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for path := range actual {
|
||||
if !declared[path] {
|
||||
return fmt.Errorf("%s: public package %s is undocumented; update integration guide in the same change", guide, path)
|
||||
}
|
||||
}
|
||||
for path := range declared {
|
||||
if !actual[path] {
|
||||
return fmt.Errorf("%s: removed or renamed public package %s is still documented", guide, path)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkMarkdownLinks 检查仓库相对目标,不访问外部 URL 或推断页面锚点。
|
||||
// 入参:root 为项目根目录,path 为 Markdown 文件。
|
||||
// 返回:链接路径越出仓库、转义无效或文件缺失时返回具体位置。
|
||||
func checkMarkdownLinks(root string, path string) error {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, match := range markdownLink.FindAllStringSubmatch(string(content), -1) {
|
||||
target := strings.Trim(match[1], "<>")
|
||||
parsed, err := url.Parse(target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: invalid Markdown target %q: %w", path, target, err)
|
||||
}
|
||||
if parsed.IsAbs() || parsed.Host != "" || parsed.Path == "" {
|
||||
continue
|
||||
}
|
||||
destination := filepath.Join(filepath.Dir(path), filepath.FromSlash(parsed.Path))
|
||||
relative, err := filepath.Rel(root, destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return fmt.Errorf("%s: link escapes repository: %s", path, target)
|
||||
}
|
||||
if _, err = os.Stat(destination); err != nil {
|
||||
return fmt.Errorf("%s: broken repository link %s: %w", path, target, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeDocFixture(t *testing.T, root string, path string, body string) {
|
||||
t.Helper()
|
||||
destination := filepath.Join(root, path)
|
||||
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(destination, []byte(body), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocsCheckDetectsAddedAndRemovedCapabilities(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
guide := "<!-- capabilities:start -->\n| `config` | YAML |\n<!-- capabilities:end -->\n"
|
||||
writeDocFixture(t, root, "docs/getting-started.md", guide)
|
||||
writeDocFixture(t, root, "config/load.go", "package config\n")
|
||||
if err := checkDocs(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeDocFixture(t, root, "newcap/platform.go", "//go:build windows\n\npackage newcap\n")
|
||||
if err := checkDocs(root); err == nil || !strings.Contains(err.Error(), "newcap is undocumented") {
|
||||
t.Fatalf("new platform capability was not detected: %v", err)
|
||||
}
|
||||
guide = strings.Replace(guide, "<!-- capabilities:end -->", "| `newcap` | new capability |\n<!-- capabilities:end -->", 1)
|
||||
writeDocFixture(t, root, "docs/getting-started.md", guide)
|
||||
if err := checkDocs(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(filepath.Join(root, "newcap/platform.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := checkDocs(root); err == nil || !strings.Contains(err.Error(), "newcap is still documented") {
|
||||
t.Fatalf("removed capability was not detected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocsCheckValidatesRepositoryLinks(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
guide := "<!-- capabilities:start -->\n| `config` | YAML |\n<!-- capabilities:end -->\n[example](../examples/sample.go)\n"
|
||||
writeDocFixture(t, root, "docs/getting-started.md", guide)
|
||||
writeDocFixture(t, root, "config/load.go", "package config\n")
|
||||
if err := checkDocs(root); err == nil || !strings.Contains(err.Error(), "broken repository link") {
|
||||
t.Fatalf("missing link was not detected: %v", err)
|
||||
}
|
||||
writeDocFixture(t, root, "examples/sample.go", "package main\n")
|
||||
if err := checkDocs(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeDocFixture(t, root, "README.md", "[guide](docs/getting-started.md)\n[external](https://example.test/guide)\n")
|
||||
if err := checkDocs(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||