dotfiles/private_dot_local/bin/executable_clone-all-gitea-...

36 lines
1.2 KiB
Bash

#!/usr/bin/env bash
GITEA_HOST=${GITEA_HOST:-$1}
if [ -z "$GITEA_HOST" ] || [ -z "$GITEA_ACCESS_TOKEN" ] ; then
echo "Clones all repos that you have access to into the current directory."
echo "You must set \$GITEA_ACCESS_TOKEN to a value from the gitea interface."
echo "Usage: $0 <gitea_host>"
exit 1
fi
mkdir -p archived
for i in $(curl -s --header "Authorization: token $GITEA_ACCESS_TOKEN" "${GITEA_HOST}/api/v1/user/repos?limit=50" | jq -r 'map(.name + "," + .ssh_url + "," + (.archived | tostring)) | flatten[]') ; do
IFS=',' read -ra fields <<< "$i"
if [ "${fields[2]}" == "true" ] ; then
if [ -d "${fields[0]}" ] ; then
echo "Moving ${fields[0]} into archived/${fields[0]}..."
mv "${fields[0]}" archived/
else
if [ -d "archived/${fields[0]}" ] ; then
echo "Archived repo ${fields[0]} already exists, skipping."
else
echo "Cloning archived/${fields[0]} from ${fields[1]}"
git clone -q "${fields[1]}" "archived/${fields[0]}"
fi
fi
else
if [ -d "${fields[0]}" ] ; then
echo "Repo ${fields[0]} already exists, skipping."
else
echo "Cloning ${fields[0]} from ${fields[1]}"
git clone -q "${fields[1]}"
fi
fi
done