Skip to content

Commit

Permalink
Add --output (in one file mode)
Browse files Browse the repository at this point in the history
Writes output to another file
  • Loading branch information
mblaschke committed Apr 9, 2017
1 parent ba791ba commit f186dae
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
31 changes: 9 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,21 @@ Usage:
goreplace
Application Options:
-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: parse
content as golang template,
search value have to start
uppercase (default: replace)
-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:
parse content as golang template, search value have to start uppercase (default: replace)
-s, --search= search term
-r, --replace= replacement term
-i, --case-insensitive ignore case of pattern to match
upper and lowercase characters
-i, --case-insensitive ignore case of pattern to match upper and lowercase characters
--stdin process stdin as input
--once=[keep|unique] replace search term only one in
a file, keep duplicaes (keep,
default) or remove them (unique)
-o, --output= write changes to this file (in one file mode)
--once=[keep|unique] replace search term only one in a file, keep duplicaes (keep, default) or remove them (unique)
--regex treat pattern as regex
--regex-backrefs enable backreferences in
replace term
--regex-backrefs enable backreferences in replace term
--regex-posix parse regex term as POSIX regex
--path= use files in this path
--path-pattern= file pattern (* for wildcard,
only basename of file)
--path-pattern= file pattern (* for wildcard, only basename of file)
--path-regex= file pattern (regex, full path)
--ignore-empty ignore empty file list,
otherwise this will result in
an error
--ignore-empty ignore empty file list, otherwise this will result in an error
-v, --verbose verbose mode
--dry-run dry run mode
-V, --version show version and exit
Expand Down Expand Up @@ -91,7 +78,7 @@ go-replace --mode=template daemon.conf
## Installation

```bash
GOREPLACE_VERSION=0.5.3 \
GOREPLACE_VERSION=0.5.4 \
&& wget -O /usr/local/bin/go-replace https://github.com/webdevops/goreplace/releases/download/$GOREPLACE_VERSION/gr-64-linux \
&& chmod +x /usr/local/bin/go-replace
```
Expand Down
18 changes: 16 additions & 2 deletions goreplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
Author = "webdevops.io"
Version = "0.5.2"
Version = "0.5.4"
)

type changeset struct {
Expand Down Expand Up @@ -53,6 +53,7 @@ var opts struct {
Replace []string `short:"r" long:"replace" description:"replacement term" `
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)"`
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"`
Expand Down Expand Up @@ -223,7 +224,15 @@ func writeContentToFile(fileitem fileitem, content bytes.Buffer) (string, bool)
return content.String(), true
} else {
var err error
err = ioutil.WriteFile(fileitem.Path, content.Bytes(), 0)

filepath := fileitem.Path

// --output
if (opts.Output != "") {
filepath = opts.Output
}

err = ioutil.WriteFile(filepath, content.Bytes(), 0644)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -388,6 +397,11 @@ func handleSpecialCliOptions(args []string) ([]string) {
})
}

// --output
if (opts.Output != "" && len(args) > 1) {
logFatalErrorAndExit(errors.New("Only one file is allowed when using --output"), 1)
}

return args
}

Expand Down
31 changes: 30 additions & 1 deletion tests/main.test
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,40 @@ Testing template mode:
> this is the third foobar line
> this is the last line
> EOF
$ cat test.txt | goreplace --mode=template --stdin -s Foobar -r ___xxx test.txt
$ cat test.txt | goreplace --mode=template --stdin -s Foobar -r ___xxx
23<45
___xxx
this is a testline
this is the second line
this is the third foobar line
this is the last line

Testing with output:

$ cat > test.txt <<EOF
> this is a testline
> this is the second line
> this is the third foobar line
> this is the last line
> EOF
$ goreplace -s foobar -r ___xxx test.txt --output test.output
$ cat test.output
this is a testline
this is the second line
this is the third ___xxx line
this is the last line

Testing with output:

$ cat > test.txt <<EOF
> this is a testline
> this is the second line
> this is the third foobar line
> this is the last line
> EOF
$ cp test.txt test2.txt
$ goreplace -s foobar -r ___xxx test.txt test2.txt --output test.output
Error: Only one file is allowed when using --output

[1]

0 comments on commit f186dae

Please sign in to comment.