mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-04-17 04:29:14 +02:00
feat(extract): added functionality to extract to specific directory
This commit is contained in:
parent
f6a7bbf81a
commit
ac1668ffe5
2 changed files with 102 additions and 108 deletions
|
|
@ -6,12 +6,15 @@ wide variety of archive filetypes.
|
|||
This way you don't have to know what specific command extracts a file, you just do `extract <filename>` and
|
||||
the function takes care of the rest.
|
||||
|
||||
|
||||
To use it, add `extract` to the plugins array in your zshrc file:
|
||||
|
||||
```zsh
|
||||
plugins=(... extract)
|
||||
```
|
||||
|
||||
To view a short explanations of the flags in this plugin, type `extract` into your terminal after installation is completed.
|
||||
|
||||
## Supported file extensions
|
||||
|
||||
| Extension | Description |
|
||||
|
|
|
|||
|
|
@ -3,50 +3,69 @@ alias x=extract
|
|||
extract() {
|
||||
setopt localoptions noautopushd
|
||||
|
||||
# Print explanation if "extract" is called with no args
|
||||
if (( $# == 0 )); then
|
||||
cat >&2 <<'EOF'
|
||||
Usage: extract [-option] [file ...]
|
||||
Usage: extract [options] [files...]
|
||||
|
||||
Options:
|
||||
-r, --remove Remove archive after unpacking.
|
||||
-r, --remove Remove archive(s) after unpacking.
|
||||
-t, --to-directory [DIRECTORY NAME] Extract into an existing directory.
|
||||
EOF
|
||||
return
|
||||
fi
|
||||
|
||||
local remove_archive=1
|
||||
# needed to set internal states by searching CLI args for flags
|
||||
local remove_archive=0
|
||||
local extract_dir=""
|
||||
|
||||
while :
|
||||
do
|
||||
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
|
||||
remove_archive=0
|
||||
remove_archive=1
|
||||
shift
|
||||
continue
|
||||
fi
|
||||
|
||||
local pwd="$PWD"
|
||||
# this sets the extraction directory to the argument after the -t flag
|
||||
if [[ "$1" == "-t" ]] || [[ "$1" == "--to-directory" ]]; then
|
||||
shift
|
||||
if [[ ! -d "$1" ]]; then
|
||||
echo "extract: specified output directory $1 is not a valid directory." >&2
|
||||
return
|
||||
fi
|
||||
extract_dir="$1"
|
||||
shift
|
||||
continue
|
||||
fi
|
||||
|
||||
# if none of the flags were found in the first arg, break the loop
|
||||
break
|
||||
done
|
||||
|
||||
# variables used for saving the current working directory and the
|
||||
# directory where the archive will be unpacked
|
||||
local work_dir="$PWD"
|
||||
|
||||
while (( $# > 0 )); do
|
||||
|
||||
if [[ ! -f "$1" ]]; then
|
||||
echo "extract: '$1' is not a valid file" >&2
|
||||
shift
|
||||
continue
|
||||
fi
|
||||
|
||||
local success=0
|
||||
local file="$1" full_path="${1:A}"
|
||||
local extract_dir="${1:t:r}"
|
||||
|
||||
# Remove the .tar extension if the file name is .tar.*
|
||||
if [[ $extract_dir =~ '\.tar$' ]]; then
|
||||
extract_dir="${extract_dir:r}"
|
||||
if [[ $extract_dir == "" ]]; then
|
||||
extract_dir="$PWD"
|
||||
fi
|
||||
|
||||
# If there's a file or directory with the same name as the archive
|
||||
# add a random string to the end of the extract directory
|
||||
if [[ -e "$extract_dir" ]]; then
|
||||
local rnd="${(L)"${$(( [##36]$RANDOM*$RANDOM ))}":1:5}"
|
||||
extract_dir="${extract_dir}-${rnd}"
|
||||
fi
|
||||
local file="$1"
|
||||
local full_path="${1:A}"
|
||||
|
||||
# Create an extraction directory based on the file name
|
||||
command mkdir -p "$extract_dir"
|
||||
builtin cd -q "$extract_dir"
|
||||
echo "extract: extracting to $extract_dir" >&2
|
||||
|
||||
local success=0
|
||||
case "${file:l}" in
|
||||
(*.tar.gz|*.tgz)
|
||||
(( $+commands[pigz] )) && { tar -I pigz -xvf "$full_path" } || tar zxvf "$full_path" ;;
|
||||
|
|
@ -95,43 +114,15 @@ EOF
|
|||
(*)
|
||||
echo "extract: '$file' cannot be extracted" >&2
|
||||
success=1 ;;
|
||||
esac
|
||||
esac # end case
|
||||
|
||||
|
||||
(( success = success > 0 ? success : $? ))
|
||||
(( success == 0 && remove_archive == 0 )) && command rm "$full_path"
|
||||
shift
|
||||
(( success == 0 && remove_archive == 1 )) && command rm "$full_path"
|
||||
|
||||
# Go back to original working directory
|
||||
builtin cd -q "$pwd"
|
||||
builtin cd -q "${work_dir}"
|
||||
|
||||
# If content of extract dir is a single directory, move its contents up
|
||||
# Glob flags:
|
||||
# - D: include files starting with .
|
||||
# - N: no error if directory is empty
|
||||
# - Y2: at most give 2 files
|
||||
local -a content
|
||||
content=("${extract_dir}"/*(DNY2))
|
||||
if [[ ${#content} -eq 1 && -e "${content[1]}" ]]; then
|
||||
# The extracted file/folder (${content[1]}) may have the same name as $extract_dir
|
||||
# If so, we need to rename it to avoid conflicts in a 3-step process
|
||||
#
|
||||
# 1. Move and rename the extracted file/folder to a temporary random name
|
||||
# 2. Delete the empty folder
|
||||
# 3. Rename the extracted file/folder to the original name
|
||||
if [[ "${content[1]:t}" == "$extract_dir" ]]; then
|
||||
# =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
|
||||
local tmp_name==(:); tmp_name="${tmp_name:t}"
|
||||
command mv "${content[1]}" "$tmp_name" \
|
||||
&& command rmdir "$extract_dir" \
|
||||
&& command mv "$tmp_name" "$extract_dir"
|
||||
# Otherwise, if the extracted folder name already exists in the current
|
||||
# directory (because of a previous file / folder), keep the extract_dir
|
||||
elif [[ ! -e "${content[1]:t}" ]]; then
|
||||
command mv "${content[1]}" . \
|
||||
&& command rmdir "$extract_dir"
|
||||
fi
|
||||
elif [[ ${#content} -eq 0 ]]; then
|
||||
command rmdir "$extract_dir"
|
||||
fi
|
||||
shift
|
||||
done
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue