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

Improve output formatting.

This commit is contained in:
William Melody 2020-04-08 16:43:07 -07:00
parent 3a810cf384
commit 902c996b3b
7 changed files with 143 additions and 110 deletions

100
hosts
View File

@ -475,6 +475,68 @@ _piped_input() {
! _interactive_input
}
# _print_entries()
#
# Usage:
# _print_entries <entries>
_print_entries() {
local _input="${1:-}"
[[ -n "${_input}" ]] || return 0
local _newline=$'\n'
if [[ -n "${2:-}" ]]
then
_input+="${_newline}${2:-}"
fi
local _disabled=0
local _max_length=0
while IFS=$'\t ' read -r -a _parts
do
if [[ "${_parts[0]}" =~ disabled ]]
then
_parts=("${_parts[@]:1}")
fi
if [[ "${_max_length}" -lt "${#_parts[0]}" ]]
then
_max_length="${#_parts[0]}"
fi
done <<< "${_input}"
_max_divided="$((${_max_length} / 8))"
while IFS=$'\t ' read -r -a _parts
do
if [[ "${_parts[0]}" =~ disabled ]]
then
_parts=("${_parts[@]:1}")
printf "disabled:\n"
fi
_current_divided=$((${#_parts[0]} / 8))
_tab_count=$((${_max_divided} - ${_current_divided} + 1))
_tabs="$(printf "%*s" ${_tab_count} | tr " " '\t')"
if [[ -n "${_parts[2]:-}" ]]
then
printf "%s%s %s\t%s\\n" \
"${_parts[0]}" \
"${_tabs}" \
"${_parts[1]}" \
"$(printf "%s" "${_parts[*]:2}" | tr '\r\n' ' ')"
else
printf "%s%s %s\\n" \
"${_parts[0]}" \
"${_tabs}" \
"${_parts[1]}"
fi
done <<< "${_input}"
}
# _verify_write_permissions
#
# Print a helpful error message when the specified operation can't be
@ -1152,21 +1214,26 @@ list() {
then
if [[ "${1}" == "disabled" ]]
then
printf "%s\\n" "${_disabled_records}"
_print_entries "${_disabled_records}"
elif [[ "${1}" == "enabled" ]]
then
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
_print_entries "$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")"
else
show "${1}"
fi
else
# NOTE: use separate expressions since using a | for the or results in
# inconsistent behavior.
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
local _enabled_records
_enabled_records="$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")"
_print_entries "${_enabled_records:-}"
if [[ -n "${_disabled_records:-}" ]]
then
printf "\\nDisabled:\\n%s\\n" "${_disabled_records}"
[[ -n "${_enabled_records:-}" ]] && printf "\\n"
printf "Disabled:\\n"
printf "%s\\n" "---------"
_print_entries "${_disabled_records}"
fi
fi
}
@ -1349,23 +1416,24 @@ show() {
then
# Run `sed` before `grep` to avoid conflict that supresses `sed` output.
local _disabled_records
_disabled_records=$(
sed -n "s/^\\#\\(disabled: .*${1}.*\\)$/\\1/p" "${HOSTS_PATH}"
)
_disabled_records="$(
sed -n "s/^\\#disabled: \\(.*${1}.*\\)$/\\1/p" "${HOSTS_PATH}"
)"
local _enabled_records
_enabled_records=$(
grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}"
)
_enabled_records="$(
grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}" || true
)"
_print_entries "${_enabled_records}"
# Output disabled records secondly for better organization.
if [[ -n "${_enabled_records}" ]]
then
printf "%s\\n" "${_enabled_records}"
fi
if [[ -n "${_disabled_records}" ]]
then
printf "%s\\n" "${_disabled_records}"
[[ -n "${_enabled_records}" ]] && printf "\\n"
printf "Disabled:\\n"
printf "%s\\n" "---------"
_print_entries "${_disabled_records}"
fi
else
help show

View File

@ -29,8 +29,8 @@ load test_helper
run "${_HOSTS}" disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
[[ "${lines[2]}" == "" ]]
}

View File

@ -29,11 +29,11 @@ load test_helper
run "${_HOSTS}" enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]]
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
}
# help ########################################################################

View File

@ -10,9 +10,9 @@ load test_helper
@test "\`hosts\` with no arguments prints enabled rules." {
run "${_HOSTS}"
[[ "${#lines[@]}" -eq 4 ]]
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]]
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${lines[4]}" == "" ]]
}

View File

@ -30,15 +30,16 @@ load test_helper
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
_expected="\
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.2 example.com
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.2 example.com
Disabled:
0.0.0.0 example.com
0.0.0.0 example.net"
---------
0.0.0.0 example.com
0.0.0.0 example.net"
_compare "'${_expected}'" "'${output}'"
[[ "${output}" == "${_expected}" ]]
}
@ -70,11 +71,11 @@ Disabled:
run "${_HOSTS}" list enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]]
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
[[ "${lines[0]}" =~ 127\.0\.0\.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255\.255\.255\.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
}
# `hosts list disabled` #######################################################
@ -104,8 +105,8 @@ Disabled:
run "${_HOSTS}" list disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
[[ "${lines[2]}" == "" ]]
}
@ -134,8 +135,8 @@ Disabled:
run "${_HOSTS}" list example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
[[ "${lines[2]}" == "" ]]
}
@ -149,7 +150,7 @@ Disabled:
run "${_HOSTS}" list "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[2]}" == "" ]]
}
@ -166,9 +167,9 @@ Disabled:
run "${_HOSTS}" list "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
[[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]]
[[ "${lines[2]}" == "" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[4]}" == "" ]]
}
# help ########################################################################

View File

@ -66,11 +66,11 @@ Description:
run "${_HOSTS}" search enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
[[ "${lines[2]}" == "::1 localhost" ]]
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
}
# `hosts search disabled` #######################################################
@ -100,8 +100,8 @@ Description:
run "${_HOSTS}" search disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "" ]]
}
@ -130,8 +130,8 @@ Description:
run "${_HOSTS}" search example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "" ]]
}
@ -145,7 +145,7 @@ Description:
run "${_HOSTS}" search "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[2]}" == "" ]]
}
@ -161,9 +161,9 @@ Description:
run "${_HOSTS}" search "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
[[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]]
[[ "${lines[2]}" == "" ]]
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[3]}" =~ 127.0.0.1[[:space:]]+example.biz[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[4]}" == "" ]]
}
# help ########################################################################

View File

@ -21,7 +21,7 @@ load test_helper
# `hosts show <ip>` #########################################################
@test "\`show <ip>\` exits with status 0." {
@test "\`show <ip>\` exits with status 0 and shows all matches." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
@ -33,27 +33,14 @@ load test_helper
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]]
}
@test "\`show <ip>\` shows all matches." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
run "${_HOSTS}" add 127.0.0.2 example.com
run "${_HOSTS}" disable example.com
}
run "${_HOSTS}" show 0.0.0.0
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net" ]]
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.net ]]
[[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
}
# `hosts show <hostname>` #####################################################
@test "\`show <hostname>\` exits with status 0." {
@test "\`show <hostname>\` exits with status 0 and shows all matches." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
@ -65,27 +52,14 @@ load test_helper
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]]
}
@test "\`show <hostname>\` shows all matches." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
run "${_HOSTS}" add 127.0.0.2 example.com
run "${_HOSTS}" disable 0.0.0.0
}
run "${_HOSTS}" show example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "127.0.0.2 example.com" ]]
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
[[ "${lines[0]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
[[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
}
# `hosts show <search string>` ################################################
@test "\`show <search string>\` exits with status 0." {
@test "\`show <search string>\` exits with status 0 and shows matching records." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
@ -96,20 +70,9 @@ load test_helper
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]]
}
@test "\`show <search string>\` prints list of matching records." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net
run "${_HOSTS}" add 127.0.0.1 example.com
}
run "${_HOSTS}" show example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "" ]]
}
@ -123,7 +86,8 @@ load test_helper
run "${_HOSTS}" show "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
printf "\${lines[0]}: '%s'\\n" "${lines[0]}"
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[2]}" == "" ]]
}
@ -139,9 +103,9 @@ load test_helper
run "${_HOSTS}" show "Comment"
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "0.0.0.0 example.net # Example Comment" ]]
[[ "${lines[1]}" == "disabled: 127.0.0.1 example.biz # Example Comment" ]]
[[ "${lines[2]}" == "" ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[4]}" == "" ]]
}
# help ########################################################################