Skip to content

Instantly share code, notes, and snippets.

@zudsniper
Forked from erdincay/sugh.sh
Last active June 7, 2023 13:23
Show Gist options
  • Save zudsniper/c3419c9be3b1b01c1608e63967799f53 to your computer and use it in GitHub Desktop.
Save zudsniper/c3419c9be3b1b01c1608e63967799f53 to your computer and use it in GitHub Desktop.
download all repositories from a specified organization. Uses the gh command line tool for authentication of private repositories.
#!/bin/bash
# dlgh.sh
# --------
#
# Download all repositories of a user or organization using the gh commandline tool.
# REQUIRES `gh auth login`!
#
# @zudsniper
# GitHub-themed header
echo -e "\e[1m\e[38;5;220mπŸ™ dlgh.sh - GitHub Repository Cloner πŸš€\e[0m"
# ANSI color schema
if [ ! -f ~/.ansi_colors.sh ]; then
curl -sSL https://raw.githubusercontent.com/zudsniper/bashbits/master/.ansi_colors.sh -o ~/.ansi_colors.sh
fi
source ~/.ansi_colors.sh
# Log function
declare -A LOG_LEVELS
LOG_LEVELS=([7]="silly" [6]="verbose" [5]="debug" [4]="http" [3]="info" [2]="warn" [1]="error" [0]="critical")
function log() {
local LEVEL=${1}
shift
echo -e "${LOG_LEVELS[$LEVEL]}: $@"
}
# CLI flags
VERSION="None"
LOG_LEVEL=3
ZIP_FLAG=false
while (( "$#" )); do
case "$1" in
--exclude|-e)
if [ -n "$2" ]; then
IFS=',' read -ra EXCLUDE_REPOS <<< "$2"
shift 2
else
echo "Error: Argument for --exclude is missing" >&2
exit 1
fi
;;
--help|-h)
echo "Usage: $0 [--version|-V] [--verbose|-v] [--log_level|-l level] [--zip|-z] username"
exit 0
;;
--version|-V)
echo $VERSION
exit 0
;;
--verbose|-v)
LOG_LEVEL=6
shift
;;
--log_level|-l)
if [ -n "$2" ] && [ "$2" -ge 0 ] && [ "$2" -le 7 ]; then
LOG_LEVEL=$2
shift 2
else
echo "Error: Argument for --log_level is missing or out of range" >&2
exit 1
fi
;;
--zip|-z)
ZIP_FLAG=true
shift
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# Check dependencies
for pkg in git gh zip; do
if ! dpkg -s $pkg >/dev/null 2>&1; then
sudo apt-get install -y $pkg
fi
done
# Authenticate with GitHub if not already authenticated
if ! gh auth status >/dev/null 2>&1; then
gh auth login
fi
# Clone repositories
USERNAME=$(echo $PARAMS | xargs)
FOLDER_NAME=$USERNAME
if [[ -x $FOLDER_NAME ]]; then
rm -rf "$FOLDER_NAME"
fi
mkdir -p $FOLDER_NAME
cd $FOLDER_NAME
REPOS=$(gh repo list $USERNAME --json sshUrl -q '.[].sshUrl' | tr -d '"')
# Clone repositories
for REPO in $REPOS; do
REPO_NAME=$(basename $REPO .git)
if [[ " ${EXCLUDE_REPOS[@]} " =~ " ${REPO_NAME} " ]]; then
continue
fi
git clone $REPO
# Check for secrets
if [ -f "$REPO_NAME/.env" ]; then
echo "Warning: .env file found in $REPO_NAME"
fi
if [ -d "$REPO_NAME/node_modules" ]; then
echo "Warning: node_modules directory found in $REPO_NAME"
fi
done
# Zip repositories
if [ "$ZIP_FLAG" = true ]; then
cd ..
zip -r $FOLDER_NAME.zip $FOLDER_NAME
fi
# Print author's GitHub handle
echo -e "script by ${A_GREEN}@zudsniper${NC}"

dlgh.sh

This is a bash script that allows you to clone repositories from a GitHub organization or user. It provides additional functionality through command line flags.

Usage

./dlgh.sh [options] organization-name max-page-number

Command Line Flags

The script supports the following command line flags:

  • --zip or -z: When specified, all cloned repositories will be outputted as a .zip compressed archive file. Each repository will be at the top level within the archive.

  • --help or -h: Prints a usage message, explaining how to use the script and its available options. This message is also displayed if the script is called with no arguments.

Dependencies

To run this script, you need to have the following dependencies installed:

  • jq: A lightweight and flexible command-line JSON processor.
  • gh: The official command-line tool for GitHub.

Authentication

Before using the script, make sure you are authenticated with GitHub by running the command:

gh auth login

If you are not authenticated, the script will display an error message and exit.

Arguments

  • organization-name: The name of the GitHub organization or user from which you want to clone repositories.

  • max-page-number: The maximum number of pages to clone. Each page retrieves up to 100 repositories. If this argument is not provided, the default value is 2.

Cloning Repositories

The script uses the gh repo list command to retrieve a list of repositories from the specified organization or user. It clones each repository using git clone command.

The script will iterate through the specified number of pages (or the default if not provided) and clone all repositories found.

Example Usage

  1. Clone repositories from the "myorg" organization, limiting to 3 pages:
./dlgh.sh myorg 3
  1. Clone repositories from the "myuser" user and create a .zip archive:
./dlgh.sh --zip myuser
  1. Display the usage message:
./dlgh.sh --help
  1. Display the usage message (no arguments):
./dlgh.sh

Please ensure that the script has executable permissions before running it:

chmod +x dlgh.sh

That's it! Enjoy cloning repositories from GitHub using this convenient script. πŸ‘πŸš€

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