Created
November 7, 2022 18:09
-
-
Save luckman212/e2470df9c555995e5779d3acff4002d9 to your computer and use it in GitHub Desktop.
stream change events via syncthing REST API
This file contains 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
#!/usr/bin/env bash | |
# https://docs.syncthing.net/rest/events-get.html | |
# https://docs.syncthing.net/dev/events.html#event-types | |
APIKEY=$(defaults read com.github.xor-gate.syncthing-macosx ApiKey) | |
[[ -n "$APIKEY" ]] || { echo "error retreiving API key"; exit 1; } | |
ENDPOINT='http://localhost:8384/rest/events/disk' | |
INTERVAL=2 | |
_wait() { | |
sleep $INTERVAL | |
} | |
while true ; do | |
JSON=$(curl -s -H "X-API-Key: $APIKEY" "$ENDPOINT?timeout=$INTERVAL&since=$LAST" 2>/dev/null) | |
[[ -n "$JSON" ]] || { _wait; continue; } | |
LAST=$(jq -r '.[-1].id' <<<"$JSON" 2>/dev/null) | |
if [[ $LAST != "$LATEST" ]] && [[ $LAST != "null" ]]; then | |
LATEST=$LAST | |
jq -r '.[] | . as $r | .data | [ | |
($r.time | sub("\\.[0-9]{3,}.*$"; "") | sub("T"; " ")), | |
$r.id, | |
([ .modifiedBy, "("+($r.type | sub("ChangeDetected"; ""))+")", .action,.type ] | join(" ")), | |
.path | |
] | @tsv' <<<"$JSON" | |
fi | |
_wait | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment