Skip to content

Commit f186dae

Browse files
committed
Add --output (in one file mode)
Writes output to another file
1 parent ba791ba commit f186dae

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

README.md

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,21 @@ Usage:
1717
goreplace
1818
1919
Application Options:
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)
20+
-m, --mode=[replace|line|lineinfile|template] 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:
21+
parse content as golang template, search value have to start uppercase (default: replace)
2922
-s, --search= search term
3023
-r, --replace= replacement term
31-
-i, --case-insensitive ignore case of pattern to match
32-
upper and lowercase characters
24+
-i, --case-insensitive ignore case of pattern to match upper and lowercase characters
3325
--stdin process stdin as input
34-
--once=[keep|unique] replace search term only one in
35-
a file, keep duplicaes (keep,
36-
default) or remove them (unique)
26+
-o, --output= write changes to this file (in one file mode)
27+
--once=[keep|unique] replace search term only one in a file, keep duplicaes (keep, default) or remove them (unique)
3728
--regex treat pattern as regex
38-
--regex-backrefs enable backreferences in
39-
replace term
29+
--regex-backrefs enable backreferences in replace term
4030
--regex-posix parse regex term as POSIX regex
4131
--path= use files in this path
42-
--path-pattern= file pattern (* for wildcard,
43-
only basename of file)
32+
--path-pattern= file pattern (* for wildcard, only basename of file)
4433
--path-regex= file pattern (regex, full path)
45-
--ignore-empty ignore empty file list,
46-
otherwise this will result in
47-
an error
34+
--ignore-empty ignore empty file list, otherwise this will result in an error
4835
-v, --verbose verbose mode
4936
--dry-run dry run mode
5037
-V, --version show version and exit
@@ -91,7 +78,7 @@ go-replace --mode=template daemon.conf
9178
## Installation
9279

9380
```bash
94-
GOREPLACE_VERSION=0.5.3 \
81+
GOREPLACE_VERSION=0.5.4 \
9582
&& wget -O /usr/local/bin/go-replace https://github.com/webdevops/goreplace/releases/download/$GOREPLACE_VERSION/gr-64-linux \
9683
&& chmod +x /usr/local/bin/go-replace
9784
```

goreplace.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
const (
1919
Author = "webdevops.io"
20-
Version = "0.5.2"
20+
Version = "0.5.4"
2121
)
2222

2323
type changeset struct {
@@ -53,6 +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+
Output string `short:"o" long:"output" description:"write changes to this file (in one file mode)"`
5657
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"`
5758
Regex bool ` long:"regex" description:"treat pattern as regex"`
5859
RegexBackref bool ` long:"regex-backrefs" description:"enable backreferences in replace term"`
@@ -223,7 +224,15 @@ func writeContentToFile(fileitem fileitem, content bytes.Buffer) (string, bool)
223224
return content.String(), true
224225
} else {
225226
var err error
226-
err = ioutil.WriteFile(fileitem.Path, content.Bytes(), 0)
227+
228+
filepath := fileitem.Path
229+
230+
// --output
231+
if (opts.Output != "") {
232+
filepath = opts.Output
233+
}
234+
235+
err = ioutil.WriteFile(filepath, content.Bytes(), 0644)
227236
if err != nil {
228237
panic(err)
229238
}
@@ -388,6 +397,11 @@ func handleSpecialCliOptions(args []string) ([]string) {
388397
})
389398
}
390399

400+
// --output
401+
if (opts.Output != "" && len(args) > 1) {
402+
logFatalErrorAndExit(errors.New("Only one file is allowed when using --output"), 1)
403+
}
404+
391405
return args
392406
}
393407

tests/main.test

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,40 @@ Testing template mode:
450450
> this is the third foobar line
451451
> this is the last line
452452
> EOF
453-
$ cat test.txt | goreplace --mode=template --stdin -s Foobar -r ___xxx test.txt
453+
$ cat test.txt | goreplace --mode=template --stdin -s Foobar -r ___xxx
454454
23<45
455455
___xxx
456456
this is a testline
457457
this is the second line
458458
this is the third foobar line
459459
this is the last line
460460

461+
Testing with output:
462+
463+
$ cat > test.txt <<EOF
464+
> this is a testline
465+
> this is the second line
466+
> this is the third foobar line
467+
> this is the last line
468+
> EOF
469+
$ goreplace -s foobar -r ___xxx test.txt --output test.output
470+
$ cat test.output
471+
this is a testline
472+
this is the second line
473+
this is the third ___xxx line
474+
this is the last line
475+
476+
Testing with output:
477+
478+
$ cat > test.txt <<EOF
479+
> this is a testline
480+
> this is the second line
481+
> this is the third foobar line
482+
> this is the last line
483+
> EOF
484+
$ cp test.txt test2.txt
485+
$ goreplace -s foobar -r ___xxx test.txt test2.txt --output test.output
486+
Error: Only one file is allowed when using --output
487+
488+
[1]
489+

0 commit comments

Comments
 (0)