-
Notifications
You must be signed in to change notification settings - Fork 0
/
gerrit-auth.sh
executable file
·57 lines (47 loc) · 1.46 KB
/
gerrit-auth.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
#!/bin/bash
KEYFILE=$1
function usage() {
# Print usage and exit
cat <<EOF
Usage: $0 <public-key-file>
$0 uploads an ssh public key to Gerrit, so repos can be
accessed from ssh://gerrit.localhost:29418.
Example:
$0 ~/.ssh/id_rsa.pub
EOF
exit 1
}
function print_repos() {
# Print out a list of array elements
local repos=("$@")
for repo in "${repos[@]}"; do
echo -e " - $repo"
done
}
if [ -z "$*" ]; then
usage
fi
if [[ -s $KEYFILE ]]; then
# Upload SSH Public Key
curl --fail -s -L -X POST -u "workshop:workshop" -H "Content-type:text/plain" \
-d "@$KEYFILE" http://gerrit.localhost/a/accounts/self/sshkeys/ > /dev/null
exit_code=$?
# Provide guidance on curl errors
if [ $exit_code -eq 7 ]; then
echo -e "\nPlease start Gerrit first:\n docker-compose up -d"
elif [ $exit_code -eq 22 ]; then
echo -e "\nPlease wait for Gerrit to finish running and try again"
fi
# Output future guidance
if [ $exit_code -eq 0 ]; then
KEYID=$(ssh-keygen -l -f "$KEYFILE")
GERRIT_REPOS="$(curl -s -L http://gerrit.localhost/projects/ \
| grep \"id\" | cut -c12- | tr -d '",')"
echo -e "Successfully uploaded public keyfile:"
echo " $KEYID"
echo -e "\nYou can now clone the available repos:"
print_repos "$GERRIT_REPOS"
echo -e "\nWith the command:"
echo -e " git clone ssh://workshop@gerrit.localhost:29418/<repo>"
fi
fi