-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgh-ssh-keygen.sh
executable file
·182 lines (157 loc) · 5.49 KB
/
gh-ssh-keygen.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
VERSION="0.0.4"
################################################################################
# ASCII #
################################################################################
Reset='\x1b[0m'
Bright='\x1b[1m'
Cyan='\033[0;36m'
Dim='\x1b[2m'
Red='\x1b[31m'
Purple='\033[0;35m'
################################################################################
# Chalk #
################################################################################
function echo_bright() {
echo -e "${Bright}$1${Reset}"
}
################################################################################
# help #
################################################################################
help()
{
echo_bright "NAME"
echo " gh-ssh-keygen 🔑 - adding a new SSH key to your GitHub account automatically"
echo
echo_bright "VERSION"
echo " $(get_version)"
echo
echo_bright "SYNOPSIS"
echo " gh-ssh-keygen [-hv]"
echo
echo_bright "DESCRIPTION"
echo " Want to enable authentication for Git operations over SSH?"
echo " This script helps you generate a SSH key on your local machine and add the key to your account on GitHub.com."
echo " See more detail on GitHub official document - https://docs.github.com/en/authentication/connecting-to-github-with-ssh"
echo
echo
echo " The following options are available:"
echo
echo " -h show this message"
echo " -v get the version"
echo
echo_bright "ENVIRONMENT"
echo " Currently only works on MacOS."
echo
echo_bright "AUTHOR"
echo " maxam2017 🦕"
echo
}
################################################################################
# version #
################################################################################
get_version() {
echo $VERSION
}
while getopts ":hv" option; do
case $option in
h)
help
exit;;
v)
get_version
exit;;
esac
done
# install gh (if not exist)
gh -h &> /dev/null
if [ $? -ne 0 ];then
echo -e "${Red}!${Reset} ${Dim}GitHub CLI is not found.${Reset}"
echo -e "${Purple}>${Reset} ${Dim}Let's install by \`brew\`...${Reset}"
echo
# install brew (if not found)
brew -h &> /dev/null
if [ $? -ne 0 ];then
echo -e "${Red}!${Reset} ${Dim}Package manager \`brew\` is not found.${Reset}"
echo -e "${Purple}>${Reset} ${Dim}Let's install install from internet...${Reset}"
echo
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
case "${SHELL}" in
*/bash*)
if [[ -r "${HOME}/.bash_profile" ]]
then
shell_profile="${HOME}/.bash_profile"
else
shell_profile="${HOME}/.profile"
fi
;;
*/zsh*)
shell_profile="${HOME}/.zprofile"
;;
*)
shell_profile="${HOME}/.profile"
;;
esac
# https://github.com/Homebrew/install/blob/master/install.sh
UNAME_MACHINE="$(/usr/bin/uname -m)"
if [[ "${UNAME_MACHINE}" == "arm64" ]]
then
# On ARM macOS, this script installs to /opt/homebrew only
HOMEBREW_PREFIX="/opt/homebrew"
else
# On Intel macOS, this script installs to /usr/local only
HOMEBREW_PREFIX="/usr/local"
fi
echo 'eval "$($HOMEBREW_PREFIX/bin/brew shellenv)"' >> ~/.zprofile
eval "$($HOMEBREW_PREFIX/bin/brew shellenv)"
fi
brew install gh
clear
fi
# get email
GH_MAIL=$(git config --global user.email)
if [ ! $GH_MAIL ]; then
echo -e "${Red}!${Reset} ${Dim}We can't find your GitHub email address in global git config.${Reset}"
while true; do
echo -en "${Cyan}?${Reset} Please Enter your GitHub email address: "
read GH_MAIL
[[ $GH_MAIL =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]
if [ $? -ne 0 ]; then
echo -e "${Red}x${Reset} ${Dim}Validation error: \"$GH_MAIL\" is not a valid email address.${Reset}"
echo
else
break
fi
done
else
echo -e "${Purple}>${Reset} Your GitHub email address \"$GH_MAIL\" was found in global git config.${Reset}"
echo -e "${Purple}>${Reset} Directly use for ssh key generation...${Reset}"
fi
# generate ssh key
echo
echo -e "${Purple}>${Reset} Generating ssh key for \"$GH_MAIL\"..."
ssh-keygen -t ed25519 -C "$GH_MAIL" -f ~/.ssh/id_ed25519
# add ssh key to ssh-agent
echo
echo -e "${Purple}>${Reset} Adding your SSH key to the ssh-agent..."
grep -q "IdentityFile ~/.ssh/id_ed25519" ~/.ssh/config
if [ $? -ne 0 ]; then
printf "Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
" >> ~/.ssh/config
fi
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# github new ssh key
echo
echo -e "${Purple}>${Reset} Adding your SSH key to your account on GitHub.com..."
gh auth login -w -s admin:public_key,write:gpg_key
while true; do
echo -en "${Cyan}?${Reset} Enter the title for the new key: "
read
gh ssh-key add ~/.ssh/id_ed25519.pub -t "$REPLY"
if [ $? = 0 ]; then
break
fi
done