Skip to content

Instantly share code, notes, and snippets.

@ashb
Created April 9, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashb/a6425709bb8a32ed3f2720aea0b445d8 to your computer and use it in GitHub Desktop.
Save ashb/a6425709bb8a32ed3f2720aea0b445d8 to your computer and use it in GitHub Desktop.
Show a diff of single line JSON objects (IAM policy docs) in terraform
#!/bin/bash
#
# shellcheck disable=SC2001
set -e -o pipefail
input="$1"
[[ -n "$input" ]] || {
[[ -t 0 ]] && { input="$(pbpaste)"; } || input="$(cat)"
}
# TODO: Deal with multiple lines in the input.
pattern='^(?: *[a-z0-9_]+: *)?(".*") => (".*")$'
before=$(echo "$input" | perl -p -e 'BEGIN { $/="" }; s/'"$pattern"'/$1/sm;' | jq -eS 'fromjson')
after=$(echo "$input" | perl -p -e 'BEGIN { $/="" }; s/'"$pattern"'/$2/sm;' | jq -eS 'fromjson')
diff -u <(echo "$before") <(echo "$after")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment