Skip to content

Instantly share code, notes, and snippets.

@smarchetti
Last active January 8, 2025 03:05
Show Gist options
  • Select an option

  • Save smarchetti/3864fbce9a888ec1c01dedd127971712 to your computer and use it in GitHub Desktop.

Select an option

Save smarchetti/3864fbce9a888ec1c01dedd127971712 to your computer and use it in GitHub Desktop.
This script automates the setup process for cloning and configuring your dotfiles repository on a new machine. It is specifically designed to handle environments with GitHub SSH authentication and streamline the initial configuration steps.
#!/bin/bash
# Usage function
usage() {
echo "Usage: $0 -e <email> -r <owner/repo> -d <destination>"
echo
echo " -e Email address for SSH key generation"
echo " -r GitHub repository in the format owner/repo"
echo " -d Destination directory for the cloned repository"
exit 1
}
# Parse command-line arguments
while getopts "e:r:d:" opt; do
case $opt in
e) EMAIL="$OPTARG" ;;
r) REPO="$OPTARG" ;;
d) DEST="$OPTARG" ;;
*) usage ;;
esac
done
# Ensure all required arguments are provided
if [ -z "$EMAIL" ] || [ -z "$REPO" ] || [ -z "$DEST" ]; then
usage
fi
# Ensure SSH key is available
if [ ! -f ~/.ssh/id_ed25519 ]; then
echo "SSH key not found. Generating a new SSH key..."
ssh-keygen -t ed25519 -C "$EMAIL" -f ~/.ssh/id_ed25519 -N ""
echo "Copy the following SSH key to GitHub:"
cat ~/.ssh/id_ed25519.pub
echo
echo "Visit https://github.com/settings/keys to add this SSH key."
echo "Press Enter after adding the SSH key to GitHub..."
read
else
echo "SSH key already exists. Skipping key generation."
fi
# Clone the dotfiles repository
if [ -d "$DEST" ]; then
echo "Destination directory $DEST already exists. Skipping clone."
else
echo "Cloning repository git@github.com:$REPO.git into $DEST..."
git clone git@github.com:$REPO.git "$DEST"
if [ $? -eq 0 ]; then
echo "Dotfiles repository cloned successfully into $DEST."
else
echo "Error: Failed to clone the repository. Please check your inputs or SSH configuration."
exit 1
fi
fi
echo "Bootstrap process complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment