Pack : Use _iterate_targets.

This commit is contained in:
Dmitry Voronin 2023-12-17 20:22:06 +03:00
parent 47a30351d1
commit 418f146141

View file

@ -18,15 +18,13 @@ function pack() {
# report no format. # report no format.
if [[ "${format}" = "" ]]; then if [[ "${format}" = "" ]]; then
echo "Could not determine output format." _error "Could not determine output format."
help pack help pack
return 2 return 2
fi fi
# All targets by default. # All targets by default.
if [[ "${targets}" = "" ]]; then [[ "${targets}" = "" ]] && targets=(*)
targets=(*)
fi
# process. # process.
case "${format}" in case "${format}" in
@ -49,15 +47,12 @@ function pack() {
_pack_xz "${output}" "${targets[@]}" _pack_xz "${output}" "${targets[@]}"
;; ;;
*) *)
echo -e "${color_bred}${target}: Format not supported.${color_default}" _error "${target}: Format not supported."
return 1 return 2
;; ;;
esac esac
# Show error. _iterate_targets process ${targets[@]}
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
fi
} }
# Attempt to unpack. # Attempt to unpack.
@ -65,28 +60,10 @@ function pack() {
# Usage: unpack [FILES] # Usage: unpack [FILES]
function unpack() { function unpack() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=(${@})
local count=0 [[ "${targets}" = "" ]] && targets=($(_ls_files | grep -E ${_unpack_supported}))
local total=${#targets[@]}
local failed=0
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_unpack_supported}))
total=${#targets[@]}
fi
# process all files.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# prepare status.
local status="[${count}/${total}] ${target}"
# show status.
echo -e "${status}"
process() {
# unpack file type. # unpack file type.
local type="${target##*.}" local type="${target##*.}"
@ -123,23 +100,13 @@ function unpack() {
_unpack_gz "${target}" _unpack_gz "${target}"
;; ;;
*) *)
echo -e "${color_bred}${target}: Format not supported.${color_default}" _error "${target}: Format not supported."
((failed++)) return 2
continue
;; ;;
esac esac
}
# actions on error. _iterate_targets process ${targets[@]}
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
function _pack_zip() { function _pack_zip() {