0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

Adding in extract file logic.

This commit is contained in:
Micheal Quinn 2019-08-10 18:26:06 -05:00
parent 7a684e7266
commit c82dda45d9
No known key found for this signature in database
GPG key ID: 0E7217F3C30BA059

View file

@ -109,6 +109,48 @@ download_file() {
return "${rcode}" return "${rcode}"
} }
#--- FUNCTION ----------------------------------------------------------------
# NAME: extract_file
# DESCRIPTION: Extracts a file into a location. Attempts to determine which
# tool to use by checking file extention.
# PARAMETERS: $1 = file to extract
# $2 = location to extract file into
# $3 = extention
# RETURNS: Return code of the tool used to extract the file
# 2 = Failed to determine which tool to use
# 3 = Failed to find tool in path
#-------------------------------------------------------------------------------
extract_file() {
local file
local dir
local ext
local rcode
file="${1}"
dir="${2}"
ext="${3}"
case "${ext}" in
"zip" ) if command -v unzip >/dev/null 2>&1; then
unzip "${file}" -d "${dir}"
rcode="${?}"
else
rcode="3"
fi
;;
"tar.gz" ) if command -v tar >/dev/null 2>&1; then
tar -xf "${file}" -C "${dir}"
rcode="${?}"
else
rcode="3"
fi
;;
* ) rcode="2";;
esac
return "${rcode}"
}
#--- FUNCTION ---------------------------------------------------------------- #--- FUNCTION ----------------------------------------------------------------
# NAME: main # NAME: main
# DESCRIPTION: Put it all together in a logical way # DESCRIPTION: Put it all together in a logical way
@ -183,6 +225,24 @@ main() {
else else
echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}"
fi fi
extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}"
extract_file_rcode="${?}"
if [[ "${extract_file_rcode}" == "0" ]]; then
echo "== Extracted ${croc_file} to ${tmpdir}/"
elif [[ "${extract_file_rcode}" == "1" ]]; then
echo "== Failed to extract ${croc_file}"
exit 1
elif [[ "${extract_file_rcode}" == "2" ]]; then
echo "== Failed to determine which extraction tool to use"
exit 1
elif [[ "${extract_file_rcode}" == "3" ]]; then
echo "== Failed to find extraction tool in path"
exit 1
else
echo "== Unknown error returned from extraction attempt"
exit 1
fi
} }
main main