Files
Crimson-Gatekeeper/srv/cmd/tool/main.go

60 lines
1.3 KiB
Go
Raw Normal View History

2026-02-25 20:10:41 +08:00
package tool
import (
"fmt"
2026-02-26 11:37:56 +08:00
"go/types"
2026-02-25 20:10:41 +08:00
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
)
func main() {
pwd, err := os.Getwd()
if err != nil {
fmt.Println("无法获取当前工作目录")
panic(err)
}
sep := string(filepath.Separator)
s := strings.Split(pwd, sep)
var project string
for i := len(s); i > 0; i-- {
base := strings.Join(s[:i], sep)
module := filepath.Join(base, "go.mod")
_, err := os.Stat(module)
if err == nil {
project = module
break
}
}
if project == "" {
panic("没有找到 go.mod ,请检查是否在 module 中调用程序")
}
project = filepath.Dir(project)
fmt.Println("当前寻根目录 " + project)
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedDeps,
Tests: false,
}, filepath.Join(project, "..."))
if err != nil {
fmt.Println("解析项目失败")
panic(err)
}
for _, pkg := range pkgs {
2026-02-26 11:37:56 +08:00
top := pkg.Types.Scope()
funcs := []*types.Func{}
for _, name := range top.Names() {
obj := top.Lookup(name)
if !strings.HasSuffix(obj.Name(), "Provider") {
continue
}
fun, ok := obj.(*types.Func)
if ok {
funcs = append(funcs, fun)
}
}
2026-02-25 20:10:41 +08:00
fmt.Println(pkg.Name)
}
}