diff --git a/syncthing.sh b/syncthing.sh index bb60814..09a5b45 100755 --- a/syncthing.sh +++ b/syncthing.sh @@ -84,6 +84,63 @@ for device_id, info in devices.items(): fi } +show_pending_folders() { + header "Pending Folders" + local pending + pending=$(st_get /rest/cluster/pending/folders) + local count + count=$(echo "$pending" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(len(d))') + if [ "$count" -eq 0 ]; then + warn "No pending folders." + return + fi + echo "$pending" | python3 -c ' +import sys,json +d=json.load(sys.stdin) +for fid,info in d.items(): + offered_by = list(info.get("offeredBy",{}).keys()) + print(f" Folder ID: {fid}") + print(f" Offered by: {offered_by}") + print("") +' + read -rp "Accept a pending folder? [y/N]: " ACCEPT + ACCEPT="${ACCEPT:-n}" + if [[ "${ACCEPT,,}" != "y" ]]; then return; fi + + local folder_ids + folder_ids=$(echo "$pending" | python3 -c 'import sys,json; [print(k) for k in json.load(sys.stdin)]') + local FOLDER_ID + if [ "$(echo "$folder_ids" | wc -l)" -eq 1 ]; then + FOLDER_ID="$folder_ids" + else + echo "Available pending folders:" + local i=1 + while IFS= read -r fid; do + echo " $i) $fid" + i=$((i+1)) + done <<< "$folder_ids" + read -rp "Choose folder number: " FOLDER_NUM + FOLDER_ID=$(echo "$folder_ids" | sed -n "${FOLDER_NUM}p") + fi + [ -n "$FOLDER_ID" ] || { warn "No folder selected."; return; } + + echo "" + info "Available directories in /var/syncthing/data/:" + ls /var/syncthing/data/ 2>/dev/null | sed 's/^/ /' || true + echo "" + read -rp "Folder path on this device: " FOLDER_PATH + [ -n "$FOLDER_PATH" ] || { warn "Path cannot be empty."; return; } + + read -rp "Folder label [$FOLDER_ID]: " FOLDER_LABEL + FOLDER_LABEL="${FOLDER_LABEL:-$FOLDER_ID}" + + st_post /rest/config/folders "$(python3 -c " +import json +print(json.dumps({'id': '$FOLDER_ID', 'label': '$FOLDER_LABEL', 'path': '$FOLDER_PATH'})) +")" + info "Folder '$FOLDER_LABEL' added at $FOLDER_PATH." +} + add_device_by_pending() { local pending="$1" local ids @@ -436,7 +493,7 @@ get_apikey while true; do header "Syncthing Manager" echo " 0) Show This Device's ID" - echo " 1) Pending Devices" + echo " 1) Pending Devices & Folders" echo " 2) List Devices" echo " 3) Add Device" echo " 4) Remove Device" @@ -451,7 +508,7 @@ while true; do echo "" case "$OPT" in 0) show_own_device_id ;; - 1) show_pending_devices ;; + 1) show_pending_devices; show_pending_folders ;; 2) list_devices ;; 3) add_device ;; 4) remove_device ;;