-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (142 loc) · 4.3 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bufio"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
func main() {
mainFilePath := "/home/runner/ThinUntimelyNanocad/utils/h.go"
projectRoot := "/home/runner/ThinUntimelyNanocad"
outputFilePath := "./structs_log.txt"
moduleName, err := getModuleName(filepath.Join(projectRoot, "go.mod"))
if err != nil {
fmt.Println("Error reading go.mod:", err)
return
}
logFile, err := os.Create(outputFilePath)
if err != nil {
fmt.Println("Error creating log file:", err)
return
}
defer logFile.Close()
imports, err := getImports(mainFilePath)
if err != nil {
fmt.Println("Error getting imports:", err)
return
}
internalImports := filterInternalImports(imports, moduleName)
for _, imp := range internalImports {
logFile.WriteString(fmt.Sprintf("Structs in internal package: %s\n", imp))
err := findStructsInPackage(projectRoot, imp, moduleName, logFile)
if err != nil {
fmt.Println("Error parsing package:", err)
}
}
}
// getModuleName reads the module name from the go.mod file
func getModuleName(goModPath string) (string, error) {
file, err := os.Open(goModPath)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "module ") {
return strings.TrimSpace(strings.TrimPrefix(line, "module ")), nil
}
}
return "", fmt.Errorf("module name not found in go.mod")
}
func getImports(filepath string) ([]string, error) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filepath, nil, parser.ImportsOnly)
if err != nil {
return nil, err
}
var imports []string
for _, imp := range node.Imports {
importPath := strings.Trim(imp.Path.Value, `"`)
imports = append(imports, importPath)
}
return imports, nil
}
// filterInternalImports returns only the imports that start with the module name
func filterInternalImports(imports []string, moduleName string) []string {
var internalImports []string
for _, imp := range imports {
if strings.HasPrefix(imp, moduleName) {
internalImports = append(internalImports, imp)
}
}
return internalImports
}
func findStructsInPackage(projectRoot, packagePath, moduleName string, logFile *os.File) error {
relativePath := strings.TrimPrefix(packagePath, moduleName+"/")
packageDir := filepath.Join(projectRoot, filepath.FromSlash(relativePath))
err := filepath.Walk(packageDir, func(path string, info os.FileInfo, err error) error {
if err != nil || filepath.Ext(path) != ".go" {
return nil
}
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.AllErrors)
if err != nil {
return err
}
// Log file path
logFile.WriteString(fmt.Sprintf("File: %s\n", path))
foundStruct := false
ast.Inspect(node, func(n ast.Node) bool {
ts, ok := n.(*ast.TypeSpec)
if ok {
if structType, isStruct := ts.Type.(*ast.StructType); isStruct {
foundStruct = true
logFile.WriteString(fmt.Sprintf(" Struct: %s\n", ts.Name.Name))
logStructFields(structType, logFile)
}
}
return true
})
if !foundStruct {
logFile.WriteString(" No structs found in this file.\n")
}
return nil
})
return err
}
func logStructFields(structType *ast.StructType, logFile *os.File) {
for _, field := range structType.Fields.List {
var fieldNames []string
for _, name := range field.Names {
fieldNames = append(fieldNames, name.Name)
}
// Get field type as string
fieldType := exprToString(field.Type)
// Log field names and types
logFile.WriteString(fmt.Sprintf(" Field: %s, Type: %s\n", strings.Join(fieldNames, ", "), fieldType))
}
}
func exprToString(expr ast.Expr) string {
switch v := expr.(type) {
case *ast.Ident:
return v.Name
case *ast.SelectorExpr:
return fmt.Sprintf("%s.%s", exprToString(v.X), v.Sel.Name)
case *ast.StarExpr:
return "*" + exprToString(v.X)
case *ast.ArrayType:
return "[]" + exprToString(v.Elt)
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", exprToString(v.Key), exprToString(v.Value))
case *ast.StructType:
return "struct{...}"
default:
return fmt.Sprintf("%T", expr)
}
}