Skip to content

Commit c16412d

Browse files
committed
Replace --once-remove-match with --once=unique
1 parent 94ff45a commit c16412d

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,34 @@ Usage:
1717
goreplace
1818
1919
Application Options:
20-
-m, --mode=[replace|line|lineinfile|template] replacement mode - replace: replace match with term; line: replace line with term; lineinfile: replace line with
21-
term or if not found append to term to file; template: parse content as golang template, search value have to
22-
start uppercase (default: replace)
20+
-m, --mode=[replace|line|lineinfile|template] replacement mode - replace:
21+
replace match with term; line:
22+
replace line with term;
23+
lineinfile: replace line with
24+
term or if not found append to
25+
term to file; template: parse
26+
content as golang template,
27+
search value have to start
28+
uppercase (default: replace)
2329
-s, --search= search term
2430
-r, --replace= replacement term
25-
-i, --case-insensitive ignore case of pattern to match upper and lowercase characters
31+
-i, --case-insensitive ignore case of pattern to match
32+
upper and lowercase characters
2633
--stdin process stdin as input
27-
--once replace search term only one in a file
28-
--once-remove-match replace search term only one in a file and also don't keep matching lines (for line and lineinfile mode)
34+
--once=[keep|unique] replace search term only one in
35+
a file, keep duplicaes (keep,
36+
default) or remove them (unique)
2937
--regex treat pattern as regex
30-
--regex-backrefs enable backreferences in replace term
38+
--regex-backrefs enable backreferences in
39+
replace term
3140
--regex-posix parse regex term as POSIX regex
3241
--path= use files in this path
33-
--path-pattern= file pattern (* for wildcard, only basename of file)
42+
--path-pattern= file pattern (* for wildcard,
43+
only basename of file)
3444
--path-regex= file pattern (regex, full path)
35-
--ignore-empty ignore empty file list, otherwise this will result in an error
45+
--ignore-empty ignore empty file list,
46+
otherwise this will result in
47+
an error
3648
-v, --verbose verbose mode
3749
--dry-run dry run mode
3850
-V, --version show version and exit

goreplace.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ var opts struct {
5353
Replace []string `short:"r" long:"replace" description:"replacement term" `
5454
CaseInsensitive bool `short:"i" long:"case-insensitive" description:"ignore case of pattern to match upper and lowercase characters"`
5555
Stdin bool ` long:"stdin" description:"process stdin as input"`
56-
Once bool ` long:"once" description:"replace search term only one in a file"`
57-
OnceRemoveMatch bool ` long:"once-remove-match" description:"replace search term only one in a file and also don't keep matching lines (for line and lineinfile mode)"`
56+
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"`
5857
Regex bool ` long:"regex" description:"treat pattern as regex"`
5958
RegexBackref bool ` long:"regex-backrefs" description:"enable backreferences in replace term"`
6059
RegexPosix bool ` long:"regex-posix" description:"parse regex term as POSIX regex"`
@@ -152,9 +151,9 @@ func applyChangesetsToLine(line string, changesets []changeset) (string, bool, b
152151
changeset := changesets[i]
153152

154153
// --once, only do changeset once if already applied to file
155-
if opts.Once && changeset.MatchFound {
156-
// --once-without-match, skip matching lines
157-
if opts.OnceRemoveMatch && searchMatch(line, changeset) {
154+
if opts.Once != "" && changeset.MatchFound {
155+
// --once=unique, skip matching lines
156+
if opts.Once == "unique" && searchMatch(line, changeset) {
158157
// matching line, not writing to buffer as requsted
159158
skipLine = true
160159
changed = true
@@ -345,7 +344,6 @@ func searchFilesInPath(path string, callback func(os.FileInfo, string)) {
345344
// --version
346345
// --path
347346
// --mode=...
348-
// --once-without-match
349347
func handleSpecialCliOptions(args []string) ([]string) {
350348
// --version
351349
if (opts.ShowVersion) {
@@ -390,12 +388,6 @@ func handleSpecialCliOptions(args []string) ([]string) {
390388
})
391389
}
392390

393-
// --once-without-match
394-
if opts.OnceRemoveMatch {
395-
// implicit enables once mode
396-
opts.Once = true
397-
}
398-
399391
return args
400392
}
401393

tests/main.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Testing line mode with multiple matches and --once:
202202
this is the foobar forth foobar line
203203
this is the last line
204204

205-
Testing line mode with multiple matches and --once-remove-match:
205+
Testing line mode with multiple matches and --once=unique:
206206

207207
$ cat > test.txt <<EOF
208208
> this is a testline
@@ -211,7 +211,7 @@ Testing line mode with multiple matches and --once-remove-match:
211211
> this is the foobar forth foobar line
212212
> this is the last line
213213
> EOF
214-
$ goreplace --mode=line -s foobar -r ___xxx --once-remove-match test.txt
214+
$ goreplace --mode=line -s foobar -r ___xxx --once=unique test.txt
215215
$ cat test.txt
216216
this is a testline
217217
this is the second line
@@ -270,7 +270,7 @@ Testing lineinfile mode with multiple matches and --once:
270270
this is the foobar forth foobar line
271271
this is the last line
272272

273-
Testing lineinfile mode with multiple matches and --once-remove-match:
273+
Testing lineinfile mode with multiple matches and --once=unique:
274274

275275
$ cat > test.txt <<EOF
276276
> this is a testline
@@ -279,7 +279,7 @@ Testing lineinfile mode with multiple matches and --once-remove-match:
279279
> this is the foobar forth foobar line
280280
> this is the last line
281281
> EOF
282-
$ goreplace --mode=lineinfile -s foobar -r ___xxx --once-remove-match test.txt
282+
$ goreplace --mode=lineinfile -s foobar -r ___xxx --once=unique test.txt
283283
$ cat test.txt
284284
this is a testline
285285
this is the second line
@@ -295,7 +295,7 @@ Testing lineinfile mode without match:
295295
> this is the foobar forth foobar line
296296
> this is the last line
297297
> EOF
298-
$ goreplace --mode=lineinfile -s barfoo -r ___xxx --once-remove-match test.txt
298+
$ goreplace --mode=lineinfile -s barfoo -r ___xxx --once=unique test.txt
299299
$ cat test.txt
300300
this is a testline
301301
this is the second line

0 commit comments

Comments
 (0)