Skip to content

Instantly share code, notes, and snippets.

@det-peralta
Created May 14, 2024 16:38
Show Gist options
  • Save det-peralta/9f244a105d757d25d29924c262bdf34e to your computer and use it in GitHub Desktop.
Save det-peralta/9f244a105d757d25d29924c262bdf34e to your computer and use it in GitHub Desktop.
This bash script helps automate the setup of GitHub authentication using either a Personal Access Token (PAT) or SSH keys. Follow the script prompts to configure your GitHub credentials securely and efficiently.
#!/bin/bash
# Function to configure Git to use a Personal Access Token (PAT)
setup_pat() {
echo "Configuring Git to use a Personal Access Token (PAT)..."
read -p 'Enter your GitHub username: ' github_username
echo "Please generate a PAT from https://github.com/settings/tokens and copy it."
read -sp 'Enter your GitHub Personal Access Token: ' github_pat
echo
# Store the PAT using Git credential helper
git config --global credential.helper store
echo "https://${github_username}:${github_pat}@github.com" > ~/.git-credentials
echo "PAT configuration complete."
}
# Function to configure SSH keys for GitHub
setup_ssh() {
echo "Setting up SSH keys for GitHub..."
read -p 'Enter your GitHub username: ' github_username
# Generate SSH key if it doesn't exist
if [ ! -f ~/.ssh/id_ed25519 ]; then
echo "Generating a new SSH key..."
ssh-keygen -t ed25519 -C "$(git config user.email)"
else
echo "SSH key already exists."
fi
# Start the SSH agent and add the SSH key
echo "Starting the SSH agent..."
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Display the SSH public key
echo "Copy the following SSH key and add it to your GitHub account at https://github.com/settings/keys"
echo "Public SSH Key:"
cat ~/.ssh/id_ed25519.pub
echo "SSH key configuration complete."
}
# Function to prompt user for setup choice
prompt_user() {
echo "Choose the authentication method you want to set up:"
echo "1. Personal Access Token (PAT)"
echo "2. SSH Key"
read -p 'Enter your choice (1 or 2): ' choice
case $choice in
1)
setup_pat
;;
2)
setup_ssh
;;
*)
echo "Invalid choice. Please run the script again and select 1 or 2."
;;
esac
}
# Main script execution
prompt_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment