mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Adding help message formatting. Adding in sudo check. Caught and fixed some global variables that should be local.
This commit is contained in:
parent
2c2920eb9c
commit
bf15c4c424
1 changed files with 82 additions and 9 deletions
|
@ -28,6 +28,35 @@
|
||||||
#===============================================================================
|
#===============================================================================
|
||||||
set -o nounset # Treat unset variables as an error
|
set -o nounset # Treat unset variables as an error
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
# DEFAULTS
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
PREFIX="/usr/local/bin"
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
# FUNCTIONS
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#--- FUNCTION ----------------------------------------------------------------
|
||||||
|
# NAME: print_help
|
||||||
|
# DESCRIPTION: Prints out a help message
|
||||||
|
# PARAMETERS: none
|
||||||
|
# RETURNS: 0
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
print_help() {
|
||||||
|
help_header="croc Installer Script"
|
||||||
|
help_message="Usage:
|
||||||
|
-p, --prefix
|
||||||
|
Prefix to install croc into. Directory must already exist.
|
||||||
|
Default = /usr/local/bin
|
||||||
|
|
||||||
|
-h, --help
|
||||||
|
Prints this helpfull message and exit."
|
||||||
|
|
||||||
|
echo "${help_header}"
|
||||||
|
echo ""
|
||||||
|
echo "${help_message}"
|
||||||
|
}
|
||||||
|
|
||||||
#--- FUNCTION ----------------------------------------------------------------
|
#--- FUNCTION ----------------------------------------------------------------
|
||||||
# NAME: print_message
|
# NAME: print_message
|
||||||
|
@ -189,6 +218,7 @@ download_file() {
|
||||||
checksum_check() {
|
checksum_check() {
|
||||||
local checksum_file
|
local checksum_file
|
||||||
local file
|
local file
|
||||||
|
local rcode
|
||||||
local shasum_1
|
local shasum_1
|
||||||
local shasum_2
|
local shasum_2
|
||||||
|
|
||||||
|
@ -277,6 +307,7 @@ extract_file() {
|
||||||
# RETURNS: 0 = File Installed
|
# RETURNS: 0 = File Installed
|
||||||
# 1 = File not installed
|
# 1 = File not installed
|
||||||
# 2 = Could not find install command
|
# 2 = Could not find install command
|
||||||
|
# 3 = Could not find sudo command
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
install_file_freebsd() {
|
install_file_freebsd() {
|
||||||
local file
|
local file
|
||||||
|
@ -291,8 +322,12 @@ install_file_freebsd() {
|
||||||
install -C -b -B '_old' -m 755 "${file}" "${prefix}"
|
install -C -b -B '_old' -m 755 "${file}" "${prefix}"
|
||||||
rcode="${?}"
|
rcode="${?}"
|
||||||
else
|
else
|
||||||
sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}"
|
if command -v sudo >/dev/null 2>&1; then
|
||||||
rcode="${?}"
|
sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}"
|
||||||
|
rcode="${?}"
|
||||||
|
else
|
||||||
|
rcode="3"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
rcode="2"
|
rcode="2"
|
||||||
|
@ -325,8 +360,12 @@ install_file_linux() {
|
||||||
install -C -b -S '_old' -m 755 -t "${prefix}" "${file}"
|
install -C -b -S '_old' -m 755 -t "${prefix}" "${file}"
|
||||||
rcode="${?}"
|
rcode="${?}"
|
||||||
else
|
else
|
||||||
sudo install -C -b -S '_old' -m 755 -t "${prefix}" "${file}"
|
if command -v sudo >/dev/null 2>&1; then
|
||||||
rcode="${?}"
|
sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}"
|
||||||
|
rcode="${?}"
|
||||||
|
else
|
||||||
|
rcode="3"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
rcode="2"
|
rcode="2"
|
||||||
|
@ -354,15 +393,18 @@ cleanup() {
|
||||||
# NAME: main
|
# NAME: main
|
||||||
# DESCRIPTION: Put it all together in a logical way
|
# DESCRIPTION: Put it all together in a logical way
|
||||||
# ...at least that is the hope...
|
# ...at least that is the hope...
|
||||||
# PARAMETERS: none
|
# PARAMETERS: 1 = prefix
|
||||||
# RETURNS: 0 = All good
|
# RETURNS: 0 = All good
|
||||||
# 1 = Something done broke
|
# 1 = Something done broke
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
main() {
|
main() {
|
||||||
local prefix
|
local prefix
|
||||||
local tmpdir
|
local tmpdir
|
||||||
|
local tmpdir_rcode
|
||||||
local croc_arch
|
local croc_arch
|
||||||
|
local croc_arch_rcode
|
||||||
local croc_os
|
local croc_os
|
||||||
|
local croc_os_rcode
|
||||||
local croc_base_url
|
local croc_base_url
|
||||||
local croc_url
|
local croc_url
|
||||||
local croc_file
|
local croc_file
|
||||||
|
@ -370,12 +412,17 @@ main() {
|
||||||
local croc_bin_name
|
local croc_bin_name
|
||||||
local croc_version
|
local croc_version
|
||||||
local croc_dl_ext
|
local croc_dl_ext
|
||||||
|
local download_file_rcode
|
||||||
|
local download_checksum_file_rcode
|
||||||
|
local checksum_check_rcode
|
||||||
|
local extract_file_rcode
|
||||||
|
local install_file_rcode
|
||||||
|
|
||||||
croc_bin_name="croc"
|
croc_bin_name="croc"
|
||||||
croc_version="6.1.1"
|
croc_version="6.1.1"
|
||||||
croc_dl_ext="tar.gz"
|
croc_dl_ext="tar.gz"
|
||||||
croc_base_url="https://github.com/schollz/croc/releases/download"
|
croc_base_url="https://github.com/schollz/croc/releases/download"
|
||||||
prefix="/usr/local/bin"
|
prefix="${1}"
|
||||||
|
|
||||||
tmpdir="$(make_tempdir "${croc_bin_name}")"
|
tmpdir="$(make_tempdir "${croc_bin_name}")"
|
||||||
tmpdir_rcode="${?}"
|
tmpdir_rcode="${?}"
|
||||||
|
@ -500,15 +547,41 @@ main() {
|
||||||
elif [[ "${install_file_rcode}" == "2" ]]; then
|
elif [[ "${install_file_rcode}" == "2" ]]; then
|
||||||
print_message "== Failed to locate 'install' command" "error"
|
print_message "== Failed to locate 'install' command" "error"
|
||||||
exit 1
|
exit 1
|
||||||
|
elif [[ "${install_file_rcode}" == "3" ]]; then
|
||||||
|
print_message "== Failed to locate 'sudo' command" "error"
|
||||||
|
exit 1
|
||||||
else
|
else
|
||||||
print_message "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" "error"
|
print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
print_message "== Cleaning up ${tmpdir}" "info"
|
print_message "== Cleaning up ${tmpdir}" "info"
|
||||||
cleanup "${tmpdir}"
|
cleanup "${tmpdir}"
|
||||||
print_message "== Installation complete" "ok"
|
print_message "== Installation complete" "ok"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main
|
#-------------------------------------------------------------------------------
|
||||||
|
# ARGUMENT PARSING
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
OPTS=$(getopt -o hp: --long help,prefix: -n 'parse-options' -- "${@}")
|
||||||
|
getopt_rcode="${?}"
|
||||||
|
if [ ${getopt_rcode} != 0 ] ; then
|
||||||
|
echo "Failed parsing options." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
eval set -- "${OPTS}"
|
||||||
|
|
||||||
|
while true; do
|
||||||
|
case "${1}" in
|
||||||
|
-p | --prefix ) PREFIX="${2}"; shift 2;;
|
||||||
|
-h | --help ) print_help; exit 0;;
|
||||||
|
-- ) shift; break ;;
|
||||||
|
* ) break ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
# CALL MAIN
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
main "${PREFIX}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue