@@ -288,6 +288,39 @@ get_runtime_identifier() {
288
288
printf " %s" " ${target_os} -${target_arch} "
289
289
}
290
290
291
+ # Create a temporary directory with a prefix. Honors DRY_RUN
292
+ new_temp_dir () {
293
+ local prefix=" $1 "
294
+ if [[ " $DRY_RUN " == true ]]; then
295
+ printf " /tmp/%s-whatif" " $prefix "
296
+ return 0
297
+ fi
298
+ local dir
299
+ if ! dir=$( mktemp -d -t " ${prefix} -XXXXXXXX" ) ; then
300
+ say_error " Unable to create temporary directory"
301
+ return 1
302
+ fi
303
+ say_verbose " Creating temporary directory: $dir "
304
+ printf " %s" " $dir "
305
+ }
306
+
307
+ # Remove a temporary directory unless KEEP_ARCHIVE is set. Honors DRY_RUN
308
+ remove_temp_dir () {
309
+ local dir=" $1 "
310
+ if [[ -z " $dir " || ! -d " $dir " ]]; then
311
+ return 0
312
+ fi
313
+ if [[ " $DRY_RUN " == true ]]; then
314
+ return 0
315
+ fi
316
+ if [[ " $KEEP_ARCHIVE " != true ]]; then
317
+ say_verbose " Cleaning up temporary files..."
318
+ rm -rf " $dir " || say_warn " Failed to clean up temporary directory: $dir "
319
+ else
320
+ printf " Archive files kept in: %s\n" " $dir "
321
+ fi
322
+ }
323
+
291
324
# Function to install/unpack archive files
292
325
install_archive () {
293
326
local archive_file=" $1 "
@@ -307,30 +340,26 @@ install_archive() {
307
340
fi
308
341
309
342
# Check archive format and extract accordingly
310
- if [[ " $archive_file " == * .zip ]]; then
311
- # Use unzip for ZIP files
343
+ if [[ " $archive_file " =~ \. zip$ ]]; then
312
344
if ! command -v unzip > /dev/null 2>&1 ; then
313
345
say_error " unzip command not found. Please install unzip to extract ZIP files."
314
346
return 1
315
347
fi
316
-
317
348
if ! unzip -o " $archive_file " -d " $destination_path " ; then
318
349
say_error " Failed to extract ZIP archive: $archive_file "
319
350
return 1
320
351
fi
321
- elif [[ " $archive_file " == * .tar.gz ]]; then
322
- # Use tar for tar.gz files on Unix systems
352
+ elif [[ " $archive_file " =~ \. tar\. gz$ ]]; then
323
353
if ! command -v tar > /dev/null 2>&1 ; then
324
354
say_error " tar command not found. Please install tar to extract tar.gz files."
325
355
return 1
326
356
fi
327
-
328
357
if ! tar -xzf " $archive_file " -C " $destination_path " ; then
329
358
say_error " Failed to extract tar.gz archive: $archive_file "
330
359
return 1
331
360
fi
332
361
else
333
- say_error " Unsupported archive format: $archive_file . Only .zip and .tar.gz are supported."
362
+ say_error " Unsupported archive format: $archive_file . Only .zip and .tar.gz files are supported."
334
363
return 1
335
364
fi
336
365
@@ -457,24 +486,6 @@ add_to_shell_profile() {
457
486
return 0
458
487
}
459
488
460
- # Cleanup function for temporary directory
461
- cleanup () {
462
- # shellcheck disable=SC2317 # Function is called via trap
463
- if [[ " $DRY_RUN " == true ]]; then
464
- # No cleanup needed in dry-run mode
465
- return 0
466
- fi
467
-
468
- if [[ -n " ${temp_dir:- } " ]] && [[ -d " $temp_dir " ]]; then
469
- if [[ " $KEEP_ARCHIVE " != true ]]; then
470
- say_verbose " Cleaning up temporary files..."
471
- rm -rf " $temp_dir " || say_warn " Failed to clean up temporary directory: $temp_dir "
472
- else
473
- printf " Archive files kept in: %s\n" " $temp_dir "
474
- fi
475
- fi
476
- }
477
-
478
489
# =============================================================================
479
490
# END: Shared code
480
491
# =============================================================================
0 commit comments