78 lines
1.6 KiB
Bash
Executable File
78 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
if [ -z $GOGS_ROOT_URL ]
|
|
then
|
|
printf "Need to set GOGS_ROOT_URL environment variable\n" >&2
|
|
printf "\tIE: https://git.cynarski.pl\n" >&2
|
|
|
|
exit
|
|
fi
|
|
|
|
if [ -z $GOGS_TOKEN ]
|
|
then
|
|
printf "Need to set GOGS_TOKEN environment variable\n" >&2
|
|
printf "\tThis can be obtained at $GOGS_ROOT_URL/user/settings/applications\n" >&2
|
|
exit
|
|
fi
|
|
|
|
if [ -z $GOGS_USER ]
|
|
then
|
|
printf "Need to set GOGS_USER environment variable\n" >&2
|
|
exit
|
|
fi
|
|
|
|
if [ -z $GOGS_REPO ]
|
|
then
|
|
printf "Need to set GOGS_REPO environment variable\n" >&2
|
|
exit
|
|
fi
|
|
|
|
if [ -f $HOME/.ssh/gogs.pub ]; then
|
|
printf "Gogs deploy key is present\n" >&2
|
|
else
|
|
mkdir -p $HOME/.ssh/
|
|
ssh-keygen -f $HOME/.ssh/gogs -t rsa -N ''
|
|
chmod 400 $HOME/.ssh/gogs
|
|
fi
|
|
|
|
prefix="https://"
|
|
sshhost=${GOGS_ROOT_URL#$prefix}
|
|
|
|
sbody=$(cat <<-END
|
|
|
|
Host $sshhost
|
|
IdentityFile ~/.ssh/gogs.pub
|
|
|
|
END
|
|
)
|
|
|
|
if [ -f $HOME/.ssh/$GOGS_REPO-sended ]; then
|
|
printf "Key sended\n" >&2
|
|
else
|
|
printf "Prepare to send key\n" >&2
|
|
title=$(hostname)@$(whoami)
|
|
printf "title $title\n" >&2
|
|
deploykey=$(cat $HOME/.ssh/gogs.pub)
|
|
body=$(cat <<-END
|
|
{
|
|
"title": "$title",
|
|
"key": "$deploykey"
|
|
}
|
|
END
|
|
)
|
|
prefix="https://"
|
|
sshhost=${GOGS_ROOT_URL#$prefix}
|
|
|
|
curl -H "Content-Type: application/json" \
|
|
-H "Authorization: token $GOGS_TOKEN" \
|
|
-d "$body" \
|
|
$GOGS_ROOT_URL/api/v1/repos/$GOGS_USER/$GOGS_REPO/keys > /dev/null
|
|
touch $HOME/.ssh/$GOGS_REPO-sended
|
|
echo "$sbody" >> $HOME/.ssh/config
|
|
|
|
echo "Almost done. Enter clone repository target dir [ENTER]"
|
|
read cdir
|
|
git clone ssh://gogs@$sshhost:65522/$GOGS_USER/$GOGS_REPO $cdir
|
|
fi
|
|
|