|
#!/bin/bash |
|
################################################################################ |
|
# To use this script copy and paste the following line without the "#" character |
|
# into a terminal on linux/mac or into 'gitbash' on Windows |
|
# gitbash is part of: MySysgit http://msysgit.github.io/ |
|
################################################################################ |
|
# curl -sLo github-key.sh http://j.mp/github-key && ./github-key.sh |
|
################################################################################ |
|
|
|
GITHUB_TOKEN=$1 |
|
KEY_FILE=~/.ssh/id_rsa.pub |
|
PRIVATE_KEY_FILE=${KEY_FILE%.*} |
|
|
|
GITHUB_KEYNAME=my-private-key-uploaded-by-script |
|
######################################## |
|
# asks for a githug personal token |
|
# with user scope |
|
######################################## |
|
if [[ "$GITHUB_TOKEN" == "" ]] ; then |
|
read -p "create a new github personal token at https://github.com/settings/tokens/new with Token description: 'ssh-key-upload-script' then click 'CREATE TOKEN' once it ready, copy paste the 40 character long token here: " GITHUB_TOKEN |
|
fi |
|
|
|
######################################## |
|
# checks key length |
|
######################################## |
|
if [ ${#GITHUB_TOKEN} -ne 40 ] ; then |
|
echo "[ERROR] the github token should be 40 chars long." |
|
exit -1; |
|
fi |
|
|
|
######################################## |
|
# checks public key, generates a default |
|
# without password |
|
######################################## |
|
if [ -f $KEY_FILE ] ; then |
|
echo "your existing public key will be used: $KEY_FILE" |
|
echo " fingerprint of public key:" |
|
ssh-keygen -lf $KEY_FILE |
|
else |
|
echo "public/private ssh keypair will be generated ..." |
|
ssh-keygen -q -t rsa -f $PRIVATE_KEY_FILE -N "" |
|
fi |
|
|
|
######################################## |
|
# adds new key via github API |
|
######################################## |
|
curl $OPTS -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user/keys -d@- <<EOF |
|
{ |
|
"title" : "$GITHUB_KEYNAME", |
|
"key" : "$(cat $KEY_FILE)" |
|
} |
|
EOF |