[archive] Check unrar with commands array

“unrar-free returns the error code 1 when run without arguments, thus
failing the presence check.” as reported at sorin-ionescu/prezto#1383

Reformat code and use one-liners instead of if/then/fi. And we don’t
need to separate local definitions from assignments anymore starting
from zsh 5.2.
This commit is contained in:
Eric Nielsen 2018-02-07 18:27:59 -05:00
parent 2d1aea3b74
commit 6af7892f91
3 changed files with 12 additions and 29 deletions

View File

@ -2,8 +2,6 @@
# Creates archive files # Creates archive files
# #
local archive_name dir_to_archive
if (( ${#} != 2 )); then if (( ${#} != 2 )); then
print "usage: ${0} [archive_name.ext] [/path/to/include/in/archive]" >&2 print "usage: ${0} [archive_name.ext] [/path/to/include/in/archive]" >&2
return 1 return 1
@ -13,9 +11,9 @@ fi
# so we don't need any argc check here. # so we don't need any argc check here.
# strip the path, just in case one is provided for some reason # strip the path, just in case one is provided for some reason
archive_name="${1:t}" local archive_name="${1:t}"
# use absolute paths, and follow symlinks # use absolute paths, and follow symlinks
dir_to_archive="${2}" local dir_to_archive="${2}"
# if the directory doesn't exist, quit. Nothing to archive # if the directory doesn't exist, quit. Nothing to archive
if [[ ! -e "${dir_to_archive}" ]]; then if [[ ! -e "${dir_to_archive}" ]]; then
@ -29,8 +27,8 @@ fi
case "${archive_name}" in case "${archive_name}" in
(*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program="gzip" "${dir_to_archive}" ;; (*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program="gzip" "${dir_to_archive}" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program="bzip2" "${dir_to_archive}" ;; (*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program="bzip2" "${dir_to_archive}" ;;
(*.tar.xz|*.txz) tar --xz --help &> /dev/null && tar -cvJf "${archive_name}" "${dir_to_archive}" ;; (*.tar.xz|*.txz) tar --xz --help &>/dev/null && tar -cvJf "${archive_name}" "${dir_to_archive}" ;;
(*.tar.lzma|*.tlz) tar --lzma --help &> /dev/null && tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;; (*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;;
(*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;; (*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;;
(*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;; (*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;;
(*.rar) rar a "${archive_name}" "${dir_to_archive}" ;; (*.rar) rar a "${archive_name}" "${dir_to_archive}" ;;

View File

@ -2,8 +2,6 @@
# Unarchives files # Unarchives files
# #
local archive_name
if (( ${#} != 1 )); then if (( ${#} != 1 )); then
print "usage: ${0} [archive.ext]" >&2 print "usage: ${0} [archive.ext]" >&2
return 1 return 1
@ -15,7 +13,7 @@ if [[ ! -s ${1} ]]; then
fi fi
# strip the path, just in case one is provided for some reason # strip the path, just in case one is provided for some reason
archive_name="${1:t}" local archive_name="${1:t}"
# using unpigz/pbunzip2 provides little to decompression time; the benefit is mainly in compression time. # using unpigz/pbunzip2 provides little to decompression time; the benefit is mainly in compression time.
# setting it as an alias in the init.zsh file should be sufficient here. # setting it as an alias in the init.zsh file should be sufficient here.
@ -23,9 +21,9 @@ archive_name="${1:t}"
case "${archive_name}" in case "${archive_name}" in
(*.tar.gz|*.tgz) tar -xvzf "${archive_name}" ;; (*.tar.gz|*.tgz) tar -xvzf "${archive_name}" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar -xvjf "${archive_name}" ;; (*.tar.bz2|*.tbz|*.tbz2) tar -xvjf "${archive_name}" ;;
(*.tar.xz|*.txz) tar --xz --help &> /dev/null && tar --xz -xvf "${archive_name}" \ (*.tar.xz|*.txz) tar --xz --help &>/dev/null && tar --xz -xvf "${archive_name}" \
|| xzcat "${archive_name}" | tar xvf - ;; || xzcat "${archive_name}" | tar xvf - ;;
(*.tar.zma|*.tlz) tar --lzma --help &> /dev/null && tar --lzma -xvf "${archive_name}" \ (*.tar.zma|*.tlz) tar --lzma --help &>/dev/null && tar --lzma -xvf "${archive_name}" \
|| lzcat "${archive_name}" | tar xvf - ;; || lzcat "${archive_name}" | tar xvf - ;;
(*.tar) tar xvf "${archive_name}" ;; (*.tar) tar xvf "${archive_name}" ;;
(*.gz) gunzip "${archive_name}" ;; (*.gz) gunzip "${archive_name}" ;;
@ -34,8 +32,7 @@ case "${archive_name}" in
(*.lzma) unlzma "${archive_name}" ;; (*.lzma) unlzma "${archive_name}" ;;
(*.Z) uncompress "${archive_name}" ;; (*.Z) uncompress "${archive_name}" ;;
(*.zip) unzip "${archive_name}";; (*.zip) unzip "${archive_name}";;
(*.rar) unrar &> /dev/null \ (*.rar) (( $+{commands[unrar]} )) && unrar x -ad "${archive_name}" \
&& unrar x -ad "${archive_name}" \
|| rar x -ad "${archive_name}" ;; || rar x -ad "${archive_name}" ;;
(*.7z|*.001) 7za x "${archive_name}" ;; (*.7z|*.001) 7za x "${archive_name}" ;;
(*) print "${0}: unknown archive type: ${archive_name}" ;; (*) print "${0}: unknown archive type: ${archive_name}" ;;

View File

@ -7,23 +7,11 @@
# #
# pigz # pigz
# #
(( ${+commands[pigz]} )) && alias gzip='pigz'
if (( ${+commands[pigz]} )); then (( ${+commands[unpigz]} )) && alias gunzip='unpigz'
alias gzip='pigz'
fi
if (( ${+commands[unpigz]} )); then
alias gunzip='unpigz'
fi
# #
# pbzip2 # pbzip2
# #
(( ${+commands[pbzip2]} )) && alias bzip2='pbzip2'
if (( ${+commands[pbzip2]} )); then (( ${+commands[pbunzip2]} )) && alias bunzip2='pbunzip2'
alias bzip2='pbzip2'
fi
if (( ${+commands[pbunzip2]} )); then
alias bunzip2='pbunzip2'
fi