1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-06-01 14:00:52 +00:00

Compare commits

...

102 Commits

Author SHA1 Message Date
William Melody
8916c43efa Add .github configuration. 2023-06-27 16:59:08 -07:00
William Melody
9ea716f861
Use two-step homebrew install in README. 2020-10-06 23:13:23 -07:00
William Melody
9a929dc70f Update to version 3.6.4 2020-09-11 18:22:23 -07:00
William Melody
80699292a4 Use 'entries' terminology consistently. 2020-09-11 17:46:18 -07:00
William Melody
06ad3e8deb Add exit status to list and associated subcommands. 2020-09-11 17:40:47 -07:00
William Melody
c74671613b Improve exit status documentation. 2020-09-11 17:22:02 -07:00
William Melody
0201480ffb Group helper variables and functions in Helpers section. 2020-09-11 17:01:29 -07:00
William Melody
0ce875c210 Use sed -i via a _sed_i() wrapper. 2020-09-11 17:01:29 -07:00
William Melody
6a58eb693d
Update README 2020-08-23 13:34:58 -07:00
William Melody
245188025a Improve realpath / readlink handling. 2020-08-16 10:14:48 -07:00
William Melody
39a182b47e Update Usage: ordering in backups. 2020-06-08 09:49:36 -07:00
William Melody
bd7edba09b Improve spacing and comments in subcommands. 2020-06-08 09:42:58 -07:00
William Melody
5049abe817 Use alpha ordering for subcommand handling in backup. 2020-06-08 09:36:16 -07:00
William Melody
5c87ad62b0 Update version to 3.6.3 2020-06-07 18:07:12 -07:00
William Melody
ca51f3f742 Remove extra newline. 2020-06-07 18:05:18 -07:00
William Melody
ade1109cba Add missing _warn statements. 2020-06-07 17:22:20 -07:00
William Melody
3c383581c5 Update version to 3.6.2 2020-06-07 17:17:00 -07:00
William Melody
2346f6f814 Add missing _download_from() function. 2020-06-07 17:15:51 -07:00
William Melody
0715b9ec66 Update version to 3.6.1 2020-06-07 15:42:50 -07:00
William Melody
42a2a9f3d4 Use __ prefix for variables in loops. 2020-06-07 15:42:11 -07:00
William Melody
c11f22e558 Update error message in _main(). 2020-06-07 15:38:29 -07:00
William Melody
0d5f41537d Fix spacing in option parsing. 2020-06-07 15:13:23 -07:00
William Melody
0a98202f84 Remove additional _debug() statement. 2020-06-07 15:06:29 -07:00
William Melody
040593bd51 Improve _load_subcommands(). 2020-06-07 15:04:45 -07:00
William Melody
0b79763874 Update Make install instructions. 2020-06-07 14:59:31 -07:00
William Melody
d6e3a8a128 Update version to 3.6.0 2020-06-07 14:57:43 -07:00
William Melody
0fa9b240c6 Remove extra space in _load_subcommands(). 2020-06-07 14:51:12 -07:00
William Melody
83bb517312 Update help and README.md for subcommands. 2020-06-07 14:49:59 -07:00
William Melody
d396c5eefb Fix order in $_SUBCOMMANDS. 2020-06-07 14:44:57 -07:00
William Melody
8ab47640f0 Rename commands to subcommands. 2020-06-07 14:44:10 -07:00
William Melody
c3be802faf Fix function name in _debug statement. 2020-06-07 14:41:35 -07:00
William Melody
768acb5d80 Rename $_DEFINED_COMMANDS to $_DEFINED_SUBCOMMANDS. 2020-06-07 14:41:05 -07:00
William Melody
379f99f4e1 Rename _load_commands() to _load_subcommands(). 2020-06-07 14:40:03 -07:00
William Melody
28fea3a599 Rename global variable to $HOSTS_DEFAULT_SUBCOMMAND. 2020-06-07 14:39:18 -07:00
William Melody
bf65fbcad8 Replace $_CMD with $_SUBCOMMAND. 2020-06-07 14:37:10 -07:00
William Melody
5d4a9dfb28 Remove extra comments in _main(). 2020-06-07 14:36:21 -07:00
William Melody
0bd86e533d Move program functions to bottom of script. 2020-06-07 14:29:41 -07:00
William Melody
a39c60aafa Use curly braces for all $_ME variable references. 2020-06-07 14:18:20 -07:00
William Melody
1da838faeb Remove extra comment. 2020-06-07 14:17:02 -07:00
William Melody
6e1d408ad5 Improve header formatting. 2020-06-07 14:14:13 -07:00
William Melody
c381c8bd98 Add MIT license text to header.
resolves gh-10
2020-06-07 14:11:15 -07:00
William Melody
426f2ed602 Improve global variable comments. 2020-06-07 14:04:37 -07:00
William Melody
7596a2901f Improve variable names in _print_entries(). 2020-06-07 14:00:28 -07:00
William Melody
4e9ad74697 Improve alignment. 2020-06-07 13:57:07 -07:00
William Melody
051e8b3840 Don't use quotes when assigning empty strings. 2020-06-07 13:54:21 -07:00
William Melody
f585447612 Improve variable handling in _print_entries(). 2020-06-07 13:51:00 -07:00
William Melody
4238e8bd81 Add completions help to README.md 2020-06-07 13:45:14 -07:00
William Melody
a64c20bdd8 Rearrange commands in script. 2020-06-07 13:43:04 -07:00
William Melody
5865f92977 Remove version from default help output. 2020-06-07 13:40:11 -07:00
William Melody
2b81ea530f Remove unused utility functions. 2020-06-07 13:39:01 -07:00
William Melody
1e6af9b309 Improve comment in $_SED_I_COMMAND assignment. 2020-06-07 13:38:17 -07:00
William Melody
47b5d51fa3 Remove unused helper functions. 2020-06-07 13:36:33 -07:00
William Melody
d775353c9a Use quotes when assigning function name. 2020-06-07 13:35:21 -07:00
William Melody
73ecc3119e Clean up program option parsing section. 2020-06-07 13:34:00 -07:00
William Melody
d242aed463 Remove obsolete $_COMMAND_ARGV variable. 2020-06-07 13:31:46 -07:00
William Melody
9bf7f782c9 Use improved _contains(). 2020-06-07 13:26:27 -07:00
William Melody
f0ebb08cd4 Use improved _join(). 2020-06-07 13:25:03 -07:00
William Melody
4aeab14d2d Remove obsolete _command_argv_includes(). 2020-06-07 13:23:54 -07:00
William Melody
952e2465b3 Add _warn() and use in completions. 2020-06-07 13:20:33 -07:00
William Melody
23d6a1c653 Move completion installation into the main script. 2020-06-07 13:20:12 -07:00
William Melody
17b2615820 Improve header, with note about LICENSE.
gh-10
2020-06-07 13:19:43 -07:00
William Melody
68b421f95a Combine Environment and Globals. 2020-06-07 12:45:07 -07:00
William Melody
a09e7b2468 Improve section dividers. 2020-06-07 12:40:44 -07:00
William Melody
dfbc83e12c Improve _debug(). 2020-06-07 12:34:55 -07:00
William Melody
fff87648d4 Update version to 3.5.1 2020-05-17 11:39:26 -07:00
William Melody
8f6a700909 Improve formatting in tests. 2020-05-17 11:33:57 -07:00
William Melody
1d948837d0 Use _exit_1 with non-help error messages. 2020-05-17 11:28:31 -07:00
William Melody
8f2553b5a3 Add $_ERROR_PREFIX to test_helper.bash. 2020-05-17 11:25:28 -07:00
William Melody
2f6290f3d4 Quote arguments to help. 2020-05-17 11:24:38 -07:00
William Melody
16a672d52a Improve code formatting. 2020-05-17 11:10:12 -07:00
William Melody
16a95406fe Replace return 1 with message with _return_1. 2020-05-17 11:03:24 -07:00
William Melody
5a7d0cca91 Replace _die() with _exit_1(). 2020-05-17 11:01:25 -07:00
William Melody
40be65b7da _verify_write_permissions() doesn't accept arguments. 2020-05-17 10:57:33 -07:00
William Melody
affd8d8657 Use brackets in _verify_write_permissions(). 2020-05-17 10:55:36 -07:00
William Melody
6e751065de Improve backups argument handling. 2020-05-17 10:54:03 -07:00
William Melody
3e326dad13 Unenable shellcheck SC2183 for printf format string. 2020-05-17 10:37:51 -07:00
William Melody
6bde3438c6 Don't use $/${} with arithmetic variables.
ShellCheck SC2004
https://github.com/koalaman/shellcheck/wiki/SC2004
2020-05-17 10:34:59 -07:00
William Melody
ca43d92d85 Remove unused variable. 2020-05-17 10:33:15 -07:00
William Melody
bc06de7476 Update version to 3.5.0 2020-04-22 12:15:15 -07:00
William Melody
9dab3606c6 Add exit status documentation to README.md 2020-04-22 12:08:02 -07:00
William Melody
88030b8a51 Return 1 from show when entry not found.
GH-9
2020-04-22 12:07:35 -07:00
William Melody
5112c3e6a0 Use consistent terminology in function documentation.
GH-9
2020-04-22 12:07:22 -07:00
William Melody
2f5f610e3c Reformat exit status documentation.
GH-9
2020-04-22 12:07:13 -07:00
William Melody
815def022e Remove .gitignore.
GH-9
2020-04-22 12:06:57 -07:00
William Melody
59bb99ea34 Merge branch 'sleepyghost01-feature/add-exit-codes'
* sleepyghost01-feature/add-exit-codes:
  Add exit code documentation
2020-04-22 12:05:37 -07:00
William Melody
40b76d2e26 Update version to 3.4.3 2020-04-16 11:48:00 -07:00
William Melody
bc6b16c014 Improve hosts-completion script locations. 2020-04-16 11:46:45 -07:00
Amin Mousavi
a67e4c997b Add exit code documentation
Problem:
It's not clear in documentation which exit code will return for each command specially commands that might return 1 when there's no action to do.

Solution:
- All possible documentation has been added.
- .ignorefile has been missed which could cause problem for other contribution so I add one by help of https://www.gitignore.io
2020-04-16 12:11:40 +02:00
William Melody
4088591bb2 Update version to 3.4.2 2020-04-14 13:21:03 -07:00
William Melody
972dc9a752 Improve README.md 2020-04-14 13:17:18 -07:00
William Melody
95807387a1 Improve README.md 2020-04-14 13:16:29 -07:00
William Melody
952d848157 Update version to 3.4.1 2020-04-14 13:06:40 -07:00
William Melody
89b016999d Remove duplicate heading in README.md 2020-04-14 13:05:47 -07:00
William Melody
91946962a6 Fix heading level in README.md 2020-04-14 13:03:37 -07:00
William Melody
886599394e Update version to 3.4.0 2020-04-14 10:13:31 -07:00
William Melody
45b39d1bc7 Update README.md 2020-04-14 10:10:20 -07:00
William Melody
6159d3e35a Update etc/README.md title. 2020-04-14 10:05:59 -07:00
William Melody
3d65bccdb5 Manage completion with scripts/hosts-completion. 2020-04-14 09:26:53 -07:00
William Melody
8cbbdc3550 Add scripts, update README.md and package.json. 2020-04-13 21:39:39 -07:00
William Melody
9cbc6ddafb Update version to 3.3.3 2020-04-13 16:49:08 -07:00
William Melody
809bb45121 Update heading in README.md 2020-04-13 16:37:45 -07:00
William Melody
638da3200e Reconfigure completion setup. 2020-04-13 16:36:58 -07:00
23 changed files with 1376 additions and 706 deletions

2
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,2 @@
github: xwmx
custom: https://paypal.me/WilliamMelody

84
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,84 @@
###############################################################################
# .github/workflows/tests.yml
#
# NOTE: GitHub Actions does not allocate a TTY, preventing detection of
# piped input using `[[ -t 0 ]]`.
#
# More information:
#
# https://github.com/actions/runner/issues/241
#
# faketty is a GitHub action that uses work-arounds to provide a tty:
#
# https://github.com/marketplace/actions/faketty
#
# Scripts used by faketty for each platform:
#
# linux: `faketty`
#
# ```bash
# #!/bin/bash
#
# script -q -e -c "$*"
# ```
#
# win32: `faketty.ps1`
#
# ```posh
# Invoke-Expression "$args"
# ```
#
# darwin: `faketty`
#
# requires: `brew install expect`
#
# ```bash
# #!/bin/bash
#
# unbuffer $*
# ```
###############################################################################
name: "hosts · Test Suite"
on:
pull_request:
branches: [ master ]
push:
branches: [ master ]
workflow_dispatch:
jobs:
test-macos-11:
name: "Test: macOS Big Sur 11.0"
runs-on: macos-11.0
steps:
- uses: actions/checkout@v3
- name: "Setup"
run: |
brew update
brew install bats-core
brew install expect
- name: "Set $TERM=xterm"
run: printf "TERM=xterm\\n" >> $GITHUB_ENV
- name: "Run bats tests"
run: unbuffer bats test
test-ubuntu-latest:
name: "Test: Ubuntu Latest"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: "Setup"
run: |
sudo apt-get update
sudo apt-get install bats -y
- name: "Install bats-core"
run: |
git clone https://github.com/bats-core/bats-core.git "${HOME}/bats-core" &&
cd "${HOME}/bats-core"
git checkout 2e2e5df6adf0b846b411b6b2f7bb654cbc3e2c4e
sudo ./install.sh /usr/local
- name: "Set $TERM=xterm"
run: printf "TERM=xterm\\n" >> $GITHUB_ENV
- name: "Run bats tests"
run: script -q -e -c "bats test"

View File

@ -5,6 +5,14 @@
# https://github.com/koalaman/shellcheck/wiki/Ignore # https://github.com/koalaman/shellcheck/wiki/Ignore
############################################################################### ###############################################################################
# Disable SC2183
#
# Running into this: https://github.com/koalaman/shellcheck/issues/1310
# TODO: Check if resolved.
#
#https://github.com/koalaman/shellcheck/wiki/SC2183
disable=SC2183
# Disable SC2206 and SC2207 # Disable SC2206 and SC2207
# #
# `IFS` and `noglob` are set. # `IFS` and `noglob` are set.

View File

@ -1,18 +0,0 @@
jobs:
include:
- os: linux
addons:
apt:
sources:
- sourceline: 'ppa:duggan/bats'
packages:
- bats
dist: bionic
language: minimal
- os: osx
before_install:
- brew install bats-core
osx_image: xcode11.3
script: bats test

View File

@ -3,6 +3,8 @@ PREFIX ?= /usr/local
install: install:
install $(BIN) $(PREFIX)/bin install $(BIN) $(PREFIX)/bin
./$(BIN) completions install
uninstall: uninstall:
rm -f $(PREFIX)/bin/$(BIN) rm -f $(PREFIX)/bin/$(BIN)
./$(BIN) completions uninstall

195
README.md
View File

@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/xwmx/hosts.svg?branch=master)](https://travis-ci.org/xwmx/hosts) [![Build Status](https://img.shields.io/github/actions/workflow/status/xwmx/hosts/tests.yml?branch=master)](https://github.com/xwmx/hosts/actions)
__ __ __ __
/ /_ ____ _____/ /______ / /_ ____ _____/ /______
@ -25,7 +25,8 @@ single, portable script that can be `curl`ed into any environment.
To install with [Homebrew](http://brew.sh/): To install with [Homebrew](http://brew.sh/):
```bash ```bash
brew install xwmx/taps/hosts brew tap xwmx/taps
brew install hosts
``` ```
### npm ### npm
@ -44,19 +45,65 @@ To install with [bpkg](https://github.com/bpkg/bpkg):
bpkg install xwmx/hosts bpkg install xwmx/hosts
``` ```
### Manual ### Make
To install manually, simply add the `hosts` script to your `$PATH`. If To install with [Make](https://en.wikipedia.org/wiki/Make_(software)),
you already have a `~/bin` directory, you can use the following command: clone this repository, navigate to the clone's root directory, and run:
```bash ```bash
curl -L https://raw.github.com/xwmx/hosts/master/hosts \ sudo make install
-o ~/bin/hosts && chmod +x ~/bin/hosts
``` ```
### Manual
To install as an administrator, copy and paste one of the following multi-line
commands:
```bash
# install using wget
sudo wget https://raw.github.com/xwmx/hosts/master/hosts -O /usr/local/bin/hosts &&
sudo chmod +x /usr/local/bin/hosts &&
sudo hosts completions install
# install using curl
sudo curl -L https://raw.github.com/xwmx/hosts/master/hosts -o /usr/local/bin/hosts &&
sudo chmod +x /usr/local/bin/hosts &&
sudo hosts completions install
```
###### User-only Installation
To install with just user permissions, simply add the `hosts` script to your
`$PATH`. If you already have a `~/bin` directory, for example, you can use
one of the following commands:
```bash
# download with wget
wget https://raw.github.com/xwmx/hosts/master/hosts -O ~/bin/hosts && chmod +x ~/bin/hosts
# download with curl
curl -L https://raw.github.com/xwmx/hosts/master/hosts -o ~/bin/hosts && chmod +x ~/bin/hosts
```
Installing with just user permissions doesn't install the completions, but
`hosts` works without them. If you have `sudo` access and want to install the
completion scripts, run the following command:
```bash
sudo hosts completions install
```
### Arch Linux
A package for Arch users is also A package for Arch users is also
[available in the AUR](https://aur.archlinux.org/packages/hosts/). [available in the AUR](https://aur.archlinux.org/packages/hosts/).
### Tab Completion
Bash and Zsh tab completion is enabled when `hosts` is installed using
Homebrew, npm, bpkg, or Make. If you are installing `hosts` manually,
[completion can be enabled with a few commands](etc/README.md).
## Usage ## Usage
### Listing Entries ### Listing Entries
@ -113,7 +160,7 @@ address, domain, or regular expression:
```bash ```bash
> hosts remove example.com > hosts remove example.com
Removing the following records: Removing the following entries:
127.0.0.1 example.com 127.0.0.1 example.com
Are you sure you want to proceed? [y/N] y Are you sure you want to proceed? [y/N] y
Removed: Removed:
@ -145,7 +192,7 @@ Removed:
### Enabling / Disabling Entries ### Enabling / Disabling Entries
Add entries are enabled by default. Disabiling an entry comments it out All entries are enabled by default. Disabiling an entry comments it out
so it has no effect, but remains in the hosts file ready to be enabled so it has no effect, but remains in the hosts file ready to be enabled
again. again.
@ -285,6 +332,7 @@ Usage:
hosts add <ip> <hostname> [<comment>] hosts add <ip> <hostname> [<comment>]
hosts backups [create | (compare | delete | restore | show) <filename>] hosts backups [create | (compare | delete | restore | show) <filename>]
hosts block <hostname>... hosts block <hostname>...
hosts completions (check | install [-d | --download] | uninstall)
hosts disable (<ip> | <hostname> | <search string>) hosts disable (<ip> | <hostname> | <search string>)
hosts disabled hosts disabled
hosts edit hosts edit
@ -294,6 +342,7 @@ Usage:
hosts list [enabled | disabled | <search string>] hosts list [enabled | disabled | <search string>]
hosts search <search string> hosts search <search string>
hosts show (<ip> | <hostname> | <search string>) hosts show (<ip> | <hostname> | <search string>)
hosts subcommands [--raw]
hosts remove (<ip> | <hostname> | <search string>) [--force] hosts remove (<ip> | <hostname> | <search string>) [--force]
hosts unblock <hostname>... hosts unblock <hostname>...
hosts --auto-sudo hosts --auto-sudo
@ -321,7 +370,29 @@ For help with a particular command, try:
hosts help <command name> hosts help <command name>
``` ```
## Commands ## Subcommands
<p align="center">
<a href="#hosts-1">(default)</a>
<a href="#hosts-add">add</a>
<a href="#hosts-backups">backups</a>
<a href="#hosts-block">block</a>
<a href="#hosts-completions">completions</a>
<a href="#hosts-disable">disable</a>
<a href="#hosts-disabled">disabled</a>
<a href="#hosts-edit">edit</a>
<a href="#hosts-enable">enable</a>
<a href="#hosts-enabled">enabled</a>
<a href="#hosts-file">file</a>
<a href="#hosts-help">help</a>
<a href="#hosts-list">list</a>
<a href="#hosts-remove">remove</a>
<a href="#hosts-search">search</a>
<a href="#hosts-show">show</a>
<a href="#hosts-subcommands">subcommands</a>
<a href="#hosts-unblock">unblock</a>
<a href="#hosts-version">version</a>
</p>
### `hosts` ### `hosts`
@ -331,7 +402,7 @@ Usage:
Description: 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 entries will
be printed. be printed.
Alias for `hosts list` Alias for `hosts list`
@ -345,6 +416,10 @@ Usage:
Description: Description:
Add a given IP address and hostname pair, along with an optional comment. Add a given IP address and hostname pair, along with an optional comment.
Exit status:
0 Entry successfully added.
1 Invalid parameters or entry exists.
``` ```
### `hosts backups` ### `hosts backups`
@ -371,6 +446,10 @@ Subcommands:
Description: Description:
Manage backups. Manage backups.
Exit status:
0 Success.
1 Invalid parameters or backup not found.
``` ```
### `hosts block` ### `hosts block`
@ -382,6 +461,10 @@ Usage:
Description: Description:
Block one or more hostnames by adding new entries assigned to `127.0.0.1` Block one or more hostnames by adding new entries assigned to `127.0.0.1`
for IPv4 and both `fe80::1%lo0` and `::1` for IPv6. for IPv4 and both `fe80::1%lo0` and `::1` for IPv6.
Exit status:
0 <hostname> successfully blocked.
1 Invalid parameters or entry exists.
``` ```
#### Blocklists #### Blocklists
@ -389,17 +472,22 @@ Description:
- [jmdugan/blocklists](https://github.com/jmdugan/blocklists) - [jmdugan/blocklists](https://github.com/jmdugan/blocklists)
- [notracking/hosts-blocklists](https://github.com/notracking/hosts-blocklists) - [notracking/hosts-blocklists](https://github.com/notracking/hosts-blocklists)
### `hosts commands` ### `hosts completions`
```text ```text
Usage: Usage:
hosts commands [--raw] hosts completions (check | install [-d | --download] | uninstall)
Options: Options:
--raw Display the command list without formatting. -d, --download Download the completion scripts and install.
Description: Description:
Display the list of available commands. Manage completion scripts. For more information, visit:
https://github.com/xwmx/hosts/blob/master/etc/README.md
Exit status:
0 Completions successfully installed.
1 Invalid parameters or other error.
``` ```
### `hosts disable` ### `hosts disable`
@ -409,8 +497,12 @@ Usage:
hosts disable (<ip> | <hostname> | <search string>) hosts disable (<ip> | <hostname> | <search string>)
Description: Description:
Disable one or more records based on a given ip address, hostname, or Disable one or more entries based on a given ip address, hostname, or
search string. search string.
Exit status:
0 Entry successfully disabled.
1 Invalid parameters or entry not found.
``` ```
### `hosts disabled` ### `hosts disabled`
@ -420,7 +512,11 @@ Usage:
hosts disabled hosts disabled
Description: Description:
List all disabled records. This is an alias for `hosts list disabled`. List all disabled entries. 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`
@ -440,8 +536,12 @@ Usage:
hosts enable (<ip> | <hostname> | <search string>) hosts enable (<ip> | <hostname> | <search string>)
Description: Description:
Enable one or more disabled records based on a given ip address, hostname, Enable one or more disabled entries based on a given ip address, hostname,
or search string. or search string.
Exit status:
0 Entry successfully enabled.
1 Invalid parameters or entry not found.
``` ```
### `hosts enabled` ### `hosts enabled`
@ -451,7 +551,11 @@ Usage:
hosts enabled hosts enabled
Description: Description:
List all enabled records. This is an alias for `hosts list enabled`. List all enabled entries. 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`
@ -482,8 +586,12 @@ Usage:
Description: 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 entries 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`
@ -497,9 +605,13 @@ Options:
--force Skip the confirmation prompt. --force Skip the confirmation prompt.
Description: Description:
Remove one or more records based on a given IP address, hostname, or search Remove one or more entries based on a given IP address, hostname, or search
string. If an IP and hostname are both provided, only records matching the string. If an IP and hostname are both provided, only entries matching the
IP and hostname pair will be removed. IP and hostname pair will be removed.
Exit status:
0 Entry successfully removed.
1 Invalid parameters or entry not found.
``` ```
### `hosts search` ### `hosts search`
@ -510,6 +622,10 @@ Usage:
Description: Description:
Search entries for <search string>. Search entries for <search string>.
Exit status:
0 One or more matching entries found.
1 Invalid parameters or entry not found.
``` ```
### `hosts show` ### `hosts show`
@ -520,6 +636,23 @@ Usage:
Description: Description:
Print entries matching a given IP address, hostname, or search string. Print entries matching a given IP address, hostname, or search string.
Exit status:
0 One or more matching entries found.
1 Invalid parameters or entry not found.
```
### `hosts subcommands`
```text
Usage:
hosts subcommands [--raw]
Options:
--raw Display the subcommands list without formatting.
Description:
Display the list of available subcommands.
``` ```
### `hosts unblock` ### `hosts unblock`
@ -530,6 +663,10 @@ Usage:
Description: Description:
Unblock one or more hostnames by removing the entries from the hosts file. Unblock one or more hostnames by removing the entries from the hosts file.
Exit status:
0 <hostname> successfully unblocked.
1 Invalid parameters or entry not found
``` ```
### `hosts version` ### `hosts version`
@ -544,12 +681,14 @@ Description:
## Tests ## Tests
To run the test suite, install [Bats](https://github.com/sstephenson/bats) and To run the [test suite](test), install [Bats](https://github.com/sstephenson/bats) and
run `bats test` in the project root directory. run `bats test` in the project root directory.
## Acknowledgements ---
<p align="center">
- https://gist.github.com/nddrylliog/1368532 Copyright (c) 2015-present William Melody • See LICENSE for details.
- https://gist.github.com/dfeyer/1369760 </p>
- https://github.com/macmade/host-manager
<p align="center">
<a href="https://github.com/xwmx/hosts">github.com/xwmx/hosts</a>
</p>

113
etc/README.md Normal file
View File

@ -0,0 +1,113 @@
# `hosts` Tab Completion
## Homebrew
Installing via Homebrew with `brew install xwmx/taps/hosts` will also
install the completion scripts.
A one-time setup might be needed to [enable completion for all Homebrew
programs](https://docs.brew.sh/Shell-Completion).
## npm, bpkg, Make
When `hosts` is installed with `npm`, `bpkg`, or Make, an install hook will
check the environment and attempt to install completions. If it's successful,
you should see a message similar to:
```bash
Completion installed: /usr/local/etc/bash_completion.d/hosts
Completion installed: /usr/local/share/zsh/site-functions/_hosts
```
If completion is working after installing through any of these methods, then
you don't need to do anything else.
## `hosts completions`
The `hosts completions` subcommand can be used for installing and uninstalling
completion scripts. Depending on your configuration, you might need to use
`sudo` to install completion scripts easily:
```bash
> sudo hosts completions check
Completion scripts not found.
> sudo hosts completions install
Completion script installed: /usr/share/bash-completion/completions/hosts
Completion script installed: /usr/local/share/zsh/site-functions/_hosts
> sudo hosts completions check
Exists: /usr/share/bash-completion/completions/hosts
Exists: /usr/local/share/zsh/site-functions/_hosts
> sudo hosts completions uninstall
Completion script removed: /usr/share/bash-completion/completions/hosts
Completion script removed: /usr/local/share/zsh/site-functions/_hosts
```
If you installed `hosts` manually by downloading just the `hosts` script,
the completion scripts won't be immediately available for
`hosts completions install`. You can try installing the completions with
the `--download` flag, which will get the latest version from GitHub:
```bash
sudo hosts completions install --download
```
`hosts completions` will try to determine the completion script directories
from your environment. If `hosts completions` isn't able to install
the completion scripts, you can try installing them manually.
## Manual Installation
### bash
#### Linux
On a current Linux OS (in a non-minimal installation), bash completion should
be available.
Place the completion script in `/etc/bash_completion.d/`:
```bash
sudo curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.bash -o /etc/bash_completion.d/hosts
```
#### macOS
If you aren't installing with homebrew, source the completion script in
`.bash_profile`:
```sh
if [[ -f /path/to/hosts-completion.bash ]]
then
source /path/to/hosts-completion.bash
fi
```
### zsh
Place the completion script in your `/path/to/zsh/completion` (typically
`~/.zsh/completion/`):
```bash
$ mkdir -p ~/.zsh/completion
$ curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.zsh > ~/.zsh/completion/_hosts
```
Include the directory in your `$fpath` by adding in `~/.zshrc`:
```bash
fpath=(~/.zsh/completion $fpath)
```
Make sure `compinit` is loaded or do it by adding in `~/.zshrc`:
```bash
autoload -Uz compinit && compinit -i
```
Then reload your shell:
```bash
exec $SHELL -l
```

1426
hosts

File diff suppressed because it is too large Load Diff

2
package-lock.json generated
View File

@ -1,5 +1,5 @@
{ {
"name": "hosts.sh", "name": "hosts.sh",
"version": "3.3.2", "version": "3.6.4",
"lockfileVersion": 1 "lockfileVersion": 1
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "hosts.sh", "name": "hosts.sh",
"version": "3.3.2", "version": "3.6.4",
"description": "A command line tool for managing hosts file entries.", "description": "A command line tool for managing hosts file entries.",
"global": true, "global": true,
"install": "make install", "install": "make install",
@ -11,7 +11,9 @@
"test": "test" "test": "test"
}, },
"scripts": { "scripts": {
"test": "bats test" "test": "bats test",
"postinstall": "./hosts completions install",
"preuninstall": "./hosts completions uninstall"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -85,7 +85,7 @@ No backups found. Create a new backup:
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups compare "${_backup_basename}" --diff run "${_HOSTS}" backups compare "${_backup_basename}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
printf "\${lines[1]}: '%s'\\n" "${lines[1]}" printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
[[ ${status} -eq 1 ]] [[ ${status} -eq 1 ]]
@ -100,7 +100,7 @@ No backups found. Create a new backup:
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups compare --diff run "${_HOSTS}" backups compare
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]] [[ ${status} -eq 1 ]]
@ -115,7 +115,7 @@ No backups found. Create a new backup:
run "${_HOSTS}" add 0.0.0.0 example.com run "${_HOSTS}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups compare "invalid-backup-name" --diff run "${_HOSTS}" backups compare "invalid-backup-name"
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]] [[ ${status} -eq 1 ]]
@ -128,6 +128,7 @@ No backups found. Create a new backup:
{ {
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
[[ -e "${_backup_path}" ]]
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
} }
@ -230,6 +231,7 @@ No backups found. Create a new backup:
{ {
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
[[ -e "${_backup_path:-}" ]]
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
} }

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts block` ################################################################# # `hosts block` ###############################################################
@test "\`block\` with no arguments exits with status 1." { @test "\`block\` with no arguments exits with status 1." {
run "${_HOSTS}" block run "${_HOSTS}" block
@ -28,7 +28,7 @@ load test_helper
[[ "${lines[1]}" == " hosts block <hostname>..." ]] [[ "${lines[1]}" == " hosts block <hostname>..." ]]
} }
# `hosts block <hostname>` ################################################# # `hosts block <hostname>` ####################################################
@test "\`block <hostname>\` exits with status 0." { @test "\`block <hostname>\` exits with status 0." {
run "${_HOSTS}" block example.com run "${_HOSTS}" block example.com

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts disabled` ############################################################# # `hosts disabled` ############################################################
@test "\`disabled\` with no arguments exits with status 0." { @test "\`disabled\` with no arguments exits with status 0." {
{ {
@ -18,7 +18,7 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`disabled\` with no arguments prints list of disabled records." { @test "\`disabled\` with no arguments 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
@ -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

@ -18,7 +18,7 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`enabled\` with no arguments prints list of enabled records." { @test "\`enabled\` with no arguments 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
@ -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

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

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts list` ############################################################# # `hosts list` ################################################################
@test "\`list\` exits with status 0." { @test "\`list\` exits with status 0." {
{ {
@ -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
@ -71,11 +71,23 @@ Disabled:
run "${_HOSTS}" list enabled run "${_HOSTS}" list enabled
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 127\.0\.0\.1[[:space:]]+localhost ]] [[ "${lines[0]}" =~ 127\.0\.0\.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255\.255\.255\.255[[:space:]]+broadcasthost ]] [[ "${lines[1]}" =~ 255\.255\.255\.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${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` #######################################################
@ -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

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts remove` ################################################################# # `hosts remove` ##############################################################
@test "\`remove\` with no arguments exits with status 1." { @test "\`remove\` with no arguments exits with status 1." {
run "${_HOSTS}" remove run "${_HOSTS}" remove
@ -53,7 +53,7 @@ load test_helper
run "${_HOSTS}" remove 127.0.0.3 --force run "${_HOSTS}" remove 127.0.0.3 --force
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${output} == "No matching records found." ]] [[ ${output} == "${_ERROR_PREFIX}No matching entries found." ]]
} }
# `hosts remove <ip> --force` ################################################# # `hosts remove <ip> --force` #################################################
@ -83,9 +83,9 @@ load test_helper
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
_compare "${_original}" "$(cat "${HOSTS_PATH}")" _compare "${_original}" "$(cat "${HOSTS_PATH}")"
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]] [[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]] [[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
} }
@test "\`remove <ip>\` removes all matches." { @test "\`remove <ip>\` removes all matches." {

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts search` ############################################################# # `hosts search` ##############################################################
@test "\`search\` with no arguments exits with status 1." { @test "\`search\` with no arguments exits with status 1." {
{ {
@ -29,14 +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>."
_compare "'${_expected}'" "'${output}'"
[[ "${output}" == "${_expected}" ]]
} }
# `hosts search enabled` ###################################################### # `hosts search enabled` ######################################################
@ -55,7 +49,7 @@ Description:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`search enabled\` prints list of enabled records." { @test "\`search 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
@ -66,14 +60,26 @@ Description:
run "${_HOSTS}" search enabled run "${_HOSTS}" search enabled
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]] [[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]] [[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]] [[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]] [[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]] [[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
} }
# `hosts search disabled` ####################################################### @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` #####################################################
@test "\`search disabled\` exits with status 0." { @test "\`search disabled\` exits with status 0." {
{ {
@ -89,7 +95,7 @@ Description:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`search disabled\` prints list of disabled records." { @test "\`search 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
@ -100,11 +106,18 @@ Description:
run "${_HOSTS}" search disabled run "${_HOSTS}" search disabled
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]] [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]] [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${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." {
@ -120,7 +133,7 @@ Description:
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
} }
@test "\`search <search string>\` prints list of matching records." { @test "\`search <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
@ -130,12 +143,12 @@ Description:
run "${_HOSTS}" search example.com run "${_HOSTS}" search example.com
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]] [[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]] [[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`search <search string>\` prints records with matching comments." { @test "\`search <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"
@ -149,7 +162,7 @@ Description:
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`search <search string>\` prints disabled records with matching comments." { @test "\`search <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"
@ -161,11 +174,18 @@ Description:
run "${_HOSTS}" search "Comment" run "${_HOSTS}" search "Comment"
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]] [[ "${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[3]}" =~ 127.0.0.1[[:space:]]+example.biz[[:space:]]+\#\ Example\ Comment ]]
[[ "${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." {
@ -177,6 +197,6 @@ Description:
run "${_HOSTS}" help search run "${_HOSTS}" help search
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" == "Usage:" ]] [[ "${lines[0]}" == "Usage:" ]]
[[ "${lines[1]}" == " hosts search <search string>" ]] [[ "${lines[1]}" == " hosts search <search string>" ]]
} }

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts show` ############################################################## # `hosts show` ################################################################
@test "\`show\` with no arguments exits with status 1." { @test "\`show\` with no arguments exits with status 1." {
run "${_HOSTS}" show run "${_HOSTS}" show
@ -19,7 +19,25 @@ load test_helper
[[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]] [[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]]
} }
# `hosts show <ip>` ######################################################### # `hosts show <no matching>` ##################################################
@test "\`show <query>\` with no matching entries 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 example.com
}
run "${_HOSTS}" show bad-query
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
[[ "${lines[0]}" =~ No\ matching\ entries ]]
}
# `hosts show <ip>` ###########################################################
@test "\`show <ip>\` exits with status 0 and shows all matches." { @test "\`show <ip>\` exits with status 0 and shows all matches." {
{ {
@ -54,12 +72,12 @@ load test_helper
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
[[ "${lines[0]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]] [[ "${lines[0]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
[[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]] [[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
} }
# `hosts show <search string>` ################################################ # `hosts show <search string>` ################################################
@test "\`show <search string>\` exits with status 0 and shows matching records." { @test "\`show <search string>\` exits with status 0 and shows 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
@ -71,12 +89,12 @@ load test_helper
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 0 ]] [[ ${status} -eq 0 ]]
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]] [[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example.com ]] [[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example.com ]]
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`show <search string>\` prints records with matching comments." { @test "\`show <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"
@ -91,7 +109,7 @@ load test_helper
[[ "${lines[2]}" == "" ]] [[ "${lines[2]}" == "" ]]
} }
@test "\`show <search string>\` prints disabled records with matching comments." { @test "\`show <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"
@ -103,8 +121,8 @@ load test_helper
run "${_HOSTS}" show "Comment" run "${_HOSTS}" show "Comment"
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]] [[ "${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[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]]
[[ "${lines[4]}" == "" ]] [[ "${lines[4]}" == "" ]]
} }

View File

@ -18,6 +18,9 @@ setup() {
export _HOSTS_TEMP_PATH export _HOSTS_TEMP_PATH
_HOSTS_TEMP_PATH="${_HOSTS_TEMP_DIR}/hosts" _HOSTS_TEMP_PATH="${_HOSTS_TEMP_DIR}/hosts"
export _ERROR_PREFIX
_ERROR_PREFIX="$(tput setaf 1)!$(tput sgr0) "
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}" cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}"
export HOSTS_PATH="${_HOSTS_TEMP_PATH}" export HOSTS_PATH="${_HOSTS_TEMP_PATH}"

View File

@ -2,7 +2,7 @@
load test_helper load test_helper
# `hosts unblock` ################################################################# # `hosts unblock` #############################################################
@test "\`unblock\` with no arguments exits with status 1." { @test "\`unblock\` with no arguments exits with status 1." {
run "${_HOSTS}" unblock run "${_HOSTS}" unblock
@ -28,7 +28,7 @@ load test_helper
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]] [[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
} }
# `hosts unblock <invalid>` ############################################ # `hosts unblock <invalid>` ###################################################
@test "\`unblock <invalid> \` exits with status 1." { @test "\`unblock <invalid> \` exits with status 1." {
{ {
@ -49,10 +49,10 @@ load test_helper
run "${_HOSTS}" unblock example.net run "${_HOSTS}" unblock example.net
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${output} == "No matching records found." ]] [[ ${output} == "${_ERROR_PREFIX}No matching entries found." ]]
} }
# `hosts unblock <hostname>` ########################################### # `hosts unblock <hostname>` ##################################################
@test "\`unblock <hostname>\` exits with status 0." { @test "\`unblock <hostname>\` exits with status 0." {
{ {