-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
439 lines (359 loc) · 12.2 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
flags "github.com/jessevdk/go-flags"
"github.com/remeh/sizedwaitgroup"
"io/ioutil"
"os"
"regexp"
"strings"
)
const (
Author = "wartner.io"
Version = "1.0.0"
)
type changeset struct {
SearchPlain string
Search *regexp.Regexp
Replace string
MatchFound bool
}
type changeresult struct {
File fileitem
Output string
Status bool
Error error
}
type fileitem struct {
Path string
Output string
}
var opts struct {
ThreadCount int ` long:"threads" description:"Set thread concurrency for replacing in multiple files at same time" default:"20"`
Mode string `short:"m" long:"mode" description:"replacement mode - replace: replace match with term; line: replace line with term; lineinfile: replace line with term or if not found append to term to file; template: parse content as golang template, search value have to start uppercase" default:"replace" choice:"replace" choice:"line" choice:"lineinfile" choice:"template"`
ModeIsReplaceMatch bool
ModeIsReplaceLine bool
ModeIsLineInFile bool
ModeIsTemplate bool
Search []string `short:"s" long:"search" description:"search term"`
Replace []string `short:"r" long:"replace" description:"replacement term"`
LineinfileBefore string ` long:"lineinfile-before" description:"add line before this regex"`
LineinfileAfter string ` long:"lineinfile-after" description:"add line after this regex"`
CaseInsensitive bool `short:"i" long:"case-insensitive" description:"ignore case of pattern to match upper and lowercase characters"`
Stdin bool ` long:"stdin" description:"process stdin as input"`
Output string `short:"o" long:"output" description:"write changes to this file (in one file mode)"`
OutputStripFileExt string ` long:"output-strip-ext" description:"strip file extension from written files (also available in multi file mode)"`
Once string ` long:"once" description:"replace search term only one in a file, keep duplicaes (keep, default) or remove them (unique)" optional:"true" optional-value:"keep" choice:"keep" choice:"unique"`
Regex bool ` long:"regex" description:"treat pattern as regex"`
RegexBackref bool ` long:"regex-backrefs" description:"enable backreferences in replace term"`
RegexPosix bool ` long:"regex-posix" description:"parse regex term as POSIX regex"`
Path string ` long:"path" description:"use files in this path"`
PathPattern string ` long:"path-pattern" description:"file pattern (* for wildcard, only basename of file)"`
PathRegex string ` long:"path-regex" description:"file pattern (regex, full path)"`
IgnoreEmpty bool ` long:"ignore-empty" description:"ignore empty file list, otherwise this will result in an error"`
Verbose bool `short:"v" long:"verbose" description:"verbose mode"`
DryRun bool ` long:"dry-run" description:"dry run mode"`
ShowVersion bool `short:"V" long:"version" description:"show version and exit"`
ShowOnlyVersion bool ` long:"dumpversion" description:"show only version number and exit"`
ShowHelp bool `short:"h" long:"help" description:"show this help message"`
}
var pathFilterDirectories = []string{"autom4te.cache", "blib", "_build", ".bzr", ".cdv", "cover_db", "CVS", "_darcs", "~.dep", "~.dot", ".git", ".hg", "~.nib", ".pc", "~.plst", "RCS", "SCCS", "_sgbak", ".svn", "_obj", ".idea"}
func applyChangesetsToFile(fileitem fileitem, changesets []changeset) (string, bool, error) {
var (
err error = nil
output string = ""
status bool = true
)
file, err := os.Open(fileitem.Path)
if err != nil {
return output, false, err
}
writeBufferToFile := false
var buffer bytes.Buffer
r := bufio.NewReader(file)
line, e := Readln(r)
for e == nil {
newLine, lineChanged, skipLine := applyChangesetsToLine(line, changesets)
if lineChanged || skipLine {
writeBufferToFile = true
}
if !skipLine {
buffer.WriteString(newLine + "\n")
}
line, e = Readln(r)
}
file.Close()
if opts.ModeIsLineInFile {
lifBuffer, lifStatus := handleLineInFile(changesets, buffer)
if lifStatus {
buffer.Reset()
buffer.WriteString(lifBuffer.String())
writeBufferToFile = lifStatus
}
}
if opts.Output != "" || opts.OutputStripFileExt != "" {
writeBufferToFile = true
}
if writeBufferToFile {
output, status = writeContentToFile(fileitem, buffer)
} else {
output = fmt.Sprintf("%s no match", fileitem.Path)
}
return output, status, err
}
func applyTemplateToFile(fileitem fileitem, changesets []changeset) (string, bool, error) {
var (
err error = nil
output string = ""
status bool = true
)
buffer, err := ioutil.ReadFile(fileitem.Path)
if err != nil {
return output, false, err
}
content := parseContentAsTemplate(string(buffer), changesets)
output, status = writeContentToFile(fileitem, content)
return output, status, err
}
func applyChangesetsToLine(line string, changesets []changeset) (string, bool, bool) {
changed := false
skipLine := false
for i, changeset := range changesets {
if opts.Once != "" && changeset.MatchFound {
if opts.Once == "unique" && searchMatch(line, changeset) {
skipLine = true
changed = true
break
}
} else {
if searchMatch(line, changeset) {
if opts.ModeIsReplaceLine || opts.ModeIsLineInFile {
if opts.RegexBackref {
line = string(changeset.Search.Find([]byte(line)))
line = changeset.Search.ReplaceAllString(line, changeset.Replace)
} else {
line = changeset.Replace
}
} else {
line = replaceText(line, changeset)
}
changesets[i].MatchFound = true
changed = true
}
}
}
return line, changed, skipLine
}
func buildSearchTerm(term string) *regexp.Regexp {
var ret *regexp.Regexp
var regex string
if opts.Regex {
regex = term
} else {
regex = regexp.QuoteMeta(term)
}
if opts.CaseInsensitive {
regex = "(?i:" + regex + ")"
}
if opts.Verbose {
logMessage(fmt.Sprintf("Using regular expression: %s", regex))
}
if opts.RegexPosix {
ret = regexp.MustCompilePOSIX(regex)
} else {
ret = regexp.MustCompile(regex)
}
return ret
}
func handleSpecialCliOptions(args []string) {
if opts.ShowOnlyVersion {
fmt.Println(Version)
os.Exit(0)
}
if opts.ShowVersion {
fmt.Println(fmt.Sprintf("replace version %s", Version))
fmt.Println(fmt.Sprintf("Copyright (C) 2019 %s", Author))
os.Exit(0)
}
if opts.ShowHelp {
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
switch mode := opts.Mode; mode {
case "replace":
opts.ModeIsReplaceMatch = true
opts.ModeIsReplaceLine = false
opts.ModeIsLineInFile = false
opts.ModeIsTemplate = false
case "line":
opts.ModeIsReplaceMatch = false
opts.ModeIsReplaceLine = true
opts.ModeIsLineInFile = false
opts.ModeIsTemplate = false
case "lineinfile":
opts.ModeIsReplaceMatch = false
opts.ModeIsReplaceLine = false
opts.ModeIsLineInFile = true
opts.ModeIsTemplate = false
case "template":
opts.ModeIsReplaceMatch = false
opts.ModeIsReplaceLine = false
opts.ModeIsLineInFile = false
opts.ModeIsTemplate = true
}
if opts.Output != "" && len(args) > 1 {
logFatalErrorAndExit(errors.New("Only one file is allowed when using --output"), 1)
}
if opts.LineinfileBefore != "" || opts.LineinfileAfter != "" {
if !opts.ModeIsLineInFile {
logFatalErrorAndExit(errors.New("--lineinfile-after and --lineinfile-before only valid in --mode=lineinfile"), 1)
}
if opts.LineinfileBefore != "" && opts.LineinfileAfter != "" {
logFatalErrorAndExit(errors.New("Only --lineinfile-after or --lineinfile-before is allowed in --mode=lineinfile"), 1)
}
}
}
func actionProcessStdinReplace(changesets []changeset) int {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
newLine, _, skipLine := applyChangesetsToLine(line, changesets)
if !skipLine {
fmt.Println(newLine)
}
}
return 0
}
func actionProcessStdinTemplate(changesets []changeset) int {
var buffer bytes.Buffer
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buffer.WriteString(scanner.Text() + "\n")
}
content := parseContentAsTemplate(buffer.String(), changesets)
fmt.Print(content.String())
return 0
}
func actionProcessFiles(changesets []changeset, fileitems []fileitem) int {
if len(fileitems) == 0 {
if opts.IgnoreEmpty {
logMessage("No files found, requsted to ignore this")
os.Exit(0)
} else {
logFatalErrorAndExit(errors.New("No files specified"), 1)
}
}
swg := sizedwaitgroup.New(8)
results := make(chan changeresult, len(fileitems))
for _, file := range fileitems {
swg.Add()
go func(file fileitem, changesets []changeset) {
var (
err error = nil
output string = ""
status bool = true
)
if opts.ModeIsTemplate {
output, status, err = applyTemplateToFile(file, changesets)
} else {
output, status, err = applyChangesetsToFile(file, changesets)
}
results <- changeresult{file, output, status, err}
swg.Done()
}(file, changesets)
}
swg.Wait()
close(results)
errorCount := 0
for result := range results {
if result.Error != nil {
logError(result.Error)
errorCount++
} else if opts.Verbose {
title := fmt.Sprintf("%s:", result.File.Path)
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, title)
fmt.Fprintln(os.Stderr, strings.Repeat("-", len(title)))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, result.Output)
fmt.Fprintln(os.Stderr, "")
}
}
if errorCount >= 1 {
fmt.Fprintln(os.Stderr, fmt.Sprintf("[ERROR] %s failed with %d error(s)", argparser.Command.Name, errorCount))
return 1
}
return 0
}
func buildChangesets() []changeset {
var changesets []changeset
if !opts.ModeIsTemplate {
if len(opts.Search) == 0 || len(opts.Replace) == 0 {
logFatalErrorAndExit(errors.New("Missing either --search or --replace for this mode"), 1)
}
}
if len(opts.Search) != len(opts.Replace) {
logFatalErrorAndExit(errors.New("Unequal numbers of search or replace options"), 1)
}
for i := range opts.Search {
search := opts.Search[i]
replace := opts.Replace[i]
changeset := changeset{search, buildSearchTerm(search), replace, false}
changesets = append(changesets, changeset)
}
return changesets
}
func buildFileitems(args []string) []fileitem {
var (
fileitems []fileitem
file fileitem
)
for _, filepath := range args {
file = fileitem{filepath, filepath}
if opts.Output != "" {
file.Output = opts.Output
} else if opts.OutputStripFileExt != "" {
file.Output = strings.TrimSuffix(file.Output, opts.OutputStripFileExt)
} else if strings.Contains(filepath, ":") {
split := strings.SplitN(filepath, ":", 2)
file.Path = split[0]
file.Output = split[1]
}
fileitems = append(fileitems, file)
}
if opts.Path != "" {
searchFilesInPath(opts.Path, func(f os.FileInfo, filepath string) {
file := fileitem{filepath, filepath}
if opts.OutputStripFileExt != "" {
file.Output = strings.TrimSuffix(file.Output, opts.OutputStripFileExt)
}
fileitems = append(fileitems, file)
})
}
return fileitems
}
var argparser *flags.Parser
func main() {
argparser = flags.NewParser(&opts, flags.PassDoubleDash)
args, err := argparser.Parse()
handleSpecialCliOptions(args)
if err != nil {
logFatalErrorAndExit(err, 1)
}
changesets := buildChangesets()
fileitems := buildFileitems(args)
exitMode := 0
if opts.Stdin {
if opts.ModeIsTemplate {
exitMode = actionProcessStdinTemplate(changesets)
} else {
exitMode = actionProcessStdinReplace(changesets)
}
} else {
exitMode = actionProcessFiles(changesets, fileitems)
}
os.Exit(exitMode)
}