mirror of
https://github.com/octoleo/hosts.git
synced 2024-11-22 21:05:11 +00:00
Prefer newlines instead of semicolons.
This commit is contained in:
parent
66edbfd03c
commit
d2b92aec5a
107
hosts
107
hosts
@ -62,7 +62,8 @@ HOSTS_PATH="${HOSTS_PATH:-/etc/hosts}"
|
|||||||
# variable has been set. The command is expected to print a message and
|
# variable has been set. The command is expected to print a message and
|
||||||
# should typically be either `echo`, `printf`, or `cat`.
|
# should typically be either `echo`, `printf`, or `cat`.
|
||||||
_debug() {
|
_debug() {
|
||||||
if [[ "${_use_debug:-"0"}" -eq 1 ]]; then
|
if [[ "${_use_debug:-"0"}" -eq 1 ]]
|
||||||
|
then
|
||||||
# Prefix debug message with "bug (U+1F41B)"
|
# Prefix debug message with "bug (U+1F41B)"
|
||||||
printf "🐛 "
|
printf "🐛 "
|
||||||
"$@"
|
"$@"
|
||||||
@ -154,12 +155,14 @@ optstring=h
|
|||||||
# argument to the option, such as wget -qO-)
|
# argument to the option, such as wget -qO-)
|
||||||
unset options
|
unset options
|
||||||
# while the number of arguments is greater than 0
|
# while the number of arguments is greater than 0
|
||||||
while (($#)); do
|
while (($#))
|
||||||
|
do
|
||||||
case $1 in
|
case $1 in
|
||||||
# if option is of type -ab
|
# if option is of type -ab
|
||||||
-[!-]?*)
|
-[!-]?*)
|
||||||
# loop over each character starting with the second
|
# loop over each character starting with the second
|
||||||
for ((i=1; i<${#1}; i++)); do
|
for ((i=1; i<${#1}; i++))
|
||||||
|
do
|
||||||
# extract 1 character from position 'i'
|
# extract 1 character from position 'i'
|
||||||
c=${1:i:1}
|
c=${1:i:1}
|
||||||
# add current char to options
|
# add current char to options
|
||||||
@ -167,14 +170,17 @@ while (($#)); do
|
|||||||
|
|
||||||
# if option takes a required argument, and it's not the last char
|
# if option takes a required argument, and it's not the last char
|
||||||
# make the rest of the string its argument
|
# make the rest of the string its argument
|
||||||
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
|
if [[ $optstring = *"$c:"* && ${1:i+1} ]]
|
||||||
|
then
|
||||||
options+=("${1:i+1}")
|
options+=("${1:i+1}")
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
;;
|
;;
|
||||||
# if option is of type --foo=bar, split on first '='
|
# if option is of type --foo=bar, split on first '='
|
||||||
--?*=*) options+=("${1%%=*}" "${1#*=}");;
|
--?*=*)
|
||||||
|
options+=("${1%%=*}" "${1#*=}")
|
||||||
|
;;
|
||||||
# end of options, stop breaking them up
|
# end of options, stop breaking them up
|
||||||
--)
|
--)
|
||||||
options+=(--endopts)
|
options+=(--endopts)
|
||||||
@ -183,7 +189,9 @@ while (($#)); do
|
|||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
# otherwise, nothing special
|
# otherwise, nothing special
|
||||||
*) options+=("$1");;
|
*)
|
||||||
|
options+=("$1")
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
shift
|
shift
|
||||||
@ -206,7 +214,8 @@ command_argv=("$0")
|
|||||||
cmd=""
|
cmd=""
|
||||||
_use_debug=0
|
_use_debug=0
|
||||||
|
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]
|
||||||
|
do
|
||||||
opt="$1"
|
opt="$1"
|
||||||
shift
|
shift
|
||||||
case "$opt" in
|
case "$opt" in
|
||||||
@ -222,7 +231,8 @@ while [ $# -gt 0 ]; 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_arguments.
|
# All subsequent arguments are added to $command_arguments.
|
||||||
if [[ -n $cmd ]]; then
|
if [[ -n $cmd ]]
|
||||||
|
then
|
||||||
command_argv+=("$opt")
|
command_argv+=("$opt")
|
||||||
else
|
else
|
||||||
cmd="$opt"
|
cmd="$opt"
|
||||||
@ -292,7 +302,8 @@ _load_commands() {
|
|||||||
[[ "$function_name" == "desc" ]] || \
|
[[ "$function_name" == "desc" ]] || \
|
||||||
[[ "$function_name" == "debug" ]] || \
|
[[ "$function_name" == "debug" ]] || \
|
||||||
[[ "$function_name" == "die" ]]
|
[[ "$function_name" == "die" ]]
|
||||||
); then
|
)
|
||||||
|
then
|
||||||
defined_commands+=("$function_name")
|
defined_commands+=("$function_name")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -319,7 +330,8 @@ _main() {
|
|||||||
_debug printf "main() \$cmd (upon entering): %s\n" "$cmd"
|
_debug printf "main() \$cmd (upon entering): %s\n" "$cmd"
|
||||||
|
|
||||||
# If $cmd is blank, then set to help
|
# If $cmd is blank, then set to help
|
||||||
if [[ -z $cmd ]]; then
|
if [[ -z $cmd ]]
|
||||||
|
then
|
||||||
cmd="$DEFAULT_COMMAND"
|
cmd="$DEFAULT_COMMAND"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -327,7 +339,8 @@ _main() {
|
|||||||
_load_commands
|
_load_commands
|
||||||
|
|
||||||
# If the command is defined, run it, otherwise return an error.
|
# If the command is defined, run it, otherwise return an error.
|
||||||
if ( _contains "$cmd" "${defined_commands[*]:-}" ); then
|
if ( _contains "$cmd" "${defined_commands[*]:-}" )
|
||||||
|
then
|
||||||
# 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
|
||||||
@ -358,7 +371,8 @@ _contains() {
|
|||||||
for _test_element in "${test_list[@]:-}"
|
for _test_element in "${test_list[@]:-}"
|
||||||
do
|
do
|
||||||
_debug printf "_contains() \$_test_element: %s\n" "$_test_element"
|
_debug printf "_contains() \$_test_element: %s\n" "$_test_element"
|
||||||
if [[ "$_test_element" == "$1" ]]; then
|
if [[ "$_test_element" == "$1" ]]
|
||||||
|
then
|
||||||
_debug printf "_contains() match: %s\n" "$1"
|
_debug printf "_contains() match: %s\n" "$1"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
@ -402,7 +416,8 @@ _command_argv_includes() {
|
|||||||
# Print a helpful error message when the specified operation can't be
|
# Print a helpful error message when the specified operation can't be
|
||||||
# performed due to the lack of write permissions.
|
# performed due to the lack of write permissions.
|
||||||
_verify_write_permissions() {
|
_verify_write_permissions() {
|
||||||
if ! test -w "${HOSTS_PATH}"; then
|
if ! test -w "${HOSTS_PATH}"
|
||||||
|
then
|
||||||
_die printf \
|
_die 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"
|
||||||
@ -441,7 +456,8 @@ sudo !!\n"
|
|||||||
desc() {
|
desc() {
|
||||||
set +e
|
set +e
|
||||||
[[ -z $1 ]] && _die printf "desc: No command name specified.\n"
|
[[ -z $1 ]] && _die printf "desc: No command name specified.\n"
|
||||||
if [[ -n ${2:-} ]]; then
|
if [[ -n ${2:-} ]]
|
||||||
|
then
|
||||||
read -d '' "_desc_$1" <<EOM
|
read -d '' "_desc_$1" <<EOM
|
||||||
$2
|
$2
|
||||||
EOM
|
EOM
|
||||||
@ -462,7 +478,8 @@ EOM
|
|||||||
# set using the desc() function.
|
# set using the desc() function.
|
||||||
_print_desc() {
|
_print_desc() {
|
||||||
local var="_desc_$1"
|
local var="_desc_$1"
|
||||||
if [[ -n ${!var:-} ]]; then
|
if [[ -n ${!var:-} ]]
|
||||||
|
then
|
||||||
printf "%s\n" "${!var}"
|
printf "%s\n" "${!var}"
|
||||||
else
|
else
|
||||||
printf "No additional information for \`%s\`\n" "$1"
|
printf "No additional information for \`%s\`\n" "$1"
|
||||||
@ -498,7 +515,8 @@ Description:
|
|||||||
Display help information for $_ME or a specified command.
|
Display help information for $_ME or a specified command.
|
||||||
EOM
|
EOM
|
||||||
help() {
|
help() {
|
||||||
if [[ ${#command_argv[@]} = 1 ]]; then
|
if [[ ${#command_argv[@]} = 1 ]]
|
||||||
|
then
|
||||||
cat <<EOM
|
cat <<EOM
|
||||||
__ __
|
__ __
|
||||||
/ /_ ____ _____/ /______
|
/ /_ ____ _____/ /______
|
||||||
@ -552,7 +570,8 @@ Description:
|
|||||||
Display the list of available commands.
|
Display the list of available commands.
|
||||||
EOM
|
EOM
|
||||||
commands() {
|
commands() {
|
||||||
if _command_argv_includes "--raw"; then
|
if _command_argv_includes "--raw"
|
||||||
|
then
|
||||||
printf "%s\n" "${defined_commands[@]}"
|
printf "%s\n" "${defined_commands[@]}"
|
||||||
else
|
else
|
||||||
printf "Available commands:\n"
|
printf "Available commands:\n"
|
||||||
@ -601,21 +620,25 @@ add() {
|
|||||||
local ip=${1:-}
|
local ip=${1:-}
|
||||||
local hostname=${2:-}
|
local hostname=${2:-}
|
||||||
local comment=${*:3}
|
local comment=${*:3}
|
||||||
if [[ -z ${ip} ]]; then
|
if [[ -z ${ip} ]]
|
||||||
|
then
|
||||||
$_ME help add
|
$_ME help add
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ -z ${hostname} ]]; then
|
elif [[ -z ${hostname} ]]
|
||||||
|
then
|
||||||
printf "Please include a hostname\n"
|
printf "Please include a hostname\n"
|
||||||
$_ME help add
|
$_ME help add
|
||||||
exit 1
|
exit 1
|
||||||
elif grep \
|
elif grep \
|
||||||
-e "^${ip}\t${hostname}$" \
|
-e "^${ip}\t${hostname}$" \
|
||||||
-e "^${ip}\t${hostname}\t.*$" "${HOSTS_PATH}" ; then
|
-e "^${ip}\t${hostname}\t.*$" "${HOSTS_PATH}"
|
||||||
_die printf \
|
then
|
||||||
"Duplicate address/host combination, %s unchanged.\n" \
|
_die printf \
|
||||||
"${HOSTS_PATH}"
|
"Duplicate address/host combination, %s unchanged.\n" \
|
||||||
|
"${HOSTS_PATH}"
|
||||||
else
|
else
|
||||||
if [[ -n ${comment} ]]; then
|
if [[ -n ${comment} ]]
|
||||||
|
then
|
||||||
local formatted_comment
|
local formatted_comment
|
||||||
formatted_comment=$(_join " " "${comment[@]}")
|
formatted_comment=$(_join " " "${comment[@]}")
|
||||||
printf "%s\t%s\t# %s\n" \
|
printf "%s\t%s\t# %s\n" \
|
||||||
@ -643,7 +666,8 @@ EOM
|
|||||||
disable() {
|
disable() {
|
||||||
_verify_write_permissions
|
_verify_write_permissions
|
||||||
local search_term=$1
|
local search_term=$1
|
||||||
if [[ -z "${search_term}" ]]; then
|
if [[ -z "${search_term}" ]]
|
||||||
|
then
|
||||||
$_ME help disable
|
$_ME help disable
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
@ -684,7 +708,8 @@ Description:
|
|||||||
EOM
|
EOM
|
||||||
edit() {
|
edit() {
|
||||||
_verify_write_permissions
|
_verify_write_permissions
|
||||||
if [[ -z "$EDITOR" ]]; then
|
if [[ -z "$EDITOR" ]]
|
||||||
|
then
|
||||||
_die printf "\$EDITOR not set.\n"
|
_die printf "\$EDITOR not set.\n"
|
||||||
else
|
else
|
||||||
"$EDITOR" "${HOSTS_PATH}"
|
"$EDITOR" "${HOSTS_PATH}"
|
||||||
@ -704,7 +729,8 @@ EOM
|
|||||||
enable() {
|
enable() {
|
||||||
_verify_write_permissions
|
_verify_write_permissions
|
||||||
local search_term=$1
|
local search_term=$1
|
||||||
if [[ -z "${search_term}" ]]; then
|
if [[ -z "${search_term}" ]]
|
||||||
|
then
|
||||||
$_ME help enable
|
$_ME help enable
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
@ -760,10 +786,13 @@ list() {
|
|||||||
# Get the disabled records up front for the two cases where they are needed.
|
# Get the disabled records up front for the two cases where they are needed.
|
||||||
disabled_records=$(sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}")
|
disabled_records=$(sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}")
|
||||||
|
|
||||||
if [[ -n "$1" ]]; then
|
if [[ -n "$1" ]]
|
||||||
if [[ "$1" == disabled ]]; then
|
then
|
||||||
|
if [[ "$1" == disabled ]]
|
||||||
|
then
|
||||||
printf "%s\n" "${disabled_records}"
|
printf "%s\n" "${disabled_records}"
|
||||||
elif [[ "$1" == enabled ]]; then
|
elif [[ "$1" == enabled ]]
|
||||||
|
then
|
||||||
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
|
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
|
||||||
else
|
else
|
||||||
$_ME show "$1"
|
$_ME show "$1"
|
||||||
@ -772,7 +801,8 @@ list() {
|
|||||||
# NOTE: use separate expressions since using a | for the or results in
|
# NOTE: use separate expressions since using a | for the or results in
|
||||||
# inconsistent behavior.
|
# inconsistent behavior.
|
||||||
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
|
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
|
||||||
if [[ -n "${disabled_records}" ]]; then
|
if [[ -n "${disabled_records}" ]]
|
||||||
|
then
|
||||||
printf "\nDisabled:\n%s\n" "${disabled_records}"
|
printf "\nDisabled:\n%s\n" "${disabled_records}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@ -794,19 +824,23 @@ EOM
|
|||||||
remove() {
|
remove() {
|
||||||
_verify_write_permissions
|
_verify_write_permissions
|
||||||
local search_string=${1:-}
|
local search_string=${1:-}
|
||||||
if [[ -z $search_string ]]; then
|
if [[ -z $search_string ]]
|
||||||
|
then
|
||||||
$_ME help remove
|
$_ME help remove
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
local target_records
|
local target_records
|
||||||
target_records=$(sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}")
|
target_records=$(sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}")
|
||||||
if [[ -z ${target_records:-} ]]; then
|
if [[ -z ${target_records:-} ]]
|
||||||
|
then
|
||||||
printf "No matching records found.\n"
|
printf "No matching records found.\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if ! _command_argv_includes "--force"; then
|
if ! _command_argv_includes "--force"
|
||||||
|
then
|
||||||
printf "Removing the following records:\n%s\n" "$target_records"
|
printf "Removing the following records:\n%s\n" "$target_records"
|
||||||
while true; do
|
while true
|
||||||
|
do
|
||||||
read -p "Are you sure you want to proceed? [y/n] " yn
|
read -p "Are you sure you want to proceed? [y/n] " yn
|
||||||
case $yn in
|
case $yn in
|
||||||
[Yy]* )
|
[Yy]* )
|
||||||
@ -839,7 +873,8 @@ Description:
|
|||||||
Print entries matching a given IP address, hostname, or search string.
|
Print entries matching a given IP address, hostname, or search string.
|
||||||
EOM
|
EOM
|
||||||
show() {
|
show() {
|
||||||
if [[ -n "$1" ]]; then
|
if [[ -n "$1" ]]
|
||||||
|
then
|
||||||
# Run `sed` before `grep` to avoid conflict that supress `sed` output.
|
# Run `sed` before `grep` to avoid conflict that supress `sed` output.
|
||||||
disabled_records=$(sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}")
|
disabled_records=$(sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}")
|
||||||
enabled_records=$(grep "^[^#]*$1" "${HOSTS_PATH}")
|
enabled_records=$(grep "^[^#]*$1" "${HOSTS_PATH}")
|
||||||
|
Loading…
Reference in New Issue
Block a user