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

Improve search behavior.

Search comments in addition to IPs and hostnames.

Call grep twice, excluding commented lines first before searching.
This commit is contained in:
William Melody 2018-08-14 11:14:55 -07:00
parent b44f3b8fe0
commit 789a0797c2
4 changed files with 75 additions and 3 deletions

2
hosts
View File

@ -1173,7 +1173,7 @@ show() {
local _enabled_records local _enabled_records
_enabled_records=$( _enabled_records=$(
grep "^[^#]*${1}" "${HOSTS_PATH}" grep --invert-match "^#" "${HOSTS_PATH}" | grep "${1}"
) )
# Output disabled records secondly for better organization. # Output disabled records secondly for better organization.

View File

@ -139,6 +139,20 @@ Disabled:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`search <search string>\` prints records with matching comments." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
run "${_HOSTS}" add 127.0.0.1 example.com
}
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[2]}" == "" ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help list\` exits with status 0." { @test "\`help list\` exits with status 0." {

View File

@ -135,6 +135,20 @@ Description:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`search <search string>\` prints records with matching comments." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
run "${_HOSTS}" add 127.0.0.1 example.com
}
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[2]}" == "" ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help search\` exits with status 0." { @test "\`help search\` exits with status 0." {

View File

@ -35,7 +35,7 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <ip>\` shows all matches." { @test "\`show <ip>\` shows all matches." {
{ {
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
@ -67,7 +67,7 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enable <hostname>\` shows all matches." { @test "\`show <hostname>\` shows all matches." {
{ {
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
@ -83,6 +83,50 @@ load test_helper
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]] [[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
} }
# `hosts show <search string>` ################################################
@test "\`list <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}" show example.com
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]]
}
@test "\`list <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[2]}" == "" ]]
}
@test "\`search <search string>\` prints records with matching comments." {
{
run "${_HOSTS}" add 0.0.0.0 example.com
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
run "${_HOSTS}" add 127.0.0.1 example.com
}
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[2]}" == "" ]]
}
# help ######################################################################## # help ########################################################################
@test "\`help show\` exits with status 0." { @test "\`help show\` exits with status 0." {