From 6af7892f9154b42aa9be042bf69fcf95ca4e9def Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Wed, 7 Feb 2018 18:27:59 -0500 Subject: [PATCH] [archive] Check unrar with commands array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit “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. --- modules/archive/functions/archive | 10 ++++------ modules/archive/functions/unarchive | 11 ++++------- modules/archive/init.zsh | 20 ++++---------------- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/modules/archive/functions/archive b/modules/archive/functions/archive index 9c5316e..7200cb0 100644 --- a/modules/archive/functions/archive +++ b/modules/archive/functions/archive @@ -2,8 +2,6 @@ # Creates archive files # -local archive_name dir_to_archive - if (( ${#} != 2 )); then print "usage: ${0} [archive_name.ext] [/path/to/include/in/archive]" >&2 return 1 @@ -13,9 +11,9 @@ fi # so we don't need any argc check here. # 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 -dir_to_archive="${2}" +local dir_to_archive="${2}" # if the directory doesn't exist, quit. Nothing to archive if [[ ! -e "${dir_to_archive}" ]]; then @@ -29,8 +27,8 @@ fi case "${archive_name}" in (*.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.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.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) tar -cvf "${archive_name}" "${dir_to_archive}" ;; (*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;; (*.rar) rar a "${archive_name}" "${dir_to_archive}" ;; diff --git a/modules/archive/functions/unarchive b/modules/archive/functions/unarchive index 1dee454..000cbdc 100644 --- a/modules/archive/functions/unarchive +++ b/modules/archive/functions/unarchive @@ -2,8 +2,6 @@ # Unarchives files # -local archive_name - if (( ${#} != 1 )); then print "usage: ${0} [archive.ext]" >&2 return 1 @@ -15,7 +13,7 @@ if [[ ! -s ${1} ]]; then fi # 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. # 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 (*.tar.gz|*.tgz) tar -xvzf "${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 - ;; - (*.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 - ;; (*.tar) tar xvf "${archive_name}" ;; (*.gz) gunzip "${archive_name}" ;; @@ -34,8 +32,7 @@ case "${archive_name}" in (*.lzma) unlzma "${archive_name}" ;; (*.Z) uncompress "${archive_name}" ;; (*.zip) unzip "${archive_name}";; - (*.rar) unrar &> /dev/null \ - && unrar x -ad "${archive_name}" \ + (*.rar) (( $+{commands[unrar]} )) && unrar x -ad "${archive_name}" \ || rar x -ad "${archive_name}" ;; (*.7z|*.001) 7za x "${archive_name}" ;; (*) print "${0}: unknown archive type: ${archive_name}" ;; diff --git a/modules/archive/init.zsh b/modules/archive/init.zsh index d135c2d..d3c43fd 100644 --- a/modules/archive/init.zsh +++ b/modules/archive/init.zsh @@ -7,23 +7,11 @@ # # pigz # - -if (( ${+commands[pigz]} )); then - alias gzip='pigz' -fi - -if (( ${+commands[unpigz]} )); then - alias gunzip='unpigz' -fi +(( ${+commands[pigz]} )) && alias gzip='pigz' +(( ${+commands[unpigz]} )) && alias gunzip='unpigz' # # pbzip2 # - -if (( ${+commands[pbzip2]} )); then - alias bzip2='pbzip2' -fi - -if (( ${+commands[pbunzip2]} )); then - alias bunzip2='pbunzip2' -fi +(( ${+commands[pbzip2]} )) && alias bzip2='pbzip2' +(( ${+commands[pbunzip2]} )) && alias bunzip2='pbunzip2'