Skip to content

Instantly share code, notes, and snippets.

@norm
Created May 4, 2018 14:04
Show Gist options
  • Save norm/24cf10eb77734df32b7f66fe5d56b396 to your computer and use it in GitHub Desktop.
Save norm/24cf10eb77734df32b7f66fe5d56b396 to your computer and use it in GitHub Desktop.
tf — terraform wrapper script
#!/bin/bash
set -e -o pipefail
case "$1" in
plan)
terraform plan -out planfile
;;
apply)
if [ ! -f planfile ]; then
echo "No plan to apply -- run 'tf plan' first."
exit 1
fi
terraform apply planfile \
&& rm planfile
;;
diff)
input="$2"
if [ -z "$input" ]; then
if [ -t 0 ]; then
input="$(pbpaste)"
else
input="$(cat)"
fi
fi
IFS=$'\n'
for line in "$input"; do
pattern='^(?: *[a-z0-9_]+: *)?(".*") => (".*")$'
before=$(
echo "$line" \
| perl -pe 'BEGIN { $/="" }; s{'"$pattern"'}{$1}sm;' \
| jq -eS 'fromjson'
)
after=$(
echo "$line" \
| perl -pe 'BEGIN { $/="" }; s{'"$pattern"'}{$2}sm;' \
| jq -eS 'fromjson'
)
diff -u <(echo "$before") <(echo "$after")
done
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment