Skip to content

Commit bf08f8f

Browse files
committed
eng/scripts/get-aspire-cli.sh: add helper functions (get_runtime_identifier, new_temp_dir, remove_temp_dir) and update usages (RID + temp dir)
1 parent c45c090 commit bf08f8f

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

eng/scripts/get-aspire-cli.sh

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,67 @@ detect_architecture() {
239239
esac
240240
}
241241

242+
# Compute runtime identifier "<os>-<arch>" from optional inputs
243+
get_runtime_identifier() {
244+
local _os="$1"
245+
local _arch="$2"
246+
local os_val arch_val
247+
248+
if [[ -z "$_os" ]]; then
249+
if ! os_val=$(detect_os); then
250+
say_error "Unsupported operating system. Current platform: $(uname -s)"
251+
return 1
252+
fi
253+
else
254+
os_val="$_os"
255+
fi
256+
257+
if [[ -z "$_arch" ]]; then
258+
if ! arch_val=$(get_cli_architecture_from_architecture "<auto>"); then
259+
return 1
260+
fi
261+
else
262+
if ! arch_val=$(get_cli_architecture_from_architecture "$_arch"); then
263+
return 1
264+
fi
265+
fi
266+
267+
printf "%s-%s" "$os_val" "$arch_val"
268+
}
269+
270+
# Create a temporary directory with a prefix. Honors DRY_RUN
271+
new_temp_dir() {
272+
local prefix="$1"
273+
if [[ "$DRY_RUN" == true ]]; then
274+
printf "/tmp/%s-whatif" "$prefix"
275+
return 0
276+
fi
277+
local dir
278+
if ! dir=$(mktemp -d -t "${prefix}-XXXXXXXX"); then
279+
say_error "Unable to create temporary directory"
280+
return 1
281+
fi
282+
say_verbose "Creating temporary directory: $dir"
283+
printf "%s" "$dir"
284+
}
285+
286+
# Remove a temporary directory unless KEEP_ARCHIVE is set. Honors DRY_RUN
287+
remove_temp_dir() {
288+
local dir="$1"
289+
if [[ -z "$dir" || ! -d "$dir" ]]; then
290+
return 0
291+
fi
292+
if [[ "$DRY_RUN" == true ]]; then
293+
return 0
294+
fi
295+
if [[ "$KEEP_ARCHIVE" != true ]]; then
296+
say_verbose "Cleaning up temporary files..."
297+
rm -rf "$dir" || say_warn "Failed to clean up temporary directory: $dir"
298+
else
299+
printf "Archive files kept in: %s\n" "$dir"
300+
fi
301+
}
302+
242303
# Common function for HTTP requests with centralized configuration
243304
secure_curl() {
244305
local url="$1"
@@ -611,28 +672,11 @@ download_and_install_archive() {
611672
local os arch runtimeIdentifier url filename checksum_url checksum_filename extension
612673
local cli_exe cli_path
613674

614-
# Detect OS and architecture if not provided
615-
if [[ -z "$OS" ]]; then
616-
if ! os=$(detect_os); then
617-
say_error "Unsupported operating system. Current platform: $(uname -s)"
618-
return 1
619-
fi
620-
else
621-
os="$OS"
622-
fi
623-
624-
if [[ -z "$ARCH" ]]; then
625-
if ! arch=$(get_cli_architecture_from_architecture "<auto>"); then
626-
return 1
627-
fi
628-
else
629-
if ! arch=$(get_cli_architecture_from_architecture "$ARCH"); then
630-
return 1
631-
fi
675+
# Construct the runtime identifier from inputs (or detect)
676+
if ! runtimeIdentifier=$(get_runtime_identifier "${OS}" "${ARCH}"); then
677+
return 1
632678
fi
633-
634-
# Construct the runtime identifier
635-
runtimeIdentifier="${os}-${arch}"
679+
os="${runtimeIdentifier%%-*}"
636680

637681
# Determine file extension based on OS
638682
if [[ "$os" == "win" ]]; then
@@ -714,34 +758,9 @@ else
714758
INSTALL_PATH_UNEXPANDED="$INSTALL_PATH"
715759
fi
716760

717-
# Create a temporary directory for downloads
718-
if [[ "$DRY_RUN" == true ]]; then
719-
temp_dir="/tmp/aspire-cli-dry-run"
720-
else
721-
temp_dir=$(mktemp -d -t aspire-cli-download-XXXXXXXX)
722-
say_verbose "Creating temporary directory: $temp_dir"
723-
fi
724-
725-
# Cleanup function for temporary directory
726-
cleanup() {
727-
# shellcheck disable=SC2317 # Function is called via trap
728-
if [[ "$DRY_RUN" == true ]]; then
729-
# No cleanup needed in dry-run mode
730-
return 0
731-
fi
732-
733-
if [[ -n "${temp_dir:-}" ]] && [[ -d "$temp_dir" ]]; then
734-
if [[ "$KEEP_ARCHIVE" != true ]]; then
735-
say_verbose "Cleaning up temporary files..."
736-
rm -rf "$temp_dir" || say_warn "Failed to clean up temporary directory: $temp_dir"
737-
else
738-
printf "Archive files kept in: %s\n" "$temp_dir"
739-
fi
740-
fi
741-
}
742-
743-
# Set trap for cleanup on exit
744-
trap cleanup EXIT
761+
# Create a temporary directory for downloads and set cleanup trap
762+
temp_dir=$(new_temp_dir "aspire-cli-download")
763+
trap 'remove_temp_dir "$temp_dir"' EXIT
745764

746765
# Download and install the archive
747766
if ! download_and_install_archive "$temp_dir"; then

0 commit comments

Comments
 (0)