-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (59 loc) · 1.41 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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorBlue = "\033[34m"
colorCyan = "\033[36m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorMagenta = "\033[35m"
)
func processDirectory(directory string) {
var goFiles []string
var filesAST []*ast.File
entries, err := os.ReadDir(directory)
if err != nil {
fmt.Printf("%sError reading directory '%s': %v%s\n", colorRed, directory, err, colorReset)
return
}
for _, entry := range entries {
entryPath := filepath.Join(directory, entry.Name())
if entry.IsDir() {
processDirectory(entryPath)
continue
}
if strings.HasSuffix(entry.Name(), ".go") && !strings.HasSuffix(entry.Name(), "_test.go") {
goFiles = append(goFiles, entryPath)
}
}
if len(goFiles) == 0 {
return
}
fmt.Printf("%sProcessing directory: %s%s\n", colorBlue, directory, colorReset)
fset := token.NewFileSet()
for _, filePath := range goFiles {
fileAST, err := parser.ParseFile(fset, filePath, nil, 0)
if err != nil {
fmt.Printf("%sError parsing file '%s': %v%s\n", colorRed, filePath, err, colorReset)
continue
}
filesAST = append(filesAST, fileAST)
}
processAST(filesAST)
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("%sUsage: %s <directory>%s\n", colorRed, os.Args[0], colorReset)
return
}
processDirectory(os.Args[1])
}