-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (128 loc) · 3.02 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
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
src = flag.String("src", "./", "source dir or file")
exclude = flag.String("exclude", "", "exclude path names, separated by commas")
filePatternRE = regexp.MustCompile(`\[[A-Za-z0-9_\-\.\\\/]+\.go\:\d+\]`)
)
func readDir(d string, prefixLen int) {
if len(d) == 0 {
log.Printf("empty dir name")
return
}
if !strings.HasPrefix(d, "..") && d[0] == '.' {
log.Printf("skip hidden dir: %s", d)
return
}
dirs, err := os.ReadDir(d)
if err != nil {
cur, _ := os.Getwd()
log.Printf("read dir %s error, err=%s\n\t%s", d, err, cur)
}
//b := path.Base(d)
for _, item := range dirs {
if item.IsDir() {
if _, has := excludePaths[item.Name()]; has {
continue
}
newDir := filepath.Join(d, item.Name())
//fmt.Printf(" read: %s\n", newDir)
readDir(newDir, prefixLen)
continue
}
if strings.HasSuffix(item.Name(), ".go") {
f := filepath.Join(d, item.Name())
//fmt.Printf(" read: %s\n", f)
readFile(f, prefixLen)
}
}
}
func readFile(f string, prefixLen int) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, f, nil, parser.ParseComments)
if err != nil {
log.Printf("parse go file %s error, err=%s", f, err)
return
}
isModify := false
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
for idx, arg := range x.Args {
if lit, ok := arg.(*ast.BasicLit); ok && lit.Kind == token.STRING {
if len(lit.Value) >= 7 &&
lit.Value[1] == '[' && lit.Value[len(lit.Value)-2] == ']' &&
filePatternRE.MatchString(lit.Value) {
pos := fset.Position(n.Pos())
replaceTo := fmt.Sprintf("\"[%s:%d]\"", pos.Filename[prefixLen:], pos.Line)
if lit.Value == replaceTo {
continue
}
isModify = true
log.Printf("Found a constant string argument:%+v, replace to \"[%s:%d]\"\n", lit.Value, pos.Filename[prefixLen:], pos.Line)
newArg := &ast.BasicLit{
Kind: token.STRING,
Value: replaceTo,
}
x.Args[idx] = newArg
}
}
}
}
return true
})
if !isModify {
return
}
var buf bytes.Buffer
err = printer.Fprint(&buf, fset, file)
if err != nil {
log.Printf("format go file %s error, err=%s", f, err)
}
if err := os.WriteFile(f, buf.Bytes(), os.ModePerm); err != nil {
log.Printf("write go file %s error, err=%s", f, err)
}
}
var excludePaths = map[string]struct{}{}
func main() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
flag.Parse()
if len(*src) == 0 {
*src = "./"
}
info, err := os.Stat(*src)
if err != nil {
log.Println(err)
}
if len(*exclude) > 0 {
arr := strings.Split(*exclude, ",")
for _, item := range arr {
excludePaths[strings.Trim(item, " ")] = struct{}{}
}
}
if info.IsDir() {
a, err := filepath.Abs(*src)
if err != nil {
log.Println("get abs path error:", err, *src)
return
}
readDir(a, len(a))
return
}
if strings.HasSuffix(*src, ".go") {
readFile(*src, 0)
}
}