1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-24 21:57:35 +00:00

Replace _die() with _exit_1().

This commit is contained in:
William Melody 2020-05-17 11:01:25 -07:00
parent 40be65b7da
commit 5a7d0cca91

63
hosts
View File

@ -109,33 +109,41 @@ debug() {
} }
############################################################################### ###############################################################################
# Die # Error Messaging
############################################################################### ###############################################################################
# _die() # _exit_1()
# #
# Usage: # Usage:
# _die printf "Error message. Variable: %s\\n" "$0" # _exit_1 <command>
# #
# A simple function for exiting with an error after executing the specified # Description:
# command. The command is expected to print a message and should typically # Exit with status 1 after executing the specified with output redirected
# be either `echo`, `printf`, or `cat`. # to standard error. The command is expected to print a message and should
_die() { # typically be either `echo`, `printf`, or `cat`.
# Prefix die message with "cross mark (U+274C)", often displayed as a red x. _exit_1() {
printf "❌ " {
"${@}" 1>&2 printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
exit 1 exit 1
} }
# die()
# _return_1()
# #
# Usage: # Usage:
# die "Error message. Variable: $0" # _return_1 <command>
# #
# Exit with an error and print the specified message. # Description:
# # Return with status 1 after executing the specified with output redirected
# This is a shortcut for the _die() function that simply echos the message. # to standard error. The command is expected to print a message and should
die() { # typically be either `echo`, `printf`, or `cat`.
_die echo "${@}" _return_1() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
return 1
} }
############################################################################### ###############################################################################
@ -193,12 +201,11 @@ _load_commands() {
"${_function_name}" "${_function_name}"
# Add the function name to the $_DEFINED_COMMANDS array unless it starts # Add the function name to the $_DEFINED_COMMANDS array unless it starts
# with an underscore or is one of the desc(), debug(), or die() functions, # with an underscore or is one of the desc(), or debug() functions,
# since these are treated as having 'private' visibility. # since these are treated as having 'private' visibility.
if ! { [[ "${_function_name}" =~ ^_(.*) ]] || \ if ! { [[ "${_function_name}" =~ ^_(.*) ]] || \
[[ "${_function_name}" == "desc" ]] || \ [[ "${_function_name}" == "desc" ]] || \
[[ "${_function_name}" == "debug" ]] || \ [[ "${_function_name}" == "debug" ]]
[[ "${_function_name}" == "die" ]]
} }
then then
_DEFINED_COMMANDS+=("${_function_name}") _DEFINED_COMMANDS+=("${_function_name}")
@ -240,7 +247,7 @@ _main() {
# Pass all comment arguments to the program except for the first ($0). # Pass all comment arguments to the program except for the first ($0).
"${_CMD}" "${_COMMAND_PARAMETERS[@]:-}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}"
else else
_die printf "Unknown command: %s\\n" "${_CMD}" _exit_1 printf "Unknown command: %s\\n" "${_CMD}"
fi fi
} }
@ -460,7 +467,7 @@ _verify_write_permissions() {
sudo "${_my_path}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}" sudo "${_my_path}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}"
exit $? exit $?
else else
_die printf \ _exit_1 printf \
"You don't have permission to perform this operation. Try again with: "You don't have permission to perform this operation. Try again with:
sudo !!\\n" sudo !!\\n"
fi fi
@ -503,11 +510,11 @@ sudo !!\\n"
# one has been set. # one has been set.
desc() { desc() {
_debug printf "desc() \${*}: %s\\n" "$@" _debug printf "desc() \${*}: %s\\n" "$@"
[[ -z "${1:-}" ]] && _die printf "desc(): No command name specified.\\n" [[ -z "${1:-}" ]] && _exit_1 printf "desc(): No command name specified.\\n"
if [[ "${1}" == "--get" ]] if [[ "${1}" == "--get" ]]
then # get ------------------------------------------------------------------ then # get ------------------------------------------------------------------
[[ -z "${2:-}" ]] && _die printf "desc(): No command name specified.\\n" [[ -z "${2:-}" ]] && _exit_1 printf "desc(): No command name specified.\\n"
local _name="${2:-}" local _name="${2:-}"
local _desc_var="___desc_${_name}" local _desc_var="___desc_${_name}"
@ -701,7 +708,7 @@ add() {
-e "^${_ip}[[:space:]]\+${_hostname}$" \ -e "^${_ip}[[:space:]]\+${_hostname}$" \
-e "^${_ip}[[:space:]]\+${_hostname}[[:space:]]\+.*$" "${HOSTS_PATH}" -e "^${_ip}[[:space:]]\+${_hostname}[[:space:]]\+.*$" "${HOSTS_PATH}"
then then
_die printf \ _exit_1 printf \
"Duplicate address/host combination, %s unchanged.\\n" \ "Duplicate address/host combination, %s unchanged.\\n" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
else else
@ -981,7 +988,7 @@ disable() {
if [[ -z "${_targets:-}" ]] if [[ -z "${_targets:-}" ]]
then then
_die printf "Not found: %s\\n" "${_search_string}" _exit_1 printf "Not found: %s\\n" "${_search_string}"
fi fi
printf "Disabling:\\n%s\\n" "${_targets}" printf "Disabling:\\n%s\\n" "${_targets}"
@ -1021,7 +1028,7 @@ edit() {
if [[ -z "${EDITOR}" ]] if [[ -z "${EDITOR}" ]]
then then
_die printf "\$EDITOR not set.\\n" _exit_1 printf "\$EDITOR not set.\\n"
else else
"${EDITOR}" "${HOSTS_PATH}" "${EDITOR}" "${HOSTS_PATH}"
fi fi
@ -1079,7 +1086,7 @@ enable() {
if [[ -z "${_targets:-}" ]] if [[ -z "${_targets:-}" ]]
then then
_die printf "Not found: %s\\n" "${_search_string}" _exit_1 printf "Not found: %s\\n" "${_search_string}"
fi fi
printf "Enabling:\\n%s\\n" "${_targets}" printf "Enabling:\\n%s\\n" "${_targets}"