torrent/nushell/tools/extractors.nu
2024-01-13 13:01:54 +03:00

35 lines
1.3 KiB
Plaintext

# Function to extract archives with different extensions.
export def extract [path:string] {
let $destdir = $path | path dirname
let $name = $path | split row '/' | last
if not ($path | path exists) {
print $"could not find ($path) to expand"
exit 1
}
let handlers = [ [extension command];
['tar\.bz2|tbz|tbz2' 'tar xvjf']
['tar\.gz|tgz' 'tar xvzf']
['tar\.xz|txz' 'tar xvf']
['tar\.Z' 'tar xvZf']
['bz2' 'bunzip2']
['deb' 'ar x']
['gz' 'gunzip']
['pkg' 'pkgutil --expand']
['rar' 'unrar x']
['tar' 'tar xvf']
['xz' 'xz --decompress']
['zip|war|jar|nupkg' 'unzip']
['Z' 'uncompress']
['7z' '7za x']
]
let maybe_handler = ($handlers | where $name =~ $'\.(($it.extension))$')
if ($maybe_handler | is-empty) {
error make { msg: "unsupported file extension" }
} else {
let handler = ($maybe_handler | first)
cd $destdir
nu -c ($handler.command + ' ' + $name)
}
}