Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Created July 12, 2025 19:00
Show Gist options
  • Save RandyMcMillan/1a6cd7e37c35a8e3a780280b26bd9f2e to your computer and use it in GitHub Desktop.
Save RandyMcMillan/1a6cd7e37c35a8e3a780280b26bd9f2e to your computer and use it in GitHub Desktop.
git-clone-gists
#!/bin/bash
# --- Configuration ---
INSTALL_DIR="/usr/local/bin"
SCRIPT_NAME="git-clone-gists"
# --- Functions ---
# Function to display usage information
show_usage() {
echo "Usage: $0 [install|uninstall|--help]"
echo ""
echo "Commands:"
echo " install Install the script to $INSTALL_DIR/$SCRIPT_NAME"
echo " uninstall Remove the script from $INSTALL_DIR/$SCRIPT_NAME"
echo " --help Show this help message"
echo ""
echo "To install for all users, run with 'sudo'."
echo "Example: sudo $0 install"
}
# Function to install the script
install_script() {
echo "Attempting to install '$0' as '$SCRIPT_NAME' to '$INSTALL_DIR'..."
# Check if the install directory exists and is writable
if [ ! -d "$INSTALL_DIR" ]; then
echo "Error: Installation directory '$INSTALL_DIR' does not exist."
echo "Please create it or choose a different directory."
exit 1
fi
if [ ! -w "$INSTALL_DIR" ]; then
echo "Warning: '$INSTALL_DIR' is not writable by the current user."
echo "You may need to run this command with 'sudo' for a system-wide installation."
fi
# Copy the script to the installation directory
cp "$0" "$INSTALL_DIR/$SCRIPT_NAME"
# Make the copied script executable
chmod +x "$INSTALL_DIR/$SCRIPT_NAME"
if [ $? -eq 0 ]; then
echo
echo "Installed '$SCRIPT_NAME'."
else
echo "Error: Failed to install '$SCRIPT_NAME'."
exit 1
fi
}
# Configuration
GITHUB_USERNAME=${1:-randymcmillan}
GITHUB_PAT=${2:-$(cat ~/GITHUB_TOKEN.txt)}
CLONE_DIR="." # Directory to clone Gists into
install_script;
# --- SCRIPT STARTS HERE ---
echo "Starting Gist cloning process for user: $GITHUB_USERNAME"
# Create the cloning directory if it doesn't exist
mkdir -p "$CLONE_DIR"
ln -s . ~/gists
cd "$CLONE_DIR" || { echo "Error: Could not enter directory $CLONE_DIR"; exit 1; }
# Fetch Gists using the GitHub API
# We use pagination to ensure all gists are fetched if you have many
PAGE=1
ALL_GISTS=()
while true; do
echo "Fetching Gists from page $PAGE..."
GISTS_PAGE=$(curl -s -H "Authorization: token $GITHUB_PAT" \
"https://api.github.com/users/$GITHUB_USERNAME/gists?page=$PAGE&per_page=100")
# Check if the response is empty (no more gists)
if [ "$(echo "$GISTS_PAGE" | jq 'length')" -eq 0 ]; then
break
fi
# Add current page's gists to the ALL_GISTS array
ALL_GISTS+=($(echo "$GISTS_PAGE" | jq -r '.[].git_pull_url'))
PAGE=$((PAGE + 1))
done
if [ ${#ALL_GISTS[@]} -eq 0 ]; then
echo "No Gists found for user $GITHUB_USERNAME. Exiting."
echo "Usage:"
echo "clone_gists.sh randymcmillan $(cat ~/GITHUB_TOKEN.txt)"
exit 0
fi
echo "Found ${#ALL_GISTS[@]} Gists to clone."
# Clone each Gist
for GIST_URL in "${ALL_GISTS[@]}"; do
GIST_ID=$(basename "$GIST_URL" .git)
GIST_DIR_NAME=$(echo "$GIST_ID" | cut -c 1-7) # Use first 7 chars of ID for directory name
echo "Cloning Gist: $GIST_URL into directory $GIST_DIR_NAME..."
if git clone "$GIST_URL" "$GIST_DIR_NAME" 2>/dev/null; then
echo "Successfully cloned $GIST_URL"
else
echo "Error cloning $GIST_URL"
fi
done
echo "Gist cloning process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment