Skip to content

Instantly share code, notes, and snippets.

@callaginn
Last active August 27, 2020 21:10
Show Gist options
  • Save callaginn/604b7949c542f5172691961289133368 to your computer and use it in GitHub Desktop.
Save callaginn/604b7949c542f5172691961289133368 to your computer and use it in GitHub Desktop.
Creates new SSH key, copies it to Github, and clones private config repo.
#!/bin/bash
#hey
br=$'\n'
b=$(tput bold)
c=$(tput sgr0)
yellow=$(tput setaf 11)
ssh_title="$(scutil --get ComputerName) ($(date +'%Y-%m-%d'))"
alert () { echo "${b}${yellow}$1${c}"; }
# Might as well ask for password up-front, right?
sudo -v
# Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Include configuration file if it exists.
test -f ~/.setup && . $_
alert "Creating New SSH Key"
ssh-keygen -f ~/.ssh/$ssh_file -t rsa -b 4096 -C $git_email -P ""
test ! -f ~/.ssh/config && touch ~/.ssh/config
echo "Host *${br} AddKeysToAgent yes${br} UseKeychain yes${br} IdentityFile ~/.ssh/$ssh_file" >> ~/.ssh/config
ssh-add -K ~/.ssh/$ssh_file
alert "Adding SSH Key to GitHub"
curl -u "$github_user:$github_pwd" --data '{"title": "'"$ssh_title"'", "key":"'"$(cat ~/.ssh/id_rsa.pub)"'"}' https://api.github.com/user/keys
alert "Cloning Config Repo"
test ! -d $sites && sudo mkdir $_ && sudo chown -R $USER $_
# this will automatically install XCode...
git clone "$setup_repo" $sites/config
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment