diff --git a/README.md b/README.md index 53f9d69..baec92d 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,12 @@ $ pounce -s '' # TODO -- [ ] apply tests. - [ ] deal with colons in file names. -- [ ] deal with no EOL last line. -- [ ] apply: only generate backup files if content actually changed. -- [ ] apply: read/write piece wise. +- [ ] deal with no EOL on last line of files. - [ ] `\n` vs `\r\n`? -- [ ] example in README could be better. -- [ ] cli flags can be better phrased. +- [ ] apply: only generate backup files if content actually changed? +- [ ] apply: read/write piece wise. +- [ ] cli flags docs can be better phrased. # Disclaimer diff --git a/common.go b/common.go index b8efa3f..bcf2bb9 100644 --- a/common.go +++ b/common.go @@ -19,9 +19,16 @@ func trimPath(p string) string { var commonConfig struct { print, nowd, collect, apply bool + dir string } var commonFlags = []cli.Flag{ + &cli.StringFlag{ + Name: "dir", + Aliases: []string{"d"}, + Destination: &commonConfig.dir, + Usage: "set working directory", + }, &cli.BoolFlag{ Name: "print", Aliases: []string{"p"}, diff --git a/pounce.go b/pounce.go index 78d21b9..d895170 100644 --- a/pounce.go +++ b/pounce.go @@ -17,6 +17,12 @@ var app = cli.App{ Version: version, Flags: append(append(commonFlags, collectFlags...), applyFlags...), Action: func(c *cli.Context) error { + if dir := commonConfig.dir; dir != "" { + if err := os.Chdir(dir); err != nil { + return fmt.Errorf("chdir: %w", err) + } + } + args := c.Args().Slice() if commonConfig.collect && commonConfig.apply || @@ -68,7 +74,7 @@ func both(werr io.Writer, args []string) error { cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - if err := err.(*exec.ExitError); err != nil { + if err, ok := err.(*exec.ExitError); ok && err != nil { return fmt.Errorf("editor exited with code %d", err.ExitCode()) } diff --git a/tests/apply.clitest b/tests/apply.clitest deleted file mode 100644 index 6a22c72..0000000 --- a/tests/apply.clitest +++ /dev/null @@ -1 +0,0 @@ -$ cd "${TESTROOT}/fixtures" diff --git a/tests/both.clitest b/tests/both.clitest new file mode 100644 index 0000000..16853aa --- /dev/null +++ b/tests/both.clitest @@ -0,0 +1,85 @@ +$ dir0=$(mktemp -d) +$ trap "rm -fR ${dir0}" 0 +$ cd "${dir0}" +$ cp -r "${TESTROOT}/fixtures" . +$ ${POUNCE} -d fixtures -c --nowd -rs '' > before +$ cat before | sed -e 's/dogs/cats/g' > after +$ ${POUNCE} -d fixtures -a < after +$ find fixtures -type f -not -name binary -exec cat {} \; +I like cats. +Cats are the best. +meow +pigs are the best! +fish are the best! +cats are the best! +cats are the best! +meow +$ +$ # with backup files +$ dir1=$(mktemp -d) +$ trap "rm -fR ${dir1}" 0 +$ cd "${dir1}" +$ cp -r "${TESTROOT}/fixtures" . +$ ${POUNCE} -d fixtures -a --bak _bak < "${dir0}/after" +$ find . -type f -print +./fixtures/README.md_bak +./fixtures/README.md +./fixtures/some/dir/animals.txt +./fixtures/some/dir/animals.txt_bak +./fixtures/some/binary +$ cat ./fixtures/README.md_bak +I like dogs. +Cats are the best. +meow +$ cat ./fixtures/README.md +I like cats. +Cats are the best. +meow +$ +$ # with editor +$ dir2=$(mktemp -d) +$ trap "rm -fR ${dir2}" 0 +$ cd "${dir2}" +$ cp -r "${TESTROOT}/fixtures" . +$ EDITOR=cat ${POUNCE} --nowd -rs 'meow' +fixtures/README.md:3:meow +fixtures/some/dir/animals.txt:5:meow +$ echo "#!/bin/sh" > ./mod.sh +$ echo "exec sed -i '' -e 's/dog/cat/g' \$1" >> ./mod.sh +$ chmod +x ./mod.sh +$ EDITOR=./mod.sh ${POUNCE} --nowd -rs 'dog' +$ find . -type f -not -name binary -and -not -name mod.sh -exec cat {} \; +I like cats. +Cats are the best. +meow +pigs are the best! +fish are the best! +cats are the best! +cats are the best! +meow +$ +$ # wd +$ dir3=$(mktemp -d) +$ trap "rm -fR ${dir3}" 0 +$ cd "${dir3}" +$ cp -r "${TESTROOT}/fixtures" . +$ ${POUNCE} -d fixtures -c -rs '' > before +$ cat before | sed -e 's/dogs/cats/g' > after +$ ${POUNCE} -a < after #=> --exit 1 +$ ${POUNCE} -d fixtures -a < after +$ +$ # abs +$ dir4=$(mktemp -d) +$ trap "rm -fR ${dir4}" 0 +$ cd "${dir4}" +$ cp -r "${TESTROOT}/fixtures" . +$ ${POUNCE} --abs -d fixtures -c -rs '' | sed -e 's/dog/cat/g' | ${POUNCE} -d fixtures -a +$ find fixtures -type f -not -name binary -and -not -name mod.sh -exec cat {} \; +I like cats. +Cats are the best. +meow +pigs are the best! +fish are the best! +cats are the best! +cats are the best! +meow