#!/bin/bash -e echo "macOS Helper to configure SSH Key to use with GitHub" echo "based off of instructions reported at:" echo "https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account" echo if [[ "$OSTYPE" != "darwin"* ]]; then echo "This script is designed for macOS only..." echo "exiting" exit 1 fi echo "Checking existing ed25519 SSH key..." if [ ! -f "$HOME/.ssh/id_ed25519" ]; then echo "You don't have it, generating it for you..." echo EMAIL=$(git config --global user.email) echo "Detected email from git config: $EMAIL" echo echo "IF IT'S NOT CORRECT, PLEASE HIT CONTROL + C TO EXIT THIS SCRIPT" echo "AND PROPERLY CONFIGURE YOUR GIT EMAIL WITH THIS COMMAND:" echo "git config --global user.email \"youremail@provider.com\"" echo echo "When prompted where to save the certificate, press ENTER" echo "and then press ENTER two more times when asked to put in a password" echo ssh-keygen -t ed25519 -C "$EMAIL" || true else echo "Good! You have it!" fi echo echo "Starting SSH Agent in the background" eval "$(ssh-agent -s)" if [ ! -f "$HOME/.ssh/config" ]; then echo echo "Creating new SSH config" touch "$HOME/.ssh/config" else echo "Using existing SSH config" fi if grep -q "IdentityFile ~/.ssh/id_ed25519" "$HOME/.ssh/config"; then echo "SSH Config already correct!" else echo "Adding the configuration to SSH Config" echo "Host *" >> "$HOME/.ssh/config" echo " AddKeysToAgent yes" >> "$HOME/.ssh/config" echo " IdentityFile ~/.ssh/id_ed25519" >> "$HOME/.ssh/config" echo echo "Adding private key to SSH Agent" ssh-add "$HOME/.ssh/id_ed25519" fi echo "Copying SSH public key in the clipboard" pbcopy < "$HOME/.ssh/id_ed25519.pub" echo echo "Now navigate to https://github.com/settings/keys" echo "and press on New SSH Key" echo echo "1. Assign a custom title. For example: macOS SSH Key" echo "2. and then press COMMAND + V in the Key field" echo "3. Hit Add SSH Key" echo "4. Confirm with your password and then enable SSO if needed with your organization"