-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgh-ma
executable file
·333 lines (285 loc) · 9.28 KB
/
gh-ma
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
# vim: set expandtab:tabstop=4:shiftwidth=4
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright Matthew Cline (2021)
# License: GPL v3+
set -o nounset
###############################################################################
# Basic setup
gh_ma_ver="1.0.0"
gh_ma_url="https://github.com/matthew-cline/gh-multi-account"
function gh_ma_help() {
echo For help see: 1>&2
echo $gh_ma_url/blob/v$gh_ma_ver/README.md 1>&2
}
gh_conf_dir=${GH_CONFIG_DIR:-"$HOME/.config/gh"}
conf_dir=${GH_MA_CONFIG_DIR:-"$gh_conf_dir/multi-account"}
conf_file="$conf_dir/tokens.conf"
if [[ ! -e "$gh_conf_dir" ]]; then
echo gh-ma: ERROR: gh config dir \"$gh_conf_dir\" does not exist 1>&2
gh_ma_help
exit 1
fi
if [[ ! -e "$conf_dir" ]]; then
echo gh-ma: ERROR: gh multi account config dir \"$conf_dir\" does \
not exist 1>&2
gh_ma_help
exit 1
fi
if [[ ! -e "$conf_file" ]]; then
echo gh-ma: ERROR: \"$conf_file\" does not exist 1>&2
gh_ma_help
exit 1
fi
if [[ ! -r "$conf_file" ]]; then
echo gh-ma: ERROR: \"$conf_file\" not readable 1>&2
gh_ma_help
exit 1
fi
is_an_alias=0
is_in_path=0
if [[ ${BASH_SOURCE[0]} == "gh" || ${BASH_SOURCE[0]} =~ /gh$ ]]; then
is_in_path=1
else
is_an_alias=1
fi
###############################################################################
# Find the GitHub CLI executable to use
if [[ -n "${GH_EXE+unset}" ]]; then
gh_exe=$GH_EXE
elif [[ $is_in_path -eq 1 ]]; then
# Cache the path to the GitHub CLI executable via a symboloc link,
# to reduce the expense of forks incurred by using non-builtin commands.
gh_exe="$conf_dir/gh"
# NOTE: if file test "-e" is given a symbolic link, it tests for the
# existence of what the link points to, not the link itself.
if [[ ! -e "$gh_exe" ]]; then
if [[ ! -w $conf_dir ]]; then
echo gh-ma: ERROR: cannot fix stale/nonexistent \"$gh_exe\" 1>&2
gh_ma_help
exit 1
fi
ma_exe="$(readlink -f "${BASH_SOURCE[0]}")"
cli_exe=""
possibilities=($(which -a gh))
for file in ${possibilities[@]}; do
# Expand "~"
eval file="$file"
# Only use absolute links
if [[ ! $file =~ ^/ ]]; then
continue
fi
# Skip anything that seems to be the multi account wrapper script
if [[ "$ma_exe" != "$(readlink -f "$file")" ]]; then
cli_exe="$file"
break
fi
done
if [[ -z "$cli_exe" ]]; then
echo gh-ma: ERROR: could not find GitHub CLI executable 1>&2
exit 1
fi
ln -f -s "$cli_exe" "$gh_exe"
fi
else
# If we're used via an alais just use whatever is in PATH
gh_exe="gh"
fi
###############################################################################
# Setup is done, but we might not have a lot to do, depending upon the
# environmental variables and the command line arguments.
# If a GitHub token is already set we have nothing to do
set +o nounset
if [[ -n "$GH_TOKEN" || -n "$GITHUB_TOKEN" || -n "$GH_ENTERPRISE_TOKEN" ||
-n "$GITHUB_ENTERPRISE_TOKEN" ]]; then
exec $gh_exe "$@"
fi
set -o nounset
token_id=""
case ${1:-""} in
--version|--help|alias|completion|config|help)
# These don't use authorization, simply pass to gh
exec $gh_exe "$@"
;;
--version-ma)
echo "gh multi account version $gh_ma_ver"
exit 0
;;
--ma-info)
if [[ $is_in_path -eq 1 ]]; then
echo We appear to be installed in PATH
if [[ -z "${GH_EXE+unset}" ]]; then
gh_exe="$(readlink "$gh_exe")"
fi
else
echo We appear to be used via an alias
fi
echo GitHub CLI executable used is \"$gh_exe\"
if [[ -n "${GH_EXE+unset}" ]]; then
echo Executable was set via env var GH_EXE
fi
echo
default_token_id_file="$conf_dir/default-token-id"
default_token_id_script="$conf_dir/default-token-id.sh"
if [[ -r "$default_token_id_script" ]]; then
source "$default_token_id_script"
script_default_id=${token_id:-""}
fi
if [[ -n ${GH_DEFAULT_TOKEN_ID+unset} ]]; then
echo Default token id is $GH_DEFAULT_TOKEN_ID
echo Default token id set by env var GH_DEFAULT_TOKEN_ID
if [[ -n "$srcript_default_id" ]]; then
echo A default token id is also specified in \
$default_token_id_script
fi
if [[ -r "$default_token_id_file" ]]; then
echo A default token id is also specified in \
$default_token_id_file
fi
elif [[ -n "$script_default_id" ]]; then
echo Default token id is $script_default_id
echo Default token id is specified in $default_token_id_script
if [[ -r "$default_token_id_file" ]]; then
echo A default token id is also specified in \
$default_token_id_file
fi
elif [[ -r "$default_token_id_file" ]]; then
echo Default token id is $(<$default_token_id_file)
echo Default token id is specified in $default_token_id_file
else
echo No default token id specified
fi
if [[ -r "$default_token_id_script" && -z "$script_default_id" ]]; then
echo $default_token_id_script exists but does not set token_id \
when sourced
fi
echo
echo GitHub CLI config dir is \"$gh_conf_dir\"
if [[ -n ${GH_CONFIG_DIR+unset} ]]; then
echo Directory set via env var GH_CONFIG_DIR
fi
if [[ ! -w "$gh_conf_dir" ]]; then
echo NOTE: you do not have write access to that directory
fi
echo
echo gh multi account config dir is \"$conf_dir\"
if [[ -n ${GH_MA_CONFIG_DIR+unset} ]]; then
echo Directory set via env var GH_MA_CONFIG_DIR
fi
if [[ ! -w "$conf_dir" ]]; then
echo NOTE: you do not have write access to that directory
fi
exit 0
;;
--token-id)
shift
if [[ -z ${1:-""} ]]; then
echo gh-ma: ERROR: --token-id missing token 1>&2
gh_ma_help
exit 1
fi
token_id="$1"
shift
;;
--is-ma)
# Simply return true (0), for use by scripts
exit 0
;;
*)
;;
esac
###############################################################################
# Get down to it
# Bash file descriptor redirection, so we don't have to do "done < $conf"
# at the end of the while loop. Also makes for a cleaner test of read
# failure.
FD=""
exec {FD}<"$conf_file"
if [[ -z "${FD}" ]]; then
echo gh-ma: ERROR: could not read \"$conf_file\" 1>&2
gh_ma_help
exit 1
fi
declare -A tokens_arr
while IFS=$': \t' read -u ${FD} id tok ; do
# Ignore empty lines and comments
if [[ -z "$id" || "$id" =~ ^# ]]; then
continue
fi
if [[ -z "$tok" ]]; then
tok="none"
fi
tokens_arr["$id"]="$tok"
done
exec {FD}<&- # close file
id_arr=("${!tokens_arr[@]}")
if [[ ${#id_arr[@]} -eq 0 ]]; then
echo gh-ma: ERROR: \"$conf_file\" has no tokens 1>&2
exit 1
fi
remote_url=$(git config remote.origin.url)
if [[ -n "$token_id" ]]; then
: # token id already set via the command line option
elif [[ -n "$remote_url" ]]; then
# Get id in the form of "host/account/repo" from
# "https://host/account/repo.git" or "host:account/repo"
# from "git@host:account/repo.git"
repo_id=${remote_url#https://}
repo_id=${repo_id##*@}
repo_id=${repo_id%.git}
# Split into parts
OLD_IFS="$IFS"
IFS=':/' id_parts=($repo_id)
IFS="$OLD_IFS"
if [[ ${#id_parts[@]} -ne 3 ]]; then
echo gh-ma: ERROR: unable to parse remote url \"$remote_url\" 1>&2
gh_ma_help
exit 1
fi
# Possible token ids, from most specific to least specific
possible_ids=(
"${id_parts[0]}/${id_parts[1]}/${id_parts[2]}"
"${id_parts[0]}/${id_parts[1]}"
"${id_parts[0]}"
)
for id in ${possible_ids[@]}; do
if [[ -n ${tokens_arr[$id]+unset} ]]; then
token_id="$id"
break
fi
done
if [[ -z "$token_id" ]]; then
echo gh-ma: ERROR: no matching token id for \"$remote_url\" 1>&2
gh_ma_help
exit 1
fi
else
if [[ -n "${GH_DEFAULT_TOKEN_ID+unset}" ]]; then
token_id="${GH_DEFAULT_TOKEN_ID}"
else
if [[ -r "$conf_dir/default-token-id.sh" ]]; then
source "$conf_dir/default-token-id.sh"
fi
if [[ -z "$token_id" && -r "$conf_dir/default-token-id" ]]; then
read token_id < "$conf_dir/default-token-id"
fi
fi
if [[ -z "$token_id" ]]; then
echo gh-ma: ERROR: no default token id 1>&2
gh_ma_help
exit 1
fi
fi
token="${tokens_arr[$token_id]:-""}"
if [[ -z "$token" ]]; then
echo gh-ma: ERROR: token id $token_id not present in tokens.conf 1>&2
gh_ma_help
exit 1
fi
if [[ "$token" == "none" ]]; then
# User wants to specifically log in for this.
exec $gh_exe "$@"
fi
export GH_TOKEN="$token"
export GH_ENTERPRISE_TOKEN="$token"
exec "$gh_exe" "$@"