初始化提交

This commit is contained in:
2026-04-07 22:44:49 +08:00
commit c47a39b2b3
10 changed files with 180 additions and 0 deletions

52
cmd/zire/main.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"zire/internal/tools"
"golang.org/x/tools/go/packages"
)
func main() {
str, err := getModPath()
if err != nil {
fmt.Println(err.Error())
panic(err)
}
cfg := &packages.Config{
Mode: packages.NeedFiles | packages.NeedTypesInfo | packages.NeedFiles | packages.NeedTypes | packages.NeedName,
Dir: str,
}
pkgs, err := packages.Load(cfg, "./...")
tools.Check(err, "加载包失败")
for _, pkg := range pkgs {
fmt.Println(pkg.Name)
fmt.Println(pkg.TypesInfo.Scopes)
}
}
func getModPath() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("获取当前目录失败: %w", err)
}
for {
modPath := filepath.Join(dir, "go.mod")
if _, err := os.Stat(modPath); err == nil {
return filepath.Dir(modPath), nil
}
parentDir := filepath.Dir(dir)
if parentDir == dir {
break // 到达根目录
}
dir = parentDir
}
return "", errors.New("没有找到 go.mod 文件")
}