-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-user.sh
More file actions
executable file
·102 lines (81 loc) · 2.49 KB
/
query-user.sh
File metadata and controls
executable file
·102 lines (81 loc) · 2.49 KB
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
#!/usr/bin/env bash
# Display message with yellow text to sdterr
function warn() {
echo -ne '\x1b[33m' >&2
echo "${@}" >&2
echo -ne '\x1b[0m' >&2
}
# Display message with red text to sdterr
function err() {
echo -ne '\x1b[31m' >&2
echo "${@}" >&2
echo -ne '\x1b[0m' >&2
}
function debug() {
echo -ne '\x1b[1;30m' >&2
echo "${@}" >&2
echo -ne '\x1b[0m' >&2
}
# Sanity check for curl – https://curl.se
if ! command -v curl &>/dev/null; then
err "curl executable not found"
exit 1
fi
# Sanity check for jq – https://stedolan.github.io/jq/
if ! command -v jq &>/dev/null; then
err "jq executable not found"
exit 1
fi
# Sanity check for GNU Privacy Guard - https://gnupg.org/
if ! command -v gpg &>/dev/null; then
err "gnupg executable not found"
exit 1
fi
# Display usage if no GitHub usernames specified
if [[ $# -lt 1 ]]; then
warn "No GitHub user names specified!"
echo
echo "Usage: $0 <GitHub user name #1> [ <GitHub user name #2> ... <GitHub user name #N> ]"
echo
exit 1
fi
# Get the directory where this script is installed, in case that's where the token file lives
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# Look for $GITHUB_TOKEN, and if not in the current environment, look for a file named
# .github_token containing the variable, e.g.:
#
# GITHUB_TOKEN=<whatever>
if [[ -z "${GITHUB_TOKEN}" ]]; then
if [[ -f "${SCRIPT_DIR}/.github_token" ]]; then
# shellcheck source=.github_token
. "${SCRIPT_DIR}/.github_token"
elif [[ -f ~/.github_token ]]; then
# shellcheck source=.github_token
. ~/.github_token
else
err "GITHUB_TOKEN environment variable not set!"
exit 1
fi
fi
TMPFILE="$(mktemp)"
trap "rm -f ${TMPFILE}" 0 1 2 15
# Debug info
#debug "Github token is ${GITHUB_TOKEN}"
#debug "Name of temp file is ${TMPFILE}"
for GH_USER in "${@}"; do
curl -sL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/users/${GH_USER}/gpg_keys" | jq -r '.[]|.raw_key' >>"${TMPFILE}"
done
# Display key info for informational purposes
echo -ne '\x1b[36m'
gpg --import --import-options show-only "${TMPFILE}"
echo -ne '\x1b[0m'
warn "Do you wish to import these keys?"
read -n 1 -s -p '(y/N)'
echo
if [[ ${REPLY} =~ ^[yY] ]]; then
gpg --import --import-options 'import-clean=yes,repair-keys=yes' "${TMPFILE}"
fi