mirror of
https://github.com/octoleo/hosts.git
synced 2024-12-28 20:12:42 +00:00
Add hosts search
function.
`hosts search` wraps `hosts list`, providing a slightly more intuitive interface.
This commit is contained in:
parent
370e1f6688
commit
73ffcffb38
@ -50,6 +50,7 @@ Usage:
|
||||
hosts file
|
||||
hosts list [enabled | disabled | <search string>]
|
||||
hosts show (<ip> | <hostname> | <search string>)
|
||||
hosts search <search string>
|
||||
hosts remove (<ip> | <hostname> | <search string>) [--force]
|
||||
```
|
||||
|
||||
@ -113,6 +114,10 @@ Open the hosts file (/etc/hosts) file in your editor.
|
||||
|
||||
Print the entire contents of the /etc/hosts file.
|
||||
|
||||
###### `hosts search <search string>`
|
||||
|
||||
Search entries for a given search string.
|
||||
|
||||
## Tests
|
||||
|
||||
To run the test suite, install [Bats](https://github.com/sstephenson/bats) and
|
||||
|
18
hosts
18
hosts
@ -1070,6 +1070,24 @@ remove() {
|
||||
printf "Removed:\n%s\n" "${target_records}"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------- search
|
||||
|
||||
desc "search" <<HEREDOC
|
||||
Usage:
|
||||
$_ME search <search string>
|
||||
|
||||
Description:
|
||||
Search entries for <search string>.
|
||||
HEREDOC
|
||||
search() {
|
||||
if _blank "${_COMMAND_ARGV[1]:-}"
|
||||
then
|
||||
$_ME help "search"
|
||||
return 1
|
||||
fi
|
||||
list "$@"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------ show
|
||||
|
||||
desc "show" <<HEREDOC
|
||||
|
151
test/search.bats
Normal file
151
test/search.bats
Normal file
@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts search` #############################################################
|
||||
|
||||
@test "\`search\` with no arguments exits with status 1." {
|
||||
{
|
||||
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}" search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@test "\`search\` with no arguments prints help information." {
|
||||
{
|
||||
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}" search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
_expected="\
|
||||
Usage:
|
||||
hosts search <search string>
|
||||
|
||||
Description:
|
||||
Search entries for <search string>."
|
||||
_compare "'${_expected}'" "'${output}'"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
|
||||
# `hosts search enabled` ######################################################
|
||||
|
||||
@test "\`search enabled\` exits with status 0." {
|
||||
{
|
||||
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}" search enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search enabled\` prints list of enabled 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.2 example.com
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
}
|
||||
|
||||
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" ]]
|
||||
}
|
||||
|
||||
# `hosts search disabled` #######################################################
|
||||
|
||||
@test "\`search disabled\` exits with status 0." {
|
||||
{
|
||||
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}" disable example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search disabled\` prints list of disabled 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}" disable example.com
|
||||
}
|
||||
|
||||
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[2]}" == "" ]]
|
||||
}
|
||||
|
||||
# `hosts search <search string>` ################################################
|
||||
|
||||
@test "\`search <search string>\` exits with status 0." {
|
||||
{
|
||||
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}" search example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search <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}" 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[2]}" == "" ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
|
||||
@test "\`help search\` exits with status 0." {
|
||||
run "${_HOSTS}" help search
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`help search\` prints help information." {
|
||||
run "${_HOSTS}" help search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts search <search string>" ]]
|
||||
}
|
Loading…
Reference in New Issue
Block a user