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

Adding in EUID check to create_prefix. Also adding in more checks to that function to make sure the tools needed are available.

This commit is contained in:
Micheal Quinn 2019-11-26 14:18:30 -06:00
parent 47d84b9947
commit 247a698757

View file

@ -341,20 +341,36 @@ extract_file() {
#--- FUNCTION ---------------------------------------------------------------- #--- FUNCTION ----------------------------------------------------------------
# NAME: create_prefix # NAME: create_prefix
# DESCRIPTION: Creates the install prefix (and any parent directories) # DESCRIPTION: Creates the install prefix (and any parent directories). If
# EUID not 0, then attempt to use sudo.
# PARAMETERS: $1 = prefix # PARAMETERS: $1 = prefix
# RETURNS: Return code of the tool used to make the directory # RETURNS: Return code of the tool used to make the directory
# 0 = Created the directory # 0 = Created the directory
# >0 = Failed to create directory # >0 = Failed to create directory
# 20 = Could not find mkdir command
# 21 = Could not find sudo command
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
create_prefix() { create_prefix() {
local prefix local prefix
local rcode local rcode
prefix="${1}" prefix="${1}"
mkdir -p "${prefix}" if command -v mkdir >/dev/null 2>&1; then
rcode="${?}" if [[ "${EUID}" == "0" ]]; then
mkdir -p "${prefix}"
rcode="${?}"
else
if command -v sudo >/dev/null 2>&1; then
sudo mkdir -p "${prefix}"
rcode="${?}"
else
rcode="21"
fi
fi
else
rcode="20"
fi
return "${rcode}" return "${rcode}"
} }
@ -643,11 +659,17 @@ main() {
if [[ ! -d "${prefix}" ]]; then if [[ ! -d "${prefix}" ]]; then
create_prefix "${prefix}" create_prefix "${prefix}"
create_prefix_rcode="${?}" create_prefix_rcode="${?}"
if [[ "${create_prefix_rcode}" -gt 0 ]]; then if [[ "${create_prefix_rcode}" == "0" ]]; then
print_message "== Failed to create the install prefix: ${prefix}" "error" print_message "== Created install prefix at ${prefix}" "info"
elif [[ "${create_prefix_rcode}" == "20" ]]; then
print_message "== Failed to find mkdir in path" "error"
exit 1
elif [[ "${create_prefix_rcode}" == "21" ]]; then
print_message "== Failed to find sudo in path" "error"
exit 1 exit 1
else else
print_message "== Created install prefix at ${prefix}" "info" print_message "== Failed to create the install prefix: ${prefix}" "error"
exit 1
fi fi
else else
print_message "== Install prefix already exists. No need to create it." "info" print_message "== Install prefix already exists. No need to create it." "info"