Skip to content

Instantly share code, notes, and snippets.

@ashb
Created April 9, 2018 16:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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