From 73ffcffb3835c0a173f9eeecd9dc185dc8b5967d Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 1 Mar 2017 12:46:27 -0800 Subject: [PATCH] Add `hosts search` function. `hosts search` wraps `hosts list`, providing a slightly more intuitive interface. --- README.md | 5 ++ hosts | 18 ++++++ test/search.bats | 151 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 test/search.bats diff --git a/README.md b/README.md index c3e6ced..65f95ac 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Usage: hosts file hosts list [enabled | disabled | ] hosts show ( | | ) + hosts search hosts remove ( | | ) [--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 entries for a given search string. + ## Tests To run the test suite, install [Bats](https://github.com/sstephenson/bats) and diff --git a/hosts b/hosts index f7515df..975b285 100755 --- a/hosts +++ b/hosts @@ -1070,6 +1070,24 @@ remove() { printf "Removed:\n%s\n" "${target_records}" } +# ---------------------------------------------------------------------- search + +desc "search" < + +Description: + Search entries for . +HEREDOC +search() { + if _blank "${_COMMAND_ARGV[1]:-}" + then + $_ME help "search" + return 1 + fi + list "$@" +} + # ------------------------------------------------------------------------ show desc "show" <` ################################################ + +@test "\`search \` 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 \` 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 " ]] +}