Skip to content

Commit ea40b97

Browse files
committed
feat: Enhance repository installation with error handling and validation
1 parent 13ff5be commit ea40b97

1 file changed

Lines changed: 89 additions & 14 deletions

File tree

alteriso/setup_3rd_repo.sh

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,28 @@ _msg_warn() {
5858
# $1: repository name
5959
# $2: include file path
6060
_repo_config() {
61+
if [[ -z "$1" || -z "$2" ]]; then
62+
_msg_error "_repo_config requires 2 arguments: <repo_name> <include_file_path>"
63+
return 1
64+
fi
65+
if [[ ! -e "$2" ]]; then
66+
_msg_error "include file '$2' does not exist"
67+
return 1
68+
fi
69+
if ! grep -v "^#" "$2" | grep -qE '^\s*Server\s*='; then
70+
_msg_error "include file '$2' does not contain any valid Server entries"
71+
return 1
72+
fi
6173
printf '[%s]\nInclude = %s\n' "$1" "$2"
6274
}
6375

76+
_pacman_R_safe() {
77+
local _pkg="$1"
78+
if _pacman -Q "$_pkg" &>/dev/null; then
79+
_pacman -R --noconfirm "$_pkg"
80+
fi
81+
}
82+
6483
_blackarch_install() {
6584
if _has_repo "blackarch"; then
6685
_msg_warn "blackarch repository already exists in pacman configuration."
@@ -175,10 +194,13 @@ _arch4edu_install_keyring() {
175194
}
176195

177196
_chaotic_install_keyring() {
178-
local _key_id="3056513887B78AEB"
179-
_pacman_key --recv-keys "$_key_id" --keyserver keyserver.ubuntu.com
180-
_pacman_key --lsign-key "$_key_id"
181-
_pacman_key --finger "$_key_id"
197+
local _key_ids=("3056513887B78AEB" "349BC7808577C592")
198+
local _key_id
199+
for _key_id in "${_key_ids[@]}"; do
200+
_pacman_key --recv-keys "$_key_id" --keyserver keyserver.ubuntu.com
201+
_pacman_key --lsign-key "$_key_id"
202+
_pacman_key --finger "$_key_id"
203+
done
182204
}
183205

184206
_blackarch_keyring_files() {
@@ -261,7 +283,7 @@ _arch4edu_install_pkgs() {
261283
# Install arch4edu-keyring using the temporary config
262284
local _old_config_path="$config_path"
263285
config_path="$_conf_file"
264-
_pacman_S -y arch4edu-keyring arch4edu-mirrorlist
286+
_pacman_S -y arch4edu-keyring mirrorlist.arch4edu
265287
config_path="$_old_config_path"
266288
}
267289

@@ -286,7 +308,9 @@ _blackarch_apppend_repo() {
286308
}
287309

288310
_archlinuxcn_apppend_repo() {
289-
_repo_config archlinuxcn "/etc/pacman.d/archlinuxcn-mirrorlist" >>"$config_path"
311+
local _mirrorlist="/etc/pacman.d/archlinuxcn-mirrorlist"
312+
sed -i 's|^# Server|Server|' "$_mirrorlist"
313+
_repo_config archlinuxcn "$_mirrorlist" >>"$config_path"
290314
}
291315

292316
_arch4edu_apppend_repo() {
@@ -313,6 +337,60 @@ _chaotic_mirrorlist() {
313337
curl -fsSL "https://gitlab.com/chaotic-aur/pkgbuilds/-/raw/main/chaotic-mirrorlist/mirrorlist"
314338
}
315339

340+
_blackarch_onfail() {
341+
local _exit_code="$?"
342+
_msg_error "Failed to install blackarch repository."
343+
344+
_pacman_R_safe "blackarch-keyring"
345+
_pacman_R_safe "blackarch-mirrorlist"
346+
347+
return "$_exit_code"
348+
}
349+
_archlinuxcn_onfail() {
350+
local _exit_code="$?"
351+
_msg_error "Failed to install archlinuxcn repository."
352+
353+
_pacman_R_safe "archlinuxcn-keyring"
354+
_pacman_R_safe "archlinuxcn-mirrorlist-git"
355+
356+
return "$_exit_code"
357+
}
358+
359+
_arch4edu_onfail() {
360+
local _exit_code="$?"
361+
_msg_error "Failed to install arch4edu repository."
362+
363+
_pacman_R_safe "arch4edu-keyring"
364+
_pacman_R_safe "mirrorlist.arch4edu"
365+
366+
return "$_exit_code"
367+
}
368+
369+
_chaotic_onfail() {
370+
local _exit_code="$?"
371+
_msg_error "Failed to install chaotic repository."
372+
373+
_pacman_R_safe "chaotic-keyring"
374+
_pacman_R_safe "chaotic-mirrorlist"
375+
376+
return "$_exit_code"
377+
}
378+
379+
_install_all() {
380+
local _failed=() repo
381+
for repo in blackarch archlinuxcn arch4edu chaotic; do
382+
if ! _"${repo}_install"; then
383+
_"$repo"_onfail
384+
_failed+=("$repo")
385+
fi
386+
done
387+
if [[ "${#_failed[@]}" -ne 0 ]]; then
388+
_msg_error "The following repositories failed to install: ${_failed[*]}"
389+
return 1
390+
fi
391+
_pacman_S -y
392+
}
393+
316394
_init() {
317395
# Parse flags: -c pacman_config, -r archlinuxcn|blackarch|arch4edu|chaotic|all, -v, -q
318396
while getopts ":c:r:vq" opt; do
@@ -360,22 +438,19 @@ _main() {
360438

361439
case "$target_repo" in
362440
blackarch)
363-
_blackarch_install
441+
_blackarch_install || _blackarch_onfail
364442
;;
365443
archlinuxcn)
366-
_archlinuxcn_install
444+
_archlinuxcn_install || _archlinuxcn_onfail
367445
;;
368446
arch4edu)
369-
_arch4edu_install
447+
_arch4edu_install || _arch4edu_onfail
370448
;;
371449
chaotic)
372-
_chaotic_install
450+
_chaotic_install || _chaotic_onfail
373451
;;
374452
all)
375-
_arch4edu_install
376-
_blackarch_install
377-
_archlinuxcn_install
378-
_chaotic_install
453+
_install_all
379454
;;
380455
esac
381456
cd "${OLDPWD-.}" || exit 1

0 commit comments

Comments
 (0)