Add exit status to `list` and associated subcommands.

This commit is contained in:
William Melody 2020-09-11 17:40:47 -07:00
parent c74671613b
commit 06ad3e8deb
6 changed files with 116 additions and 23 deletions

View File

@ -512,6 +512,10 @@ Usage:
Description: Description:
List all disabled records. This is an alias for `hosts list disabled`. List all disabled records. This is an alias for `hosts list disabled`.
Exit status:
0 One or more disabled entries found.
1 Invalid parameters or no disabled entries found.
``` ```
### `hosts edit` ### `hosts edit`
@ -547,6 +551,10 @@ Usage:
Description: Description:
List all enabled records. This is an alias for `hosts list enabled`. List all enabled records. This is an alias for `hosts list enabled`.
Exit status:
0 One or more enabled entries found.
1 Invalid parameters or no enabled entries found.
``` ```
### `hosts file` ### `hosts file`
@ -579,6 +587,10 @@ Description:
List the existing IP / hostname pairs, optionally limited to a specified List the existing IP / hostname pairs, optionally limited to a specified
state. When provided with a seach string, all matching enabled records will state. When provided with a seach string, all matching enabled records will
be printed. be printed.
Exit status:
0 One or more matching entries found.
1 Invalid parameters or entry not found.
``` ```
### `hosts remove` ### `hosts remove`

30
hosts
View File

@ -1131,6 +1131,10 @@ Usage:
Description: Description:
List all disabled records. This is an alias for \`hosts list disabled\`. List all disabled records. This is an alias for \`hosts list disabled\`.
Exit status:
0 One or more disabled entries found.
1 Invalid parameters or no disabled entries found.
HEREDOC HEREDOC
disabled() { disabled() {
list disabled list disabled
@ -1230,6 +1234,10 @@ Usage:
Description: Description:
List all enabled records. This is an alias for \`hosts list enabled\`. List all enabled records. This is an alias for \`hosts list enabled\`.
Exit status:
0 One or more enabled entries found.
1 Invalid parameters or no enabled entries found.
HEREDOC HEREDOC
enabled() { enabled() {
list enabled list enabled
@ -1258,6 +1266,10 @@ Description:
List the existing IP / hostname pairs, optionally limited to a specified List the existing IP / hostname pairs, optionally limited to a specified
state. When provided with a seach string, all matching enabled records will state. When provided with a seach string, all matching enabled records will
be printed. be printed.
Exit status:
0 One or more matching entries found.
1 Invalid parameters or entry not found.
HEREDOC HEREDOC
list() { list() {
local _disabled_records local _disabled_records
@ -1265,22 +1277,30 @@ list() {
sed -n "s/^\\#disabled: \\(.*\\)$/\\1/p" "${HOSTS_PATH}" sed -n "s/^\\#disabled: \\(.*\\)$/\\1/p" "${HOSTS_PATH}"
) )
# NOTE: use separate expressions since using `|` for the 'or' results in
# inconsistent behavior.
local _enabled_records
_enabled_records="$(
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
)"
if [[ -n "${1:-}" ]] if [[ -n "${1:-}" ]]
then then
if [[ "${1}" == "disabled" ]] if [[ "${1}" == "disabled" ]]
then then
[[ -z "${_disabled_records}" ]] && return 1
_print_entries "${_disabled_records}" _print_entries "${_disabled_records}"
elif [[ "${1}" == "enabled" ]] elif [[ "${1}" == "enabled" ]]
then then
_print_entries "$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")" [[ -z "${_enabled_records}" ]] && return 1
_print_entries "${_enabled_records}"
else else
show "${1}" show "${1}"
fi fi
else else
# NOTE: use separate expressions since using `|` for the 'or' results in [[ -z "${_enabled_records}" ]] &&
# inconsistent behavior. [[ -z "${_enabled_records}" ]] &&
local _enabled_records return 1
_enabled_records="$(grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}")"
_print_entries "${_enabled_records:-}" _print_entries "${_enabled_records:-}"

View File

@ -34,6 +34,13 @@ load test_helper
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`disabled\` exits with status 1 when no matching entries found." {
run "${_HOSTS}" disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help disabled\` exits with status 0." { @test "\`help disabled\` exits with status 0." {

View File

@ -36,6 +36,18 @@ load test_helper
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`enabled\` exits with status 1 when no matching entries found." {
{
run "${_HOSTS}" disable localhost
run "${_HOSTS}" disable broadcasthost
}
run "${_HOSTS}" enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help enabled\` exits with status 0." { @test "\`help enabled\` exits with status 0." {

View File

@ -18,7 +18,7 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list\` prints lists of enabled and disabled records." { @test "\`list\` prints lists of enabled and disabled entries." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
@ -60,7 +60,7 @@ Disabled:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list enabled\` prints list of enabled records." { @test "\`list enabled\` prints list of enabled entries." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
@ -78,6 +78,18 @@ Disabled:
[[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]] [[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
} }
@test "\`list enabled\` exits with status 1 when no matching entries found." {
{
run "${_HOSTS}" disable localhost
run "${_HOSTS}" disable broadcasthost
}
run "${_HOSTS}" list enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# `hosts list disabled` ####################################################### # `hosts list disabled` #######################################################
@test "\`list disabled\` exits with status 0." { @test "\`list disabled\` exits with status 0." {
@ -94,7 +106,7 @@ Disabled:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list disabled\` prints list of disabled records." { @test "\`list disabled\` prints list of disabled entries." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
@ -110,6 +122,13 @@ Disabled:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`list disabled\` exits with status 1 when no matching entries found." {
run "${_HOSTS}" list disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# `hosts list <search string>` ################################################ # `hosts list <search string>` ################################################
@test "\`list <search string>\` exits with status 0." { @test "\`list <search string>\` exits with status 0." {
@ -125,7 +144,7 @@ Disabled:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`list <search string>\` prints list of matching records." { @test "\`list <search string>\` prints list of matching entries." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net run "${_HOSTS}" add 0.0.0.0 example.net
@ -140,7 +159,7 @@ Disabled:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`list <search string>\` prints records with matching comments." { @test "\`list <search string>\` prints entries with matching comments." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
@ -155,7 +174,7 @@ Disabled:
} }
@test "\`list <search string>\` prints disabled records with matching comments." { @test "\`list <search string>\` prints disabled entries with matching comments." {
{ {
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment" run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
@ -172,6 +191,13 @@ Disabled:
[[ "${lines[4]}" == "" ]] [[ "${lines[4]}" == "" ]]
} }
@test "\`list <search string>\` exits with status 1 when no matching entries found." {
run "${_HOSTS}" list query-that-matches-no-entries
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help list\` exits with status 0." { @test "\`help list\` exits with status 0." {

View File

@ -29,18 +29,8 @@ load test_helper
run "${_HOSTS}" search run "${_HOSTS}" search
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_expected="\ [[ "${lines[0]}" == "Usage:" ]]
Usage: [[ "${lines[1]}" == " hosts search <search string>" ]]
hosts search <search string>
Description:
Search entries for <search string>.
Exit status:
0 One or more matching entries found.
1 Invalid parameters or entry not found."
_compare "'${_expected}'" "'${output}'"
[[ "${output}" == "${_expected}" ]]
} }
# `hosts search enabled` ###################################################### # `hosts search enabled` ######################################################
@ -77,6 +67,18 @@ Exit status:
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
@test "\`search enabled\` exits with status 1 when no matching entries found." {
{
run "${_HOSTS}" disable localhost
run "${_HOSTS}" disable broadcasthost
}
run "${_HOSTS}" search enabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# `hosts search disabled` ##################################################### # `hosts search disabled` #####################################################
@test "\`search disabled\` exits with status 0." { @test "\`search disabled\` exits with status 0." {
@ -109,6 +111,13 @@ Exit status:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`search disabled\` exits with status 1 when no matching entries found." {
run "${_HOSTS}" search disabled
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# `hosts search <search string>` ################################################ # `hosts search <search string>` ################################################
@test "\`search <search string>\` exits with status 0." { @test "\`search <search string>\` exits with status 0." {
@ -170,6 +179,13 @@ Exit status:
[[ "${lines[4]}" == "" ]] [[ "${lines[4]}" == "" ]]
} }
@test "\`search <search string>\` exits with status 1 when no matching entries found." {
run "${_HOSTS}" search query-that-matches-no-entries
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help search\` exits with status 0." { @test "\`help search\` exits with status 0." {