From 1d4e25b6a5bdd76ee5e396944893862d458cbe38 Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Sat, 18 Apr 2026 21:21:24 -0700 Subject: [PATCH] syncthing.sh: auto-unshare folder from all devices before removal if shared --- syncthing.sh | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/syncthing.sh b/syncthing.sh index b877209..b64c8f0 100755 --- a/syncthing.sh +++ b/syncthing.sh @@ -268,8 +268,35 @@ for i, f in enumerate(json.load(sys.stdin), 1): folder_id=$(echo "$folders" | python3 -c "import sys,json; f=json.load(sys.stdin)[$((CHOICE-1))]; print(f['id'])") folder_label=$(echo "$folders" | python3 -c "import sys,json; f=json.load(sys.stdin)[$((CHOICE-1))]; print(f['label'] or f['id'])") - read -rp "Remove folder '${folder_label}'? [y/N]: " CONFIRM - [[ "${CONFIRM,,}" == "y" ]] || { info "Aborted."; return; } + local folder_config shared_devices + folder_config=$(st_get "/rest/config/folders/${folder_id}") + shared_devices=$(echo "$folder_config" | python3 -c "import sys,json; [print(d['deviceID']) for d in json.load(sys.stdin).get('devices',[])]") + + if [ -n "$shared_devices" ]; then + local devices + devices=$(st_get /rest/config/devices) + warn "Folder '${folder_label}' is currently shared with:" + while IFS= read -r did; do + local dname + dname=$(echo "$devices" | python3 -c "import sys,json; devs=json.load(sys.stdin); match=[d['name'] for d in devs if d['deviceID']==sys.argv[1]]; print(match[0] if match else sys.argv[1])" "$did") + echo " - $dname" + done <<< "$shared_devices" + warn "It will be unshared from all devices before removal." + read -rp "Proceed? [y/N]: " CONFIRM + [[ "${CONFIRM,,}" == "y" ]] || { info "Aborted."; return; } + local updated + updated=$(echo "$folder_config" | python3 -c " +import sys, json +f = json.load(sys.stdin) +f['devices'] = [] +print(json.dumps(f)) +") + st_put "/rest/config/folders/${folder_id}" "$updated" >/dev/null + info "Folder '${folder_label}' unshared from all devices." + else + read -rp "Remove folder '${folder_label}'? [y/N]: " CONFIRM + [[ "${CONFIRM,,}" == "y" ]] || { info "Aborted."; return; } + fi st_delete "/rest/config/folders/${folder_id}" >/dev/null info "Folder '${folder_label}' removed."