-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_setup_script.sh
executable file
·68 lines (53 loc) · 2.34 KB
/
git_setup_script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Setup github ssh keys. Reference: https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
# Warning, not robust when saving ssh key to a file other than "$HOME/.ssh/id_rsa"
source ./script_utils.sh
runprompt "Github SSH Key setup"
####### Start script #######
# Get github commit email
function getemail() {
echo -e -n "${COLOUR}Enter Github commit email (preferably your public userID+username@users.noreply.github.com email): ${NORMAL}" && read -r EMAIL
# Simple but not robust email validation
if [[ $EMAIL =~ ^.+@.+$ ]]; then
echo -e "\n${COLOUR}Using $EMAIL"
echo -e -n "${COLOUR}Is this correct? (y/n): ${NORMAL}" && read -r REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
emailok="true"
fi
else
echo -e "${INVALID}Invalid email"
fi
}
# Get valid email from user
emailok="false"
getemail
while [[ $emailok == "false" ]]; do
echo
getemail
done
echo -e -n "${COLOUR}Enter your name to appear in commits: ${NORMAL}" && read -r NAME
echo -e "${COLOUR}Setting git global config for user.email and user.name${NORMAL}\n"
git config --global user.email $EMAIL
git config --global user.name $NAME
# Generate ssh key
echo -e "${COLOUR}Generating SSH key\n${NORMAL}"
ssh-keygen -t rsa -b 4096 -C $EMAIL
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add the SSH private key
ssh-add $HOME/.ssh/id_rsa
echo -e -n "${COLOUR}Copy ssh key from "$HOME/.ssh/id_rsa" and open github.com now to add ssh key to settings? (y/n): ${NORMAL}" && read -r REPLY
if [[ $REPLY =~ ^[Yy]$ ]];
then
# Copy the ssh key to clipboard
xclip -sel clip < $HOME/.ssh/id_rsa.pub
echo -e "\n${COLOUR}Copied ssh key to clipboard.${NORMAL}"
firefox https://github.com/settings/ssh/new
else
echo -e "${COLOUR}Ok. Copy the contents of $HOME/.ssh/id_rsa.pub later, and head to https://github.com/settings/keys to add the ssh key. Exiting script..."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
echo -e -n "\n${COLOUR}After adding the ssh key to github, press Enter to continue and validate ssh key has been setup correctly. ${NORMAL}" && read -r REPLY
echo -e "\n${COLOUR}Check if the following ssh procedure returns ${NORMAL}\"Hi \$username!...\"${COLOUR} to confirm your ssh connection works.\n${NORMAL}"
ssh -T git@github.com