Skip to content

Commit 59dcbcc

Browse files
committed
refactor(functions): Extract local region code detection to separate function
Simplify logic. Signed-off-by: 林博仁(Buo-ren Lin) <[email protected]>
1 parent 3ffcc8a commit 59dcbcc

File tree

1 file changed

+53
-37
lines changed

1 file changed

+53
-37
lines changed

dev-assets/functions.sh

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,53 @@ refresh_package_manager_local_cache(){
506506
esac
507507
}
508508

509+
detect_local_region_code(){
510+
local -a curl_opts=(
511+
# Return non-zero exit status when HTTP error occurs
512+
--fail
513+
514+
# Do not show progress meter but keep error messages
515+
--silent
516+
--show-error
517+
)
518+
local ip_reverse_lookup_service_response
519+
if ! ip_reverse_lookup_service_response="$(
520+
curl \
521+
"${curl_opts[@]}" \
522+
https://ipinfo.io/json
523+
)"; then
524+
printf \
525+
'%s: Warning: Unable to detect the local region code(IP address reverse lookup service not available).\n' \
526+
"${FUNCNAME[0]}" \
527+
1>&2
528+
return 3
529+
fi
530+
531+
local region_code
532+
local -a grep_opts=(
533+
--perl-regexp
534+
--only-matching
535+
)
536+
if ! region_code="$(
537+
grep \
538+
"${grep_opts[@]}" \
539+
'(?<="country": ")[[:alpha:]]+' \
540+
<<<"${ip_reverse_lookup_service_response}"
541+
)"; then
542+
printf \
543+
'%s: Warning: Unable to parse out the local region code.\n' \
544+
"${FUNCNAME[0]}" \
545+
1>&2
546+
return 4
547+
fi
548+
549+
# The returned region code may be capitalized, normalize it.
550+
region_code="${region_code,,*}"
551+
552+
printf '%s' "${region_code}"
553+
return 0
554+
}
555+
509556
switch_ubuntu_local_mirror(){
510557
print_progress 'Switching to use the local Ubuntu software archive mirror to minimize package installation time...'
511558

@@ -537,50 +584,19 @@ switch_ubuntu_local_mirror(){
537584

538585
printf \
539586
'Info: Detecting local region code...\n'
540-
local -a curl_opts=(
541-
# Return non-zero exit status when HTTP error occurs
542-
--fail
543-
544-
# Do not show progress meter but keep error messages
545-
--silent
546-
--show-error
547-
)
548-
local ip_reverse_lookup_service_response region_code
549-
if ! ip_reverse_lookup_service_response="$(
550-
curl \
551-
"${curl_opts[@]}" \
552-
https://ipinfo.io/json
553-
)"; then
587+
local region_code
588+
if ! region_code="$(detect_local_region_code)"; then
554589
printf \
555-
'Warning: Unable to detect the local region code(IP address reverse lookup service not available), falling back to the default.\n' \
590+
'Warning: Unable to detect the local region code, falling back to the default.\n' \
556591
1>&2
557592
region_code=
558593
else
559-
local -a grep_opts=(
560-
--perl-regexp
561-
--only-matching
562-
)
563-
if ! region_code="$(
564-
grep \
565-
"${grep_opts[@]}" \
566-
'(?<="country": ")[[:alpha:]]+' \
567-
<<<"${ip_reverse_lookup_service_response}"
568-
)"; then
569-
printf \
570-
'Warning: Unable to query the local region code, falling back to default.\n' \
571-
1>&2
572-
region_code=
573-
else
574-
printf \
575-
'Info: Local region code determined to be "%s".\n' \
576-
"${region_code}"
577-
fi
594+
printf \
595+
'Info: Local region code determined to be "%s".\n' \
596+
"${region_code}"
578597
fi
579598

580599
if test -n "${region_code}"; then
581-
# The returned region code is capitalized, fixing it.
582-
region_code="${region_code,,*}"
583-
584600
printf \
585601
'Info: Checking whether the local Ubuntu archive mirror exists...\n'
586602
local -a curl_opts=(

0 commit comments

Comments
 (0)