-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrhino-hotfix
executable file
·358 lines (317 loc) · 10.2 KB
/
rhino-hotfix
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash
tabs 4
export BOLD=$'\033[1m'
export NC=$'\033[0m'
export RED=$'\033[0;31m'
export GREEN=$'\033[0;32m'
export YELLOW=$'\033[0;33m'
export BLUE=$'\033[0;34m'
export PURPLE=$'\033[0;35m'
export CYAN=$'\033[0;36m'
export BRed=$'\033[1;31m'
export BGreen=$'\033[1;32m'
export BYellow=$'\033[1;33m'
export BBlue=$'\033[1;34m'
export BPurple=$'\033[1;35m'
export BCyan=$'\033[1;36m'
export FIXDIR="/tmp/rhino-hotfix"
export CARCH="$(dpkg --print-architecture)"
function fancy_message() {
local MESSAGE_TYPE="${1}" MESSAGE="${2}" FANCYTEXT
shift 2
local PRINTARGS=("${@}")
case ${MESSAGE_TYPE} in
info) FANCYTEXT="[${BGreen}+${NC}] ${BOLD}INFO${NC}:" ;;
warn) FANCYTEXT="[${BYellow}*${NC}] ${BOLD}WARNING${NC}:" ;;
error) FANCYTEXT="[${BRed}!${NC}] ${BOLD}ERROR${NC}:" ;;
note) FANCYTEXT="[${BCyan}~${NC}] ${BOLD}NOTE${NC}:" ;;
sub) FANCYTEXT="\t[${BBlue}>${NC}]" ;;
*) FANCYTEXT="[${BOLD}?${NC}] ${BOLD}UNKNOWN${NC}:" ;;
esac
case ${MESSAGE_TYPE} in
info|sub) printf "${FANCYTEXT} ${MESSAGE}\n" "${PRINTARGS[@]}" ;;
*) printf "${FANCYTEXT} ${MESSAGE}\n" "${PRINTARGS[@]}" >&2 ;;
esac
}
function ask() {
local prompt default reply template="${1}"
shift 1
local yn="${@: -1}"
local rest=("${@:1:$(($# - 1))}")
if [[ ${yn} == 'Y' ]]; then
prompt="${BIGreen}Y${NC}/${RED}n${NC}"
default='Y'
elif [[ ${yn} == 'N' ]]; then
prompt="${GREEN}y${NC}/${BRed}N${NC}"
default='N'
else
prompt="${GREEN}y${NC}/${RED}n${NC}"
fi
printf "${template} [$prompt] " "${rest[@]}"
if [[ ${DISABLE_PROMPTS:-z} == "z" ]]; then
export DISABLE_PROMPTS="no"
fi
if [[ $DISABLE_PROMPTS == "no" ]]; then
read -r reply <&0
if [[ $NON_INTERACTIVE ]]; then
if [[ -z $reply ]]; then
echo -n "$default"
fi
echo "$reply"
fi
else
echo "$default"
reply=$default
fi
if [[ -z $reply ]]; then
reply=$default
fi
while :; do
case "$reply" in
Y* | y*)
export answer=1
break
;;
N* | n*)
export answer=0
break
;;
*)
printf "${template} [$prompt] " "${rest[@]}"
read -r reply < /dev/tty
;;
esac
done
}
function cleanup() {
if [[ -d "${FIXDIR}" ]]; then
rm -rf "${FIXDIR}"/*
fi
unset SRCDIR 2> /dev/null
unset -f hotfix 2> /dev/null
}
function trap_ctrlc() {
fancy_message warn "Interrupted, cleaning up"
cleanup
exit 1
}
function parse_link() {
local metalink
local -n ref_user="${2}" ref_repo="${3}" ref_branch="${4}" ref_pr="${5}"
metalink="${1}"
if [[ ${metalink} == *"#"* ]]; then
if [[ ${metalink} == *":"* ]]; then
fancy_message error "Branch and PR cannot both be provided"
exit 1
fi
ref_pr="${metalink##*#}"
metalink="${metalink%%#*}"
fi
if [[ ${metalink} == *":"* ]]; then
ref_branch="${metalink##*:}"
metalink="${metalink%%:*}"
else
ref_branch="main"
fi
[[ -z ${metalink} ]] && metalink="rhino-linux/hotfix"
ref_user="${metalink%%/*}"
if [[ ${metalink} == *"/"* ]]; then
ref_repo="${metalink##*/}"
else
fancy_message error "Malformed input"
exit 1
fi
}
function parse_pr() {
local orig_user="${1}" orig_repo="${2}" num="${3}" head_name head_sha head_user gh_provides
local -n ref_url="${4}"
gh_provides=$(curl -fsSL "https://api.github.com/repos/${orig_user}/${orig_repo}/pulls/${num}") || exit 1
head_name=$(echo "${gh_provides}" | jq -r '.head.repo.name')
head_sha=$(echo "${gh_provides}" | jq -r '.head.sha')
head_user=$(echo "${gh_provides}" | jq -r '.head.user.login')
ref_url="https://raw.githubusercontent.com/${head_user}/${head_name}/${head_sha}"
}
function array.contains() {
local check
local -n arra="${1:?No array passed to array.contains}"
local input="${2:?No input given to array.contains}"
for check in "${arra[@]}"; do
if [[ ${check} == "${input}" ]]; then
return 0
fi
done
return 1
}
function fail_out_functions() {
local func="${1}" name="${2}"
trap - ERR
eval "${restoreshopt}"
fancy_message error "Hotfix for ${BLUE}${name}${NC} failed"
fancy_message info "Cleaning up"
exit 1
}
function run_function() {
local func="${1}" name="${2}"
${func}
return $?
}
function safe_run() {
local func="${1}" name="${2}"
export restoreshopt="$(shopt -p; shopt -p -o)"
local -
shopt -o -s errexit errtrace pipefail
local restoretrap="$(trap -p ERR)"
trap "fail_out_functions '${func}' '${name}'" ERR
run_function "${func}" "${name}"
trap - ERR
eval "${restoreshopt}"
eval "${restoretrap}"
}
if ! [[ -f "/etc/os-release" ]]; then
fancy_message error "Not running on a Linux system."
exit 1
fi
while IFS='=' read -r key value; do
case "${key}" in
"NAME") d_name="${value//\"/}" ;;
"VERSION_CODENAME") d_codename="${value//\"/}" ;;
"ID") d_id="${value//\"/}" ;;
esac
done < /etc/os-release
if ! [[ ${d_name} == "Rhino Linux" && ${d_codename} == "devel" && ${d_id} == "ubuntu" ]]; then
fancy_message error "Not running on a Rhino Linux system."
exit 1
fi
input="${1}"
if [[ ${input} == "-h" || ${input} == "--help" ]]; then
echo "Usage: rhino-hotfix <input> [-h]
Input format:
<hotfix>[@<metalink>]
<hotfix> (optional):
- \`hotfix\`: Fetch a specific hotfix.
- \`hotfix@<metalink>\`: Fetch from a specific repo, branch, or PR.
<metalink> (optional):
- \`[user/repo]\`: Use a specific repo.
- \`[user/repo:branch]\` or \`[:branch]\`: Use a specific branch.
- \`[user/repo#PR]\` or \`[#PR]\`: Use a specific PR number.
Note: only branch or PR can be used, not both.
Examples:
rhino-hotfix # List hotfixes from rhino-linux/hotfix.
rhino-hotfix :branch # List hotfixes from an upstream branch.
rhino-hotfix hotfix # Fetch a hotfix from rhino-linux/hotfix.
rhino-hotfix hotfix@#42 # Fetch a hotfix from PR #42 upstream.
rhino-hotfix @user/repo#99 # List hotfixes from PR #99 downstream."
exit 0
fi
trap cleanup EXIT
trap trap_ctrlc 2
if [[ ${input} == *"@"* ]]; then
IN_KEY="${input%%@*}"
input="${input##*@}"
elif [[ ${input} != *":"* && ${input} != *"#"* && ${input} != *"/"* ]]; then
IN_KEY="${input}"
unset input
else
unset IN_KEY
fi
parse_link "${input}" user repo branch pr
login="${CYAN}${user}/${repo}${NC}"
if [[ -z ${pr} ]]; then
BASEURL="https://raw.githubusercontent.com/${user}/${repo}/refs/heads/${branch}"
[[ ${branch} != "main" ]] && login+="${YELLOW}:${branch}${NC}"
else
parse_pr "${user}" "${repo}" "${pr}" BASEURL
login+="${YELLOW}#${pr}${NC}"
fi
hotfixes="$(curl -fsSL ${BASEURL}/hotfixes.json)" || { fancy_message error "Failed to fetch hotfixes from ${login}"; exit 1; }
if [[ ${hotfixes} == "{}" ]]; then
fancy_message note "There are ${BPurple}0${NC} hotfixes available from ${login}"
exit 0
fi
mapfile -t keys < <(jq -r 'keys[]' <<< "${hotfixes}")
mapfile -t scripts < <(jq -r '.[] | .script' <<< "${hotfixes}")
mapfile -t descriptions < <(jq -r '.[] | .description' <<< "${hotfixes}")
mapfile -t published_dates < <(jq -r '.[] | .published' <<< "${hotfixes}")
declare -A data
for i in "${!keys[@]}"; do
data["${keys[i]}_script"]="${scripts[i]}"
data["${keys[i]}_description"]="${descriptions[i]}"
data["${keys[i]}_published"]="${published_dates[i]}"
done
if [[ -n ${IN_KEY} ]]; then
if ! array.contains keys "${IN_KEY}"; then
fancy_message error "${BOLD}${IN_KEY}${NC} is not an available hotfix from ${login}"
exit 1
fi
fancy_message info "Fetched hotfix from ${login}"
fancy_message sub "${BOLD}${IN_KEY}${NC} (${data[${IN_KEY}_published]}) - ${data[${IN_KEY}_description]}"
selected_keys=("${IN_KEY}")
else
fancy_message info "Fetched hotfixes from ${login}"
fancy_message note "There are ${BPurple}${#keys[@]}${NC} hotfixes available:"
echo -e "\t[${RED}N${NC}] Exit"
for i in "${!keys[@]}"; do
echo -e "\t[${GREEN}$((i + 1))${NC}] ${BOLD}${keys[i]}${NC} (${data[${keys[i]}_published]}) - ${data[${keys[i]}_description]}"
done
if ((${#keys[@]} <= 6)); then
for ((i = 0; i < ${#keys[@]}; i++)); do
numlist+="${GREEN}$((i + 1))${NC} "
done
else
numlist="1 2 3 ... "
for ((i = ${#keys[@]} - 3; i < ${#keys[@]}; i++)); do
numlist+="${GREEN}$((i + 1))${NC} "
done
fi
read -p "[${BPurple}#${NC}] ${BOLD}PICK${NC}: Which hotfixes would you like to install? [${numlist}${RED}N${NC}] " -a selection
selected_keys=()
if array.contains selection "N" || array.contains selection "n"; then
fancy_message info "Cleaning up"
exit 0
fi
for choice in "${selection[@]}"; do
if [[ "${choice}" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice < ${#keys[@]} + 1 )); then
selected_keys+=("${keys[$((choice - 1))]}")
else
fancy_message error "${GREEN}${choice}${NC} is not an option"
exit 1
fi
done
fi
mkdir -p "${FIXDIR}"
cd "${FIXDIR}"
for i in "${selected_keys[@]}"; do
export SRCDIR="${FIXDIR}/${i}-${data[${i}_published]}"
mkdir -p "${SRCDIR}"
cd "${SRCDIR}"
curl -fsSL "${BASEURL}/${data[${i}_script]}" -o "${FIXDIR}/${data[${i}_script]##*/}" || { fancy_message error "failed to fetch script for ${BLUE}${i}${NC}"; exit 1; }
ask "(${BBlue}${i}${NC}) Do you want to view/edit the hotfix?" N
if ((answer == 1)); then
(
if [[ -n $EDITOR ]]; then
$EDITOR "${FIXDIR}/${data[${i}_script]##*/}"
elif [[ -n $VISUAL ]]; then
$VISUAL "${FIXDIR}/${data[${i}_script]##*/}"
elif command -v sensible-editor > /dev/null; then
sensible-editor "${FIXDIR}/${data[${i}_script]##*/}"
elif command -v editor > /dev/null; then
editor "${FIXDIR}/${data[${i}_script]##*/}"
elif command -v nano > /dev/null; then
nano "${FIXDIR}/${data[${i}_script]##*/}"
elif command -v vim > /dev/null; then
vim "${FIXDIR}/${data[${i}_script]##*/}"
elif command -v vi > /dev/null; then
vi "${FIXDIR}/${data[${i}_script]##*/}"
else
fancy_message error "No editors found, please set the EDITOR or VISUAL environment variable."
fi
)
fi
source "${FIXDIR}/${data[${i}_script]##*/}"
safe_run hotfix "${i}"
cd "${FIXDIR}"
unset -f hotfix 2> /dev/null
rm -rf "${SRCDIR}"
done
fancy_message info "Cleaning up"
exit 0