myitcv / x

Mono-repo for all myitcv.io/... Go code
MIT License
103 stars 20 forks source link

cmd/egrunner: inject set -e in subcommands and command substitutions #83

Open myitcv opened 5 years ago

myitcv commented 5 years ago

To ensure any failures bubble up to the top-level script.

Also, for process substitutions:

cat <(echo "hello"; false; echo "nothing") <(echo "goodbye"; false; echo "really")

will need to be rewritten to something like:

pushd $(mktemp -d) > /dev/null
mkfifo f1
(set -eu; set -o pipefail; echo "hello"; $comm; echo "nothing") > f1 2>&1 &
echo $! >> pids
mkfifo f2
(set -eu; set -o pipefail; echo "goodbye"; $comm; echo "really") > f2 2>&1 &
echo $! >> pids
cat f1 f2
for pid in $(<pids)
do
    wait $pid || echo $pid >> failed
done
if [ -e failed ]
then
    echo "command failed"
    rm -rf $PWD
    popd > /dev/null
    false
else
    rm -rf $PWD
    popd > /dev/null
fi

which gives:

$ pushd $(mktemp -d) >/dev/null
$ mkfifo f1
$ (
        set -eu
        set -o pipefail
        echo "hello"
        false
        echo "nothing"
) >f1 2>&1 &
$ echo $! >>pids
$ mkfifo f2
$ (
        set -eu
        set -o pipefail
        echo "goodbye"
        false
        echo "really"
) >f2 2>&1 &
$ echo $! >>pids
$ cat f1 f2
hello
goodbye
$ for pid in $(<pids); do
        wait $pid || echo $pid >>failed
done
$ if [ -e failed ]; then
        echo "command failed"
        rm -rf $PWD
        popd >/dev/null
        false
else
        rm -rf $PWD
        popd >/dev/null
fi
command failed