#!/bin/bash set -euo pipefail # Best practice: fail fast on errors, unset vars, pipeline failures FILE="${1:-}" FILE_TYPE="${2:-auto}" OUTPUT_DIR="${3:-}" PREFIX_ARGS="${4:-}" ADDITIONAL_ARGS="${5:-}" # Validate that a file was provided if [[ -z "$FILE" ]]; then echo "Error: No file specified." >&2 exit 1 fi # Resolve relative paths and check if file exists if [[ ! -f "$FILE" ]]; then echo "Error: File not found: '$FILE'" >&2 exit 1 fi # Auto-detect file type if needed if [[ "$FILE_TYPE" == "auto" ]]; then if [[ "$FILE" =~ \.(tar\.gz|tgz)$ ]]; then FILE_TYPE="tgz" elif [[ "$FILE" =~ \.zip$ ]]; then FILE_TYPE="zip" else echo "Error: Unable to auto-detect file type from extension '$FILE'. Specify 'zip' or 'tgz'." >&2 exit 1 fi fi # Ensure output directory exists if specified if [[ -n "$OUTPUT_DIR" ]]; then mkdir -p "$OUTPUT_DIR" fi # Build the extraction command case "$FILE_TYPE" in tgz) COMMAND=(tar $PREFIX_ARGS -xzf "$FILE" $ADDITIONAL_ARGS) [[ -n "$OUTPUT_DIR" ]] && COMMAND+=(-C "$OUTPUT_DIR") ;; zip) COMMAND=(unzip $PREFIX_ARGS -o "$FILE" $ADDITIONAL_ARGS) [[ -n "$OUTPUT_DIR" ]] && COMMAND+=(-d "$OUTPUT_DIR") ;; *) echo "Error: Invalid fileType '$FILE_TYPE'. Must be 'auto', 'zip', or 'tgz'." >&2 exit 1 ;; esac # Output the built command (for GitHub Actions step output or local debugging) if [[ -z "${GITHUB_OUTPUT-}" ]]; then GITHUB_OUTPUT="/dev/stdout" fi echo "command=${COMMAND[*]}" >> "$GITHUB_OUTPUT" # Execute the command safely using an array (prevents word splitting issues) exec "${COMMAND[@]}"