@@ -267,6 +267,39 @@ get_runtime_identifier() {
267
267
printf " %s" " ${target_os} -${target_arch} "
268
268
}
269
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
+
270
303
# Function to install/unpack archive files
271
304
install_archive () {
272
305
local archive_file=" $1 "
@@ -286,30 +319,26 @@ install_archive() {
286
319
fi
287
320
288
321
# Check archive format and extract accordingly
289
- if [[ " $archive_file " == * .zip ]]; then
290
- # Use unzip for ZIP files
322
+ if [[ " $archive_file " =~ \. zip$ ]]; then
291
323
if ! command -v unzip > /dev/null 2>&1 ; then
292
324
say_error " unzip command not found. Please install unzip to extract ZIP files."
293
325
return 1
294
326
fi
295
-
296
327
if ! unzip -o " $archive_file " -d " $destination_path " ; then
297
328
say_error " Failed to extract ZIP archive: $archive_file "
298
329
return 1
299
330
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
302
332
if ! command -v tar > /dev/null 2>&1 ; then
303
333
say_error " tar command not found. Please install tar to extract tar.gz files."
304
334
return 1
305
335
fi
306
-
307
336
if ! tar -xzf " $archive_file " -C " $destination_path " ; then
308
337
say_error " Failed to extract tar.gz archive: $archive_file "
309
338
return 1
310
339
fi
311
340
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."
313
342
return 1
314
343
fi
315
344
@@ -436,24 +465,6 @@ add_to_shell_profile() {
436
465
return 0
437
466
}
438
467
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
-
457
468
# =============================================================================
458
469
# END: Shared code
459
470
# =============================================================================
@@ -709,7 +720,7 @@ download_and_install_archive() {
709
720
fi
710
721
711
722
# Install the archive
712
- if ! install_archive " $filename " " $INSTALL_PATH " " $target_os " ; then
723
+ if ! install_archive " $filename " " $INSTALL_PATH " ; then
713
724
return 1
714
725
fi
715
726
@@ -754,16 +765,9 @@ else
754
765
INSTALL_PATH_UNEXPANDED=" $INSTALL_PATH "
755
766
fi
756
767
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
767
771
768
772
# Download and install the archive
769
773
if ! download_and_install_archive " $temp_dir " " $OS_ARG " " $ARCH_ARG " ; then
0 commit comments