updated gitlab_get_user_ssh_public_keys.sh

pull/2/head
Hari Sekhon 4 years ago
parent b7f0fd90b4
commit a2a6b564cf

@ -14,12 +14,13 @@
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(dirname "${BASH_SOURCE[0]}")"
usage(){
cat <<EOF
Fetches a GitLab user's public SSH key(s) via the GitLab API
User can be given as first argument, or environment variables \$GITLAB_USER or \$USER
User can be given as first argument, otherwise falls back to using environment variables \$GITLAB_USER or \$USER
${0##*/} <user>
EOF
@ -49,7 +50,16 @@ else
usage
fi
# XXX: not handling paging because if you have > 100 SSH keys I want to know what is going on first!
echo "# Fetching SSH Public Key(s) from GitLab for account: $user" >&2
echo "#" >&2
curl -sS --fail "https://gitlab.com/api/v4/users/$user/keys" |
jq -r '.[].key'
shopt -s nocasematch
# case insensitive matching requires [[
if [[ "$user" == "${GITHUB_USER:-}" ]]; then
# authenticated query gets more information which I use to add standard SSH key description suffix back (useful if loading these keys to other systems to know what their descriptions are)
"$srcdir/gitlab_api.sh" "/user/keys"
else
curl -sS --fail "https://gitlab.com/api/v4/users/$user/keys"
fi |
jq -r '.[] | [.key, .title] | @tsv'

Loading…
Cancel
Save