forked from piranha/goreplace
/
goreplace.go
400 lines (331 loc) · 8.08 KB
/
goreplace.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// (c) 2011-2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"bytes"
"errors"
"fmt"
goopt "github.com/droundy/goopt"
color "github.com/wsxiaoys/terminal/color"
"os"
"path/filepath"
"regexp"
)
var (
Author = "Alexander Solovyov"
Version = "0.3.8"
Summary = "gr [OPTS] string-to-search\n"
byteNewLine []byte = []byte("\n")
ignoreCase = goopt.Flag([]string{"-i", "--ignore-case"}, []string{},
"ignore pattern case", "")
onlyName = goopt.Flag([]string{"-n", "--filename"}, []string{},
"print only filenames", "")
ignoreFiles = goopt.Strings([]string{"-x", "--exclude"}, "RE",
"exclude files that match the regexp from search")
singleline = goopt.Flag([]string{"-s", "--singleline"}, []string{},
"match on a single line (^/$ will be beginning/end of line)", "")
plaintext = goopt.Flag([]string{"-p", "--plain"}, []string{},
"search plain text", "")
replace = goopt.String([]string{"-r", "--replace"}, "",
"replace found substrings with this string")
force = goopt.Flag([]string{"--force"}, []string{},
"force replacement in binary files", "")
showVersion = goopt.Flag([]string{"-v", "--version"}, []string{},
"show version and exit", "")
noIgnoresGlobal = goopt.Flag([]string{"-I", "--no-autoignore"}, []string{},
"do not read .git/.hgignore files", "")
)
func main() {
goopt.Author = Author
goopt.Version = Version
goopt.Summary = Summary
goopt.Usage = func() string {
return fmt.Sprintf("Usage of goreplace %s:\n\t", Version) +
goopt.Summary + "\n" + goopt.Help()
}
var noIgnores bool
for _, item := range os.Args[1:] {
if item == "-I" || item == "--no-autoignore" {
noIgnores = true
}
}
cwd, _ := os.Getwd()
ignorer := NewIgnorer(cwd, noIgnores)
goopt.Summary += fmt.Sprintf("\n%s", ignorer)
goopt.Parse(nil)
if *showVersion {
fmt.Printf("goreplace %s\n", goopt.Version)
return
}
ignorer.Append(*ignoreFiles)
if len(goopt.Args) == 0 {
println(goopt.Usage())
return
}
arg := goopt.Args[0]
if *plaintext {
arg = escapeRegex(arg)
}
if *ignoreCase {
arg = "(?i:" + arg + ")"
}
pattern, err := regexp.Compile(arg)
errhandle(err, true, "")
if pattern.Match([]byte("")) {
errhandle(errors.New("Your pattern matches empty string"), true, "")
}
searchFiles(pattern, ignorer)
}
func errhandle(err error, exit bool, moreinfo string, a ...interface{}) bool {
if err == nil {
return false
}
fmt.Fprintf(os.Stderr, "%s\n%s\n", err, fmt.Sprintf(moreinfo, a...))
if exit {
os.Exit(1)
}
return true
}
func searchFiles(pattern *regexp.Regexp, ignorer Ignorer) {
v := &GRVisitor{pattern, ignorer, false}
errors := make(chan error, 64)
filepath.Walk(".", walkFunc(v, errors))
select {
case err := <-errors:
errhandle(err, false, "")
default:
}
}
func walkFunc(v *GRVisitor, errors chan<- error) filepath.WalkFunc {
return func(fn string, fi os.FileInfo, err error) error {
if err != nil {
errors <- err
return nil
}
// NOTE: if a directory is a symlink, filepath.Walk won't recurse inside
if fi.Mode()&os.ModeSymlink != 0 {
if fi, err = os.Stat(fn); err != nil {
errors <- err
return nil
}
}
if fi.IsDir() {
if !v.VisitDir(fn, fi) {
return filepath.SkipDir
}
return nil
}
v.VisitFile(fn, fi)
return nil
}
}
type GRVisitor struct {
pattern *regexp.Regexp
ignorer Ignorer
// Used to prevent sparse newline at the end of output
prependNewLine bool
}
func (v *GRVisitor) VisitDir(fn string, fi os.FileInfo) bool {
return !v.ignorer.Ignore(fi.Name(), true)
}
func (v *GRVisitor) VisitFile(fn string, fi os.FileInfo) {
if fi.IsDir() {
return
}
if fi.Size() >= 1024*1024*10 {
fmt.Fprintf(os.Stderr, "Skipping %s, too big: %d\n", fn, fi.Size())
return
}
if fi.Size() == 0 {
return
}
if v.ignorer.Ignore(fn, false) {
return
}
f, content := v.GetFileAndContent(fn, fi)
defer f.Close()
if len(*replace) == 0 {
v.SearchFile(fn, content)
return
}
changed, result := v.ReplaceInFile(fn, content)
if changed {
f.Seek(0, 0)
n, err := f.Write(result)
errhandle(err, true, "Error writing replacement in file %s", fn)
if int64(n) < fi.Size() {
err := f.Truncate(int64(n))
errhandle(err, true, "Error truncating file to size %d", f)
}
}
}
func (v *GRVisitor) GetFileAndContent(fn string, fi os.FileInfo) (f *os.File, content []byte) {
var err error
var msg string
if len(*replace) > 0 {
f, err = os.OpenFile(fn, os.O_RDWR, 0666)
msg = "can't open file %s for reading and writing"
} else {
f, err = os.Open(fn)
msg = "can't open file %s for reading"
}
if err != nil {
errhandle(err, false, msg, fn)
return
}
content = make([]byte, fi.Size())
n, err := f.Read(content)
errhandle(err, true, "can't read file %s", fn)
if int64(n) != fi.Size() {
panic(fmt.Sprintf("Not whole file was read, only %d from %d",
n, fi.Size()))
}
return
}
func (v *GRVisitor) SearchFile(fn string, content []byte) {
lines := IntList([]int{})
binary := false
if bytes.IndexByte(content, 0) != -1 {
binary = true
}
for _, info := range v.FindAllIndex(content) {
if lines.Contains(info.num) {
continue
}
if v.prependNewLine {
fmt.Println("")
v.prependNewLine = false
}
first := len(lines) == 0
lines = append(lines, info.num)
if first {
if binary && !*onlyName {
fmt.Printf("Binary file %s matches\n", fn)
break
} else {
color.Printf("@g%s\n", fn)
}
}
if *onlyName {
return
}
color.Printf("@!@y%d:", info.num)
coloredLine := v.pattern.ReplaceAllStringFunc(string(info.line),
func(wrap string) string {
return color.Sprintf("@Y%s", wrap)
})
fmt.Printf("%s\n", coloredLine)
}
if len(lines) > 0 {
v.prependNewLine = true
}
}
func getSuffix(num int) string {
if num > 1 {
return "s"
}
return ""
}
func (v *GRVisitor) ReplaceInFile(fn string, content []byte) (changed bool, result []byte) {
changed = false
binary := false
changenum := 0
if *singleline {
panic("Can't handle singleline replacements yet")
}
if *plaintext {
panic("Can't handle plain text replacements yet")
}
if bytes.IndexByte(content, 0) != -1 {
binary = true
}
result = v.pattern.ReplaceAllFunc(content, func(s []byte) []byte {
if binary && !*force {
errhandle(
errors.New("supply --force to force change of binary file"),
false, "")
}
if !changed {
changed = true
color.Printf("@g%s", fn)
}
changenum += 1
return []byte(*replace)
})
if changenum > 0 {
color.Printf("@!@y - %d change%s made\n",
changenum, getSuffix(changenum))
}
return changed, result
}
type LineInfo struct {
num int
line []byte
}
func (v *GRVisitor) FindAllIndex(content []byte) (res []*LineInfo) {
if *singleline {
return v.singlelineFindAllIndex(content)
}
linenum, last := 1, 0
for _, bounds := range v.pattern.FindAllIndex(content, -1) {
linenum += bytes.Count(content[last:bounds[0]], byteNewLine)
last = bounds[0]
begin, end := beginend(content, bounds[0], bounds[1])
res = append(res, &LineInfo{linenum, content[begin:end]})
}
return res
}
func (v *GRVisitor) singlelineFindAllIndex(content []byte) (res []*LineInfo) {
linenum, begin, end := 1, 0, 0
for i := 0; i < len(content); i++ {
if content[i] == '\n' {
end = i
line := content[begin:end]
if v.pattern.Match(line) {
res = append(res, &LineInfo{linenum, line})
}
linenum += 1
begin = end + 1
}
}
return res
}
// Given a []byte, start and finish of some inner slice, will find nearest
// newlines on both ends of this slice
func beginend(s []byte, start int, finish int) (begin int, end int) {
begin = 0
end = len(s)
for i := start; i >= 0; i-- {
if s[i] == byteNewLine[0] {
begin = i + 1
break
}
}
// -1 to check if current location is the end of a string
for i := finish - 1; i < len(s); i++ {
if s[i] == byteNewLine[0] {
end = i
break
}
}
return
}
type IntList []int
func (il IntList) Contains(i int) bool {
for _, x := range il {
if x == i {
return true
}
}
return false
}
func escapeRegex(arg string) (escaped string){
toEscape := "\\.()[]{}+*?|^$"
for _, c := range arg {
if strings.ContainsRune(toEscape, c) {
escaped += "\\"
}
escaped += string(c)
}
return escaped
}