@@ -22,35 +22,17 @@ var opts struct {
22
22
SearchRegex * regexp.Regexp
23
23
Replace string `short:"r" long:"replace" required:"true" description:"replacement term" `
24
24
IgnoreCase bool `short:"i" long:"ignore-case" description:"ignore pattern case"`
25
- WholeLine bool ` long:"whole -line" description:"replace whole line"`
25
+ ReplaceLine bool ` long:"replace -line" description:"replace whole line instead of only match "`
26
26
Regex bool ` long:"regex" description:"treat pattern as regex"`
27
+ RegexBackref bool ` long:"regex-backrefs" description:"enable backreferences in replace term"`
27
28
Verbose bool `short:"v" long:"verbose" description:"verbose mode"`
28
29
DryRun bool ` long:"dry-run" description:"dry run mode"`
29
30
ShowVersion bool `short:"V" long:"version" description:"show version and exit"`
30
31
ShowHelp bool `short:"h" long:"help" description:"show this help message"`
31
32
}
32
33
33
- // Replace content parts in file
34
- func replaceContentInFile (filepath string ) {
35
- var err error
36
-
37
- read , err := ioutil .ReadFile (filepath )
38
- if err != nil {
39
- panic (err )
40
- }
41
-
42
- content , replaceStatus := replaceText (string (read ))
43
-
44
- if replaceStatus {
45
- writeContentToFile (filepath , content )
46
- } else {
47
- logMessage (fmt .Sprintf ("%s no match" , filepath ))
48
- }
49
-
50
- }
51
-
52
34
// Replace line (if match is found) in file
53
- func replaceLineInFile (filepath string ) {
35
+ func replaceInFile (filepath string ) {
54
36
file , err := os .Open (filepath )
55
37
if err != nil {
56
38
panic (err )
@@ -63,7 +45,13 @@ func replaceLineInFile(filepath string) {
63
45
line , e := Readln (r )
64
46
for e == nil {
65
47
if searchMatch (line ) {
66
- buffer .WriteString (opts .Replace + "\n " )
48
+ if opts .ReplaceLine {
49
+ line = opts .Replace
50
+ } else {
51
+ line = replaceText (line )
52
+ }
53
+
54
+ buffer .WriteString (line + "\n " )
67
55
replaceStatus = true
68
56
} else {
69
57
buffer .WriteString (line + "\n " )
@@ -73,7 +61,7 @@ func replaceLineInFile(filepath string) {
73
61
}
74
62
75
63
if replaceStatus {
76
- writeContentToFile (filepath , buffer . String () )
64
+ writeContentToFile (filepath , buffer )
77
65
} else {
78
66
logMessage (fmt .Sprintf ("%s no match" , filepath ))
79
67
}
@@ -106,30 +94,27 @@ func searchMatch(content string) (bool) {
106
94
}
107
95
108
96
// Replace text in whole content based on search options
109
- func replaceText (content string ) (string , bool ) {
110
- status := false
111
-
112
- if searchMatch (content ) {
113
- status = true
114
- content = opts .SearchRegex .ReplaceAllString (content , opts .Replace )
97
+ func replaceText (content string ) (string ) {
98
+ if opts .RegexBackref {
99
+ return opts .SearchRegex .ReplaceAllString (content , opts .Replace )
100
+ } else {
101
+ return opts .SearchRegex .ReplaceAllLiteralString (content , opts .Replace )
115
102
}
116
-
117
- return content , status
118
103
}
119
104
120
105
// Write content to file
121
- func writeContentToFile (filepath string , content string ) {
106
+ func writeContentToFile (filepath string , content bytes. Buffer ) {
122
107
if opts .DryRun {
123
108
title := fmt .Sprintf ("%s:" , filepath )
124
109
125
110
fmt .Println (title )
126
111
fmt .Println (strings .Repeat ("-" , len (title )))
127
- fmt .Println (content )
112
+ fmt .Println (content . String () )
128
113
fmt .Println ()
129
114
fmt .Println ()
130
115
} else {
131
116
var err error
132
- err = ioutil .WriteFile (filepath , [] byte ( content ), 0 )
117
+ err = ioutil .WriteFile (filepath , content . Bytes ( ), 0 )
133
118
if err != nil {
134
119
panic (err )
135
120
}
@@ -150,9 +135,9 @@ func logError(err error) {
150
135
fmt .Printf ("Error: %s\n " , err )
151
136
}
152
137
153
- // Process search term
138
+ // Build search term
154
139
// Compiles regexp if regexp is used
155
- func processSearchTerm () {
140
+ func buildSearchTerm () {
156
141
var regex string
157
142
158
143
if opts .Regex {
@@ -208,17 +193,13 @@ func main() {
208
193
209
194
handleSpecialOptions (argparser , args )
210
195
211
- processSearchTerm ()
196
+ buildSearchTerm ()
212
197
213
198
for i := range args {
214
199
var file string
215
200
file = args [i ]
216
201
217
- if opts .WholeLine {
218
- replaceLineInFile (file )
219
- } else {
220
- replaceContentInFile (file )
221
- }
202
+ replaceInFile (file )
222
203
}
223
204
224
205
os .Exit (0 )
0 commit comments