Skip to content

Commit 267fa2b

Browse files
committed
wip
1 parent cae0e45 commit 267fa2b

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

eng/scripts/get-aspire-cli.sh

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ get_runtime_identifier() {
267267
printf "%s" "${target_os}-${target_arch}"
268268
}
269269

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+
270303
# Function to install/unpack archive files
271304
install_archive() {
272305
local archive_file="$1"
@@ -286,30 +319,26 @@ install_archive() {
286319
fi
287320

288321
# Check archive format and extract accordingly
289-
if [[ "$archive_file" == *.zip ]]; then
290-
# Use unzip for ZIP files
322+
if [[ "$archive_file" =~ \.zip$ ]]; then
291323
if ! command -v unzip >/dev/null 2>&1; then
292324
say_error "unzip command not found. Please install unzip to extract ZIP files."
293325
return 1
294326
fi
295-
296327
if ! unzip -o "$archive_file" -d "$destination_path"; then
297328
say_error "Failed to extract ZIP archive: $archive_file"
298329
return 1
299330
fi
300-
elif [[ "$archive_file" == *.tar.gz ]]; then
301-
# Use tar for tar.gz files on Unix systems
331+
elif [[ "$archive_file" =~ \.tar\.gz$ ]]; then
302332
if ! command -v tar >/dev/null 2>&1; then
303333
say_error "tar command not found. Please install tar to extract tar.gz files."
304334
return 1
305335
fi
306-
307336
if ! tar -xzf "$archive_file" -C "$destination_path"; then
308337
say_error "Failed to extract tar.gz archive: $archive_file"
309338
return 1
310339
fi
311340
else
312-
say_error "Unsupported archive format: $archive_file. Only .zip and .tar.gz are supported."
341+
say_error "Unsupported archive format: $archive_file. Only .zip and .tar.gz files are supported."
313342
return 1
314343
fi
315344

@@ -436,24 +465,6 @@ add_to_shell_profile() {
436465
return 0
437466
}
438467

439-
# Cleanup function for temporary directory
440-
cleanup() {
441-
# shellcheck disable=SC2317 # Function is called via trap
442-
if [[ "$DRY_RUN" == true ]]; then
443-
# No cleanup needed in dry-run mode
444-
return 0
445-
fi
446-
447-
if [[ -n "${temp_dir:-}" ]] && [[ -d "$temp_dir" ]]; then
448-
if [[ "$KEEP_ARCHIVE" != true ]]; then
449-
say_verbose "Cleaning up temporary files..."
450-
rm -rf "$temp_dir" || say_warn "Failed to clean up temporary directory: $temp_dir"
451-
else
452-
printf "Archive files kept in: %s\n" "$temp_dir"
453-
fi
454-
fi
455-
}
456-
457468
# =============================================================================
458469
# END: Shared code
459470
# =============================================================================
@@ -709,7 +720,7 @@ download_and_install_archive() {
709720
fi
710721

711722
# Install the archive
712-
if ! install_archive "$filename" "$INSTALL_PATH" "$target_os"; then
723+
if ! install_archive "$filename" "$INSTALL_PATH"; then
713724
return 1
714725
fi
715726

@@ -754,16 +765,9 @@ else
754765
INSTALL_PATH_UNEXPANDED="$INSTALL_PATH"
755766
fi
756767

757-
# Create a temporary directory for downloads
758-
if [[ "$DRY_RUN" == true ]]; then
759-
temp_dir="/tmp/aspire-cli-dry-run"
760-
else
761-
temp_dir=$(mktemp -d -t aspire-cli-download-XXXXXXXX)
762-
say_verbose "Creating temporary directory: $temp_dir"
763-
fi
764-
765-
# Set trap for cleanup on exit
766-
trap cleanup EXIT
768+
# Create a temporary directory for downloads and set cleanup trap
769+
temp_dir=$(new_temp_dir "aspire-cli-download")
770+
trap 'remove_temp_dir "$temp_dir"' EXIT
767771

768772
# Download and install the archive
769773
if ! download_and_install_archive "$temp_dir" "$OS_ARG" "$ARCH_ARG"; then

0 commit comments

Comments
 (0)