-
Notifications
You must be signed in to change notification settings - Fork 28
/
what_to_do.txt
99 lines (61 loc) · 1.76 KB
/
what_to_do.txt
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
can select the web framework
can select the database
can do transaction
can select the log library
can store the log into file
create a documentation
refactor the internal code
create a tutorial for gogen
add to go-awesome
https://github.com/Redocly/redoc
interactive command line
adding timeout in usecase
create unit testing
go mod ast
func ReadAllAvailablePackageFromImport(parsedGoFile string) ([]string, error) {
const goModFileName = "go.mod"
mapOfRequire := map[string]string{}
dataInBytes, err := os.ReadFile(goModFileName)
if err != nil {
return nil, err
}
parsedGoMod, err := modfile.Parse(goModFileName, dataInBytes, nil)
if err != nil {
return nil, err
}
for _, r := range parsedGoMod.Require {
if len(r.Syntax.Token) == 1 {
mapOfRequire[r.Syntax.Token[0]] = fmt.Sprintf("%v/pkg/mod/%v", build.Default.GOPATH, r.Syntax.Token[0])
continue
}
mapOfRequire[r.Syntax.Token[0]] = fmt.Sprintf("%v/pkg/mod/%v@%v", build.Default.GOPATH, r.Syntax.Token[0], r.Syntax.Token[1])
}
file, err := parser.ParseFile(token.NewFileSet(), parsedGoFile, nil, parser.ImportsOnly)
if err != nil {
return nil, err
}
packages := make([]string, 0)
ast.Inspect(file, func(node ast.Node) bool {
if err != nil {
return false
}
switch n := node.(type) {
case *ast.ImportSpec:
pathValue := strings.Trim(n.Path.Value, `"`)
for k, m := range mapOfRequire {
if strings.HasPrefix(pathValue, k) {
pathToLib := fmt.Sprintf("%v%v", m, pathValue[len(k):])
pkgs, err := parser.ParseDir(token.NewFileSet(), pathToLib, nil, parser.PackageClauseOnly)
if err != nil {
return false
}
for _, pkg := range pkgs {
packages = append(packages, pkg.Name)
}
}
}
}
return true
})
return packages, nil
}