Last active
June 22, 2021 08:57
-
-
Save bmiro/0b785f9d556bd1a305762d4523522b95 to your computer and use it in GitHub Desktop.
JSON diff tool for cli (jsondiff) using `jq` with able to show it on vim with `--vim`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIFF_PARAMS="-aur --color=auto" | |
# Precondition check | |
which jq > /dev/null || (echo "jq is required" && exit 1) | |
# Arg check | |
for arg in "$@"; do | |
shift | |
case "$arg" in | |
"--vim") | |
vim="true" | |
;; | |
*) | |
set -- "$@" "$arg" | |
esac | |
done | |
json1="$1" | |
json2="$2" | |
stat "$json1" > /dev/null || exit $? | |
stat "$json2" > /dev/null || exit $? | |
# Main | |
sorted1=$(mktemp --suffix=.jsondiff) | |
sorted2=$(mktemp --suffix=.jsondiff) | |
jq -S . "$json1" > "$sorted1" | |
jq -S . "$json2" > "$sorted2" | |
[ "$vim" ] && cmd=vimdiff || cmd="diff $DIFF_PARAMS" | |
$cmd "$sorted1" "$sorted2" | |
# Tear down | |
rm "$sorted1" | |
rm "$sorted2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
Preconditions
JSONdiff
Usage
Simple usage
Display on vimdiff (require
apt install vim
)