107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
// 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/zhangdeman/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" || directory == "ratelimit":
|
|
invalid = invalid || strings.HasPrefix(imported, "github.com/gin-gonic/") || imported == module+"/http" || strings.HasPrefix(imported, module+"/http/")
|
|
case strings.HasPrefix(directory, "archive/"):
|
|
invalid = invalid || strings.HasPrefix(imported, "github.com/gin-gonic/") || strings.HasPrefix(imported, module+"/http") || strings.HasPrefix(imported, module+"/database") || strings.HasPrefix(imported, module+"/redis")
|
|
case strings.HasPrefix(directory, "security/password"):
|
|
invalid = invalid || (strings.Contains(strings.Split(imported, "/")[0], ".") && imported != "golang.org/x/crypto/argon2")
|
|
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
|
|
})
|
|
}
|