Skip to content

Commit

Permalink
plugin/extract: shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelicWizard committed Mar 4, 2022
1 parent 32523a5 commit 1671df9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ plugins/available/colors.plugin.bash
plugins/available/direnv.plugin.bash
plugins/available/dirs.plugin.bash
plugins/available/docker-machine.plugin.bash
plugins/available/extract.plugin.bash
plugins/available/gif.plugin.bash
plugins/available/git-subrepo.plugin.bash
plugins/available/git.plugin.bash
Expand Down
29 changes: 17 additions & 12 deletions plugins/available/extract.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ about-plugin 'one command to extract them all...'

# extract file(s) from compressed status
function extract() {
local opt
local OPTIND=1
local opt OPTIND=1 verbose
local filename filedirname targetdirname
while getopts "hv" opt; do
case "$opt" in
h)
Expand All @@ -27,32 +27,37 @@ EOU
done
shift $((OPTIND - 1))

[ $# -eq 0 ] && extract -h && return 1
while [ $# -gt 0 ]; do
if [[ ! -f "$1" ]]; then
if [[ $# -eq 0 ]]; then
extract -h
return 1
fi

while [[ $# -gt 0 ]]; do
if [[ ! -f "${1:-}" ]]; then
echo "extract: '$1' is not a valid file" >&2
shift
continue
fi

local -r filename=$(basename -- $1)
local -r filedirname=$(dirname -- $1)
local targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< $filename)
if [ "$filename" = "$targetdirname" ]; then
local -r filename=$(basename -- "$1")
local -r filedirname=$(dirname -- "$1")
# shellcheck disable=SC2001 # we don't depend on `extglob`...
targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< "$filename")
if [[ "$filename" == "$targetdirname" ]]; then
# archive type either not supported or it doesn't need dir creation
targetdirname=""
else
mkdir -v "$filedirname/$targetdirname"
fi

if [ -f "$1" ]; then
if [[ -f "$1" ]]; then
case "$1" in
*.tar.bz2 | *.tbz | *.tbz2) tar "x${verbose}jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.gz | *.tgz) tar "x${verbose}zf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.xz | *.txz) tar "x${verbose}Jf" "$1" -C "$filedirname/$targetdirname" ;;
*.tar.Z) tar "x${verbose}Zf" "$1" -C "$filedirname/$targetdirname" ;;
*.bz2) bunzip2 "$1" ;;
*.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;;
*.deb) dpkg-deb -x"${verbose}" "$1" "${1:0:-4}" ;;
*.pax.gz)
gunzip "$1"
set -- "$@" "${1:0:-3}"
Expand All @@ -61,7 +66,7 @@ EOU
*.pax) pax -r -f "$1" ;;
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;;
*.rar) unrar x "$1" ;;
*.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;;
*.rpm) rpm2cpio "$1" | cpio -idm"${verbose}" ;;
*.tar) tar "x${verbose}f" "$1" -C "$filedirname/$targetdirname" ;;
*.xz) xz --decompress "$1" ;;
*.zip | *.war | *.jar | *.nupkg) unzip "$1" -d "$filedirname/$targetdirname" ;;
Expand Down

0 comments on commit 1671df9

Please sign in to comment.