#!/usr/bin/env bash # Bulk-clone State Hub registered repos onto railiance01 under /home/tegwick/. # Usage: railiance01-bulk-clone-repos.sh [--dry-run] [--slug SLUG] set -euo pipefail API_BASE="${API_BASE:-http://127.0.0.1:8000}" CLONE_ROOT="${CLONE_ROOT:-/home/tegwick}" DRY_RUN=0 if [[ "${1:-}" == "--dry-run" ]]; then DRY_RUN=1 shift fi SLUG_FILTER="${1:-}" clone_url_for() { local slug="$1" remote="$2" case "${remote}" in forgejo-remote:*|gitea-remote:*) echo "${remote}" ;; ssh://git@92.205.130.254:30022/coulomb/*) echo "gitea-remote:coulomb/${slug}.git" ;; http://92.205.130.254:32166/coulomb/*) echo "gitea-remote:coulomb/${slug}.git" ;; http://gitea.local/worsch/*) echo "gitea-remote:coulomb/${slug}.git" ;; "") echo "gitea-remote:coulomb/${slug}.git" ;; *) echo "gitea-remote:coulomb/${slug}.git" ;; esac } repos_json=$(curl -fsS "${API_BASE}/repos/") mapfile -t slugs < <(python3 -c " import json, sys repos = json.load(sys.stdin) filt = sys.argv[1] if len(sys.argv) > 1 else '' for r in sorted(repos, key=lambda x: x['slug']): if filt and r['slug'] != filt: continue print(r['slug']) " <<<"${repos_json}" "${SLUG_FILTER}") cloned=0 skipped=0 failed=0 for slug in "${slugs[@]}"; do dest="${CLONE_ROOT}/${slug}" if [[ -d "${dest}/.git" ]]; then echo "SKIP ${slug} (exists)" skipped=$((skipped + 1)) continue fi remote=$(python3 -c " import json, sys repos = json.load(sys.stdin) for r in repos: if r['slug'] == sys.argv[1]: print(r.get('remote_url') or '') break " <<<"${repos_json}" "${slug}") url=$(clone_url_for "${slug}" "${remote}") if [[ "${DRY_RUN}" -eq 1 ]]; then echo "DRY ${slug} <- ${url}" continue fi echo "CLONE ${slug} <- ${url}" if git clone "${url}" "${dest}"; then cloned=$((cloned + 1)) else echo "FAIL ${slug}" >&2 failed=$((failed + 1)) fi done echo "done: cloned=${cloned} skipped=${skipped} failed=${failed}"