nuscripts/tools/download.nu
2024-01-13 13:50:06 +03:00

87 lines
2.6 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)
}
}
export def "download_do" [ dest: string url: string , --minsize=1] {
let $destdir = $dest | path dirname
mkdir $destdir
print $"download url:($url) to dest:($dest)"
# Download the file
http get $url | save -f $dest
let minsize = $minsize * 1000000
if ($minsize > 0) {
let p = ls $dest
if (($p.size | into int).0 < $minsize) {
print $"The download was too small for ($dest)"
exit 1
} else {
print $"Download ok for ($dest)"
}
}
# echo $"File size: $file_size bytes"
}
export def "download_expand" [ dest: string url: string , --minsize=1] {
mkdir $dest
print $"download expand url:($url) to dest:($dest)"
let $file_name = $url | split row '/' | last
let $ext = $file_name | split row '.' | skip 1 | | str join "." | str downcase
let $dest2 = $"($dest)/downloadedfile.($ext)"
print $dest2
download_do $dest2 $url --minsize=$minsize
let $e = extract $dest2
}
# export def init_plugins [] {
# runonce run init_plugins {
# register bin/nu_plugin_dns
# register bin/nu_plugin_desktop_notifications
# register bin/nu_plugin_net
# register bin/nu_plugin_port_list
# register bin/nu_plugin_port_scan
# register bin/nu_plugin_query
# } --reset true
# }