Skip to content

Commit f22b70d

Browse files
committed
plugin/extract: shellcheck
1 parent 8c6f306 commit f22b70d

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

clean_files.txt

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ plugins/available/colors.plugin.bash
9898
plugins/available/direnv.plugin.bash
9999
plugins/available/dirs.plugin.bash
100100
plugins/available/docker-machine.plugin.bash
101+
plugins/available/extract.plugin.bash
101102
plugins/available/gif.plugin.bash
102103
plugins/available/git-subrepo.plugin.bash
103104
plugins/available/git.plugin.bash

plugins/available/extract.plugin.bash

+17-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ about-plugin 'one command to extract them all...'
33

44
# extract file(s) from compressed status
55
function extract() {
6-
local opt
7-
local OPTIND=1
6+
local opt OPTIND=1 verbose
7+
local filename filedirname targetdirname
88
while getopts "hv" opt; do
99
case "$opt" in
1010
h)
@@ -27,32 +27,37 @@ EOU
2727
done
2828
shift $((OPTIND - 1))
2929

30-
[ $# -eq 0 ] && extract -h && return 1
31-
while [ $# -gt 0 ]; do
32-
if [[ ! -f "$1" ]]; then
30+
if [[ $# -eq 0 ]]; then
31+
extract -h
32+
return 1
33+
fi
34+
35+
while [[ $# -gt 0 ]]; do
36+
if [[ ! -f "${1:-}" ]]; then
3337
echo "extract: '$1' is not a valid file" >&2
3438
shift
3539
continue
3640
fi
3741

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

48-
if [ -f "$1" ]; then
53+
if [[ -f "$1" ]]; then
4954
case "$1" in
5055
*.tar.bz2 | *.tbz | *.tbz2) tar "x${verbose}jf" "$1" -C "$filedirname/$targetdirname" ;;
5156
*.tar.gz | *.tgz) tar "x${verbose}zf" "$1" -C "$filedirname/$targetdirname" ;;
5257
*.tar.xz | *.txz) tar "x${verbose}Jf" "$1" -C "$filedirname/$targetdirname" ;;
5358
*.tar.Z) tar "x${verbose}Zf" "$1" -C "$filedirname/$targetdirname" ;;
5459
*.bz2) bunzip2 "$1" ;;
55-
*.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;;
60+
*.deb) dpkg-deb -x"${verbose}" "$1" "${1:0:-4}" ;;
5661
*.pax.gz)
5762
gunzip "$1"
5863
set -- "$@" "${1:0:-3}"
@@ -61,7 +66,7 @@ EOU
6166
*.pax) pax -r -f "$1" ;;
6267
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;;
6368
*.rar) unrar x "$1" ;;
64-
*.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;;
69+
*.rpm) rpm2cpio "$1" | cpio -idm"${verbose}" ;;
6570
*.tar) tar "x${verbose}f" "$1" -C "$filedirname/$targetdirname" ;;
6671
*.xz) xz --decompress "$1" ;;
6772
*.zip | *.war | *.jar | *.nupkg) unzip "$1" -d "$filedirname/$targetdirname" ;;

0 commit comments

Comments
 (0)