/
parse_struct_import_path.go
153 lines (127 loc) · 3 KB
/
parse_struct_import_path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package parser
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
)
type ImportPath struct {
}
const (
gomodFileName = "go.mod"
)
func (p *ImportPath) GetCurrentDirModFilePath() (modDir, modPath string, err error) {
// 获取目录
dir, err := os.Getwd()
if err != nil {
return
}
modDir, modPath, err = getModFilePath(dir)
if err != nil {
return
}
return
}
func (p *ImportPath) GetModFilePath(dir string) (modDir, modPath string, err error) {
return getModFilePath(dir)
}
func getModFilePath(dir string) (modDir, modPath string, err error) {
// 找到go.mod所在目录
var modfilePath string
var modfileDir = dir
for {
modfilePath = filepath.Join(modfileDir, gomodFileName)
_, err := os.Stat(modfilePath)
if os.IsNotExist(err) {
// 不存在,则继续往上层目录找
tmpDir, file := filepath.Split(modfileDir)
if file == "" {
modfilePath = ""
break
}
modfileDir = filepath.Clean(tmpDir)
continue
}
break
}
if modfilePath == "" {
return "", "", fmt.Errorf("can't find go mod file")
}
modDir = modfileDir
modPath = modfilePath
return
}
// GetCurrentDirPath get import path in current directory
func (p *ImportPath) GetByCurrentDir() (path string, err error) {
return getPkgPathFromDir()
}
func getPkgPathFromDir() (pkgPath string, err error) {
// 获取目录
dir, err := os.Getwd()
if err != nil {
return
}
modfileDir, modfilePath, err := getModFilePath(dir)
if err != nil {
return
}
// 解析目录里的go.mod文件,获取模块名
content, err := os.ReadFile(modfilePath)
if err != nil {
return
}
modPath := modfile.ModulePath(content)
// 拿到go.mod所在目录和模块名,再结合当前目录信息,得到当前包路径
// modPath + (modfileDir - dir)
relPart := strings.ReplaceAll(dir, modfileDir, "")
pkgPath = filepath.Join(modPath, relPart)
return
}
func (p *ImportPath) SplitImportPathWithType(importPathWithType string) (string, string) {
lastDotIndex := strings.LastIndex(importPathWithType, ".")
if lastDotIndex == -1 || lastDotIndex == len(importPathWithType)-1 || lastDotIndex == 0 {
panic(fmt.Errorf("路径有问题: %s", importPathWithType))
}
return importPathWithType[:lastDotIndex], importPathWithType[lastDotIndex+1:]
}
type Module struct {
modfile.Module
RelDir string
}
// FindAllModule find all module in dir
func (p *ImportPath) FindAllModule(dir string) (mods []Module, err error) {
if err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
if errors.Is(err, os.ErrPermission) {
return filepath.SkipDir
}
return err
}
if d.IsDir() {
return nil
}
if d.Name() != "go.mod" {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
f, err := modfile.Parse(path, content, nil)
if err != nil {
return err
}
mod := Module{
Module: *f.Module,
RelDir: filepath.Dir(path),
}
mods = append(mods, mod)
return nil
}); err != nil {
return
}
return
}