Skip to content

Commit 22b756f

Browse files
committed
Implement source:destination as argument
1 parent c571ab6 commit 22b756f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Application Options:
4141
-h, --help show this help message
4242
```
4343

44+
Files must be specified as arguments and will be overwritten after parsing. If you want an alternative location for
45+
saving the file the argument can be specified as `source:destination`, eg.
46+
`go-replace -s foobar -r barfoo daemon.conf.tmpl:daemon.conf`.
47+
48+
If `--path` (with or without `--path-pattern` or `--path-regex`) the files inside path are used as source and will
49+
be overwritten. If `daemon.conf.tmpl` should be written as `daemon.conf` the option `--output-strip-ext=.tmpl` will do
50+
this based on the source file name.
51+
52+
4453
| Mode | Description |
4554
|:-----------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------|
4655
| replace | Replace search term inside one line with replacement. |
@@ -75,7 +84,7 @@ Process file with:
7584
```bash
7685
export SERVERNAME=www.foobar.example
7786
export DOCUMENTROOT=/var/www/foobar.example/
78-
go-replace --mode=template --output-strip-ext=.tmpl daemon.conf.tmpl
87+
go-replace --mode=template daemon.conf.tmpl:daemon.conf
7988
```
8089

8190
Reuslt file `daemon.conf`:

goreplace.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,17 @@ func buildFileitems(args []string) ([]fileitem) {
586586
file := fileitem{filepath, filepath}
587587

588588
if opts.Output != "" {
589+
// use specific output
589590
file.Output = opts.Output
590591
} else if opts.OutputStripFileExt != "" {
592+
// remove file ext from saving destination
591593
file.Output = strings.TrimSuffix(file.Output, opts.OutputStripFileExt)
594+
} else if strings.Contains(filepath, ":") {
595+
// argument like "source:destination"
596+
pair := strings.Split(filepath, ":")
597+
598+
file.Path = pair[0]
599+
file.Output = pair[1]
592600
}
593601

594602
fileitems = append(fileitems, file)

tests/main.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,18 @@ Testing with --output but multiple arguments:
507507

508508
[1]
509509

510+
Testing with source:dest:
511+
512+
$ cat > test.txt <<EOF
513+
> this is a testline
514+
> this is the second line
515+
> this is the third foobar line
516+
> this is the last line
517+
> EOF
518+
$ goreplace -s foobar -r ___xxx test.txt:test.output
519+
$ cat test.output
520+
this is a testline
521+
this is the second line
522+
this is the third ___xxx line
523+
this is the last line
524+

0 commit comments

Comments
 (0)