Skip to content

Instantly share code, notes, and snippets.

@luckman212
Last active February 17, 2024 16:34
Show Gist options
  • Save luckman212/095fdb8544eef1aad8bda5d8d4dbc8d0 to your computer and use it in GitHub Desktop.
Save luckman212/095fdb8544eef1aad8bda5d8d4dbc8d0 to your computer and use it in GitHub Desktop.
json-diff using jd, for diffing Syncthing file debug outputs: https://docs.syncthing.net/rest/db-file-get.html
#!/usr/bin/env bash
case $1 in
-h|--help|'') echo "${0##*/} [files-or-dir]"; exit;;
esac
#requires jd, jq
for f in jd jq; do
if ! hash $f &>/dev/null; then brew install --quiet $f; fi
done
declare -A FARR
c=0
if [[ -d $1 ]]; then
FPATH=$(realpath "$1")
while IFS= read -r FNAME; do
FARR[$c]="$FNAME"
(( c++ ))
done < <(find -s "$FPATH" -type f -name "*.json")
else
for FNAME in "$@"; do
FARR[$c]="$FNAME"
(( c++ ))
done
fi
ARRSIZE=${#FARR[@]}
if (( ARRSIZE == 0 )); then
echo 1>&2 "no files to process"
exit
fi
for (( n=1; n<ARRSIZE; n++ )); do
(( p = n - 1 ))
clear
echo "${FG}comparing ${FARR[$p]} <==> ${FARR[$n]}${FN}"
jd -f jd -color "${FARR[$p]}" "${FARR[$n]}"
echo
echo "${FL}.global.platform.darwin ONLY diff:${FN}"
jq -r <"${FARR[$p]}" '.global.platform.darwin' >/tmp/p
jq -r <"${FARR[$n]}" '.global.platform.darwin' >/tmp/n
if jd -f jd -color /tmp/p /tmp/n; then
echo "${FG}** no diff **${FN}"
fi
echo
left=$(( ARRSIZE - n - 1 ))
(( left > 0 )) && read -r -p "press <ENTER> to continue or ⌃c to ABORT ($left left)"
done
rm /tmp/p /tmp/n 2>/dev/null
@luckman212
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment