Skip to content

Instantly share code, notes, and snippets.

@nakamuraos
Last active February 9, 2022 01:20
Show Gist options
  • Save nakamuraos/ac316d55f019c5b3407398fe467f2b18 to your computer and use it in GitHub Desktop.
Save nakamuraos/ac316d55f019c5b3407398fe467f2b18 to your computer and use it in GitHub Desktop.
Clone all repository on GitHub
#!/bin/bash
# Author: NakamuraOS
# https://github.com/nakamuraos
# Last update: 2021/24/11
# =================
# Default value
# Token string
TOKEN=''
# Namspace (username or orgs)
NAMESPACE=''
# Folder to save repositories
FOLDER_CLONE=$(pwd)
# clone_url | ssh_url | svn_url
URL_TYPE='ssh_url'
# ==================
YELLOW="\e[33m"
GREEN="\e[1;32m"
RED="\e[1;31m"
ENDCOLOR="\e[0m"
echo -e "${YELLOW}"
echo -e "====================================="
echo -e "| CLONE ALL REPOSITORY GITHUB |"
echo -e "====================================="
echo -e "| Thank you for using this script. |"
echo -e "| Give me a star if it's useful. |"
echo -e "====================================="
echo -e "${ENDCOLOR}"
echo -e "${YELLOW}"
echo -e "⚠️ Note: 🤟 Make sure you added SSH key to your GitHub account"
echo -e " if you using method SSH."
echo -e " 🤟 If not you'll failed to clone or you'll must manually"
echo -e " fill username/password to each repo."
echo -e "${ENDCOLOR}"
# ==================
PROCESS_CLONE () {
type=$1
type_url=$2
namespace=$3
token=$4
folder_clone=$5
if [[ $namespace != '' ]]
then
if [[ $token != '' ]]; then HEADER="-H 'Authorization: token ${token}'"; fi
# =====
COMMAND="curl -s ${HEADER} 'https://api.github.com/${type}/${namespace}/repos?per_page=1000' | grep -e '${type_url}*' | cut -d \\\" -f 4"
echo -e "🥰 ${GREEN}Origin: https://github.com/${namespace}${ENDCOLOR}"
echo -e "🤫 ${GREEN}Fetching list of repository...${ENDCOLOR}"
LIST_REPOSITORY=($(eval "$COMMAND"))
echo -e "👽 ${GREEN}Detected ${#LIST_REPOSITORY[@]} repository total.${ENDCOLOR}"
sleep 1;
echo -e "👻 ${GREEN}Starting clone all to ${folder_clone}...${ENDCOLOR}"
sleep 1;
echo ""
for i in "${!LIST_REPOSITORY[@]}"; do
echo -e "👉 ${GREEN}[$((i+1))/${#LIST_REPOSITORY[@]}] Cloning ${LIST_REPOSITORY[i]}...${ENDCOLOR}"
git -C "${folder_clone}" clone "${LIST_REPOSITORY[i]}" && echo "Cloned $((i+1))/${#LIST_REPOSITORY[@]}"
sleep 1;
done
echo "Done."
else
echo "Aborted."
fi
}
# ==================
# Bash Menu Script
echo "Type of repositories which you're cloning:"
PS3="Please enter your choice: "
options=("Personal" "Organization")
select opt in "${options[@]}" "Quit"; do
if [[ $REPLY -gt 0 && $REPLY -lt 3 ]]; then
# Input folder clone
read -rp "Clone into folder (${FOLDER_CLONE:-not set}): " folder_clone
# Input pertional token
read -rp "GitHub personal token (optional) (${TOKEN:-not set}): " token
fi
# Actions
case "$REPLY" in
1)
read -rp "Username clone (${NAMESPACE:-not set}): " namespace
echo ""
PROCESS_CLONE "users" "${URL_TYPE}" "${namespace:-$NAMESPACE}" "${token:-$TOKEN}" "${folder_clone:-$FOLDER_CLONE}"
break;
;;
2)
read -rp "Organization clone (${NAMESPACE:-not set}): " namespace
echo ""
PROCESS_CLONE "orgs" "${URL_TYPE}" "${namespace:-$NAMESPACE}" "${token:-$TOKEN}" "${folder_clone:-$FOLDER_CLONE}"
break;
;;
$((${#options[@]}+1))) echo "Goodbye!"; break;;
*) echo -e "${RED}Invalid option. Try another one.${ENDCOLOR}";continue;;
esac
done
@nakamuraos
Copy link
Author

Screenshot:

Screenshot_2021-11-24_12-42-42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment