This commit is contained in:
root 2026-03-16 12:19:11 -03:00
commit 73ff9ee8ee
No known key found for this signature in database
31 changed files with 4906 additions and 0 deletions

79
templates/extract.sh.nix Normal file
View file

@ -0,0 +1,79 @@
{ pkgs }:
pkgs.writeShellScriptBin "extract" ''
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
log_info() { echo -e "''${GREEN}[INFO]''${NC} $1"; }
log_warn() { echo -e "''${YELLOW}[WARN]''${NC} $1"; }
log_error() { echo -e "''${RED}[ERROR]''${NC} $1"; }
usage() {
echo "Usage: $0 <archive1> [archive2] ..."
echo "Extracts zip and 7z files into folders named after the archive."
exit 1
}
extract_file() {
local archive="$1"
local archive_name
local output_dir
local extension
if [[ ! -f "$archive" ]]; then
log_error "File not found: $archive"
return 1
fi
archive_name=$(${pkgs.coreutils}/bin/basename "$archive")
extension="''${archive_name##*.}"
extension="''${extension,,}"
if [[ "$extension" != "zip" && "$extension" != "7z" ]]; then
log_error "Unsupported file type: $archive (only .zip and .7z supported)"
return 1
fi
output_dir="''${archive%.*}"
${pkgs.coreutils}/bin/mkdir -p "$output_dir"
log_info "Extracting: $archive -> $output_dir/"
if ${pkgs.p7zip}/bin/7z x "$archive" -o"$output_dir" -y >/dev/null 2>&1; then
log_info "Successfully extracted with 7zip: $archive"
return 0
fi
log_warn "7zip failed for: $archive"
if [[ "$extension" == "zip" ]]; then
if ${pkgs.unzip}/bin/unzip -o "$archive" -d "$output_dir" >/dev/null 2>&1; then
log_info "Successfully extracted with unzip: $archive"
return 0
fi
log_error "unzip also failed for: $archive"
return 1
fi
log_error "Failed to extract: $archive"
return 1
}
if [[ $# -eq 0 ]]; then
usage
fi
exit_code=0
for archive in "$@"; do
if ! extract_file "$archive"; then
exit_code=1
fi
done
exit $exit_code
''