Improve code formatting.

This commit is contained in:
William Melody 2020-05-17 11:10:12 -07:00
parent 16a95406fe
commit 16a672d52a
1 changed files with 47 additions and 45 deletions

92
hosts
View File

@ -723,23 +723,23 @@ add() {
_formatted_comment=$(_join " " "${_comment[@]}") _formatted_comment=$(_join " " "${_comment[@]}")
printf "%s%s%s\\t# %s\\n" \ printf "%s%s%s\\t# %s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \ "${_tabs}" \
"${_hostname}" \ "${_hostname}" \
"${_formatted_comment}" >> "${HOSTS_PATH}" "${_formatted_comment}" >> "${HOSTS_PATH}"
printf "Added:\\n%s%s%s\\t# %s\\n" \ printf "Added:\\n%s%s%s\\t# %s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \ "${_tabs}" \
"${_hostname}" \ "${_hostname}" \
"${_formatted_comment}" "${_formatted_comment}"
else else
printf "%s%s%s\\n" \ printf "%s%s%s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \ "${_tabs}" \
"${_hostname}" >> "${HOSTS_PATH}" "${_hostname}" >> "${HOSTS_PATH}"
printf "Added:\\n%s%s%s\\n" \ printf "Added:\\n%s%s%s\\n" \
"${_ip}" \ "${_ip}" \
"${_tabs}" \ "${_tabs}" \
"${_hostname}" "${_hostname}"
fi fi
fi fi
@ -832,7 +832,7 @@ backups() {
_verify_write_permissions _verify_write_permissions
if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${_filename:-}" ]] && if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${_filename:-}" ]] &&
[[ -e "${_hosts_dirname}/${_filename:-}" ]] [[ -e "${_hosts_dirname}/${_filename:-}" ]]
then then
rm "${_hosts_dirname}/${_filename:-}" && rm "${_hosts_dirname}/${_filename:-}" &&
printf "Backup deleted: %s\\n" "${_hosts_dirname}/${_filename:-}" printf "Backup deleted: %s\\n" "${_hosts_dirname}/${_filename:-}"
@ -881,8 +881,8 @@ backups() {
set +f set +f
for __filename in $(cd "${_hosts_dirname}" && ls -1 hosts* 2> /dev/null) for __filename in $(cd "${_hosts_dirname}" && ls -1 hosts* 2> /dev/null)
do do
if [[ "${__filename:-}" != "hosts" ]] && \ if [[ "${__filename:-}" != "hosts" ]] &&
[[ ! "${__filename:-}" =~ ^hosts_test..{6}$ ]] [[ ! "${__filename:-}" =~ ^hosts_test..{6}$ ]]
then then
_filenames+=("${__filename:-}") _filenames+=("${__filename:-}")
fi fi
@ -977,10 +977,10 @@ disable() {
# which apparently doesn't work properly with all versions of sed. # which apparently doesn't work properly with all versions of sed.
local _targets local _targets
_targets=$( _targets=$(
sed -n \ sed -n \
-e "s/${_regex_ip}/\\1/p" \ -e "s/${_regex_ip}/\\1/p" \
-e "s/${_regex_commented_hostname}/\\1/p" \ -e "s/${_regex_commented_hostname}/\\1/p" \
-e "s/${_regex_hostname}/\\1/p" \ -e "s/${_regex_hostname}/\\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
@ -993,10 +993,10 @@ disable() {
printf "Disabling:\\n%s\\n" "${_targets}" printf "Disabling:\\n%s\\n" "${_targets}"
"${_SED_I_COMMAND[@]}" \ "${_SED_I_COMMAND[@]}" \
-e "s/${_regex_ip}/\\#disabled: \\1/g" \ -e "s/${_regex_ip}/\\#disabled: \\1/g" \
-e "s/${_regex_commented_hostname}/\\#disabled: \\1/g" \ -e "s/${_regex_commented_hostname}/\\#disabled: \\1/g" \
-e "s/${_regex_hostname}/\\#disabled: \\1/g" \ -e "s/${_regex_hostname}/\\#disabled: \\1/g" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
fi fi
} }
@ -1075,10 +1075,10 @@ enable() {
local _targets local _targets
_targets=$( _targets=$(
sed -n \ sed -n \
-e "s/${_regex_ip}/\\1/p" \ -e "s/${_regex_ip}/\\1/p" \
-e "s/${_regex_commented_hostname}/\\1/p" \ -e "s/${_regex_commented_hostname}/\\1/p" \
-e "s/${_regex_hostname}/\\1/p" \ -e "s/${_regex_hostname}/\\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
@ -1091,10 +1091,10 @@ enable() {
printf "Enabling:\\n%s\\n" "${_targets}" printf "Enabling:\\n%s\\n" "${_targets}"
"${_SED_I_COMMAND[@]}" \ "${_SED_I_COMMAND[@]}" \
-e "s/${_regex_ip}/\\1/g" \ -e "s/${_regex_ip}/\\1/g" \
-e "s/${_regex_commented_hostname}/\\1/g" \ -e "s/${_regex_commented_hostname}/\\1/g" \
-e "s/${_regex_hostname}/\\1/g" \ -e "s/${_regex_hostname}/\\1/g" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
fi fi
} }
@ -1262,17 +1262,17 @@ remove() {
if ((_is_search_pair)) if ((_is_search_pair))
then then
_target_records=$( _target_records=$(
sed -n \ sed -n \
-e "s/${_regex_ip_hostname_commented}/\\1/p" \ -e "s/${_regex_ip_hostname_commented}/\\1/p" \
-e "s/${_regex_ip_hostname}/\\1/p" \ -e "s/${_regex_ip_hostname}/\\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
else else
_target_records=$( _target_records=$(
sed -n \ sed -n \
-e "s/${_regex_ip}/\\1/p" \ -e "s/${_regex_ip}/\\1/p" \
-e "s/${_regex_commented_hostname}/\\1/p" \ -e "s/${_regex_commented_hostname}/\\1/p" \
-e "s/${_regex_hostname}/\\1/p" \ -e "s/${_regex_hostname}/\\1/p" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
) )
fi fi
@ -1305,15 +1305,15 @@ remove() {
if ((_is_search_pair)) if ((_is_search_pair))
then then
"${_SED_I_COMMAND[@]}" \ "${_SED_I_COMMAND[@]}" \
-e "/${_regex_ip_hostname_commented}/d" \ -e "/${_regex_ip_hostname_commented}/d" \
-e "/${_regex_ip_hostname}/d" \ -e "/${_regex_ip_hostname}/d" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
else else
"${_SED_I_COMMAND[@]}" \ "${_SED_I_COMMAND[@]}" \
-e "/${_regex_ip}/d" \ -e "/${_regex_ip}/d" \
-e "/${_regex_commented_hostname}/d" \ -e "/${_regex_commented_hostname}/d" \
-e "/${_regex_hostname}/d" \ -e "/${_regex_hostname}/d" \
"${HOSTS_PATH}" "${HOSTS_PATH}"
fi fi
@ -1367,7 +1367,8 @@ show() {
grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" || true grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" || true
)" )"
if [[ -z "${_disabled_records}" ]] && [[ -z "${_enabled_records}" ]] if [[ -z "${_disabled_records}" ]] &&
[[ -z "${_enabled_records}" ]]
then then
_return_1 printf "No matching entries.\\n" _return_1 printf "No matching entries.\\n"
fi fi
@ -1412,10 +1413,10 @@ unblock() {
for __hostname in "${@}" for __hostname in "${@}"
do do
remove 127.0.0.1 "${__hostname}" --force remove 127.0.0.1 "${__hostname}" --force
# unblock IPv6 # unblock IPv6
remove "fe80::1%lo0" "${__hostname}" --force remove "fe80::1%lo0" "${__hostname}" --force
remove "::1" "${__hostname}" --force remove "::1" "${__hostname}" --force
done done
} }
@ -1500,7 +1501,8 @@ do
*) *)
# The first non-option argument is assumed to be the command name. # The first non-option argument is assumed to be the command name.
# All subsequent arguments are added to $_COMMAND_ARGV. # All subsequent arguments are added to $_COMMAND_ARGV.
if [[ -z "${_CMD:-}" ]] && [[ "${__opt:-}" =~ ${_SUBCOMMANDS_PATTERN} ]] if [[ -z "${_CMD:-}" ]] &&
[[ "${__opt:-}" =~ ${_SUBCOMMANDS_PATTERN} ]]
then then
_CMD="${__opt}" _CMD="${__opt}"
else else