diff --git a/hub/onboard-spoke.sh b/hub/onboard-spoke.sh index 93268ec..7c78231 100755 --- a/hub/onboard-spoke.sh +++ b/hub/onboard-spoke.sh @@ -163,12 +163,33 @@ if [[ "${ADD_UNION,,}" == "y" ]]; then 3) UPSTREAM_TAG=":writeback" ;; *) warn "Invalid choice, defaulting to full read/write."; UPSTREAM_TAG="" ;; esac - UPSTREAM="${SPOKE_NAME}-remote:${UNION_PATH}${UPSTREAM_TAG}" + if [ -n "$UNION_PATH" ]; then + UPSTREAM="${SPOKE_NAME}-remote:${UNION_PATH}${UPSTREAM_TAG}" + else + UPSTREAM="${SPOKE_NAME}-remote:${UPSTREAM_TAG}" + fi if grep -q "^\[${UNION_NAME}\]" "$RCLONE_CONF" 2>/dev/null; then if grep -A5 "^\[${UNION_NAME}\]" "$RCLONE_CONF" | grep -qF "${SPOKE_NAME}-remote:"; then warn "Upstream for ${SPOKE_NAME}-remote already in union remote [${UNION_NAME}], skipping." else - sed -i "/^\[${UNION_NAME}\]/,/^\[/{s|^upstreams = \(.*\)|upstreams = \1 ${UPSTREAM}|}" "$RCLONE_CONF" + python3 - "$RCLONE_CONF" "$UNION_NAME" "$UPSTREAM" <<'PYEOF' +import sys +path, section, upstream = sys.argv[1], sys.argv[2], sys.argv[3] +with open(path) as f: + lines = f.readlines() +out = [] +in_section = False +for line in lines: + if line.strip() == f"[{section}]": + in_section = True + elif line.strip().startswith("["): + in_section = False + if in_section and line.startswith("upstreams ="): + line = line.rstrip() + " " + upstream + "\n" + out.append(line) +with open(path, "w") as f: + f.writelines(out) +PYEOF info "Added '$UPSTREAM' to union remote [${UNION_NAME}]." fi else