Commit Graph

238 Commits

Author SHA1 Message Date
William Melody e9a7ecd6a7 Update version to 3.2.2 2020-03-16 22:36:18 -07:00
William Melody 0f3c59bd86 Rename `SED_COMMAND` variable to `_SED_I_COMMAND`. 2020-03-16 20:46:34 -07:00
William Melody d91a4406a7 Update version to 3.2.1 2020-03-16 15:07:52 -07:00
William Melody 774505abc0 Update version to 3.2.0 2020-03-15 18:55:34 -07:00
William Melody 659bbb4755 Use consistent argument name in documentation. 2020-03-15 18:51:42 -07:00
William Melody 0247826a41 Indicate in help text that `--skip-backup` is optional. 2020-03-15 18:49:03 -07:00
William Melody 4eed5251da Add `backups` command.
`backups` provides subcommands for managing backups of the hosts file.
2020-03-15 18:42:16 -07:00
William Melody ada8cb6019 Handle multiple hostnames with `block` and `unblock`. 2020-03-15 17:10:29 -07:00
William Melody ab2a193371 Update version to 3.1.3 2020-03-15 17:08:11 -07:00
William Melody 4be5105f67 Modify `sed` platform handling.
Use array for `sed -i` command so arguments are passed as expected. This
resolves an issue where a copy of the hosts file is created with quotes
appended to the filename.
2020-03-15 17:03:53 -07:00
William Melody bfbe08a65b Update version to 3.1.2 2020-03-15 11:45:39 -07:00
William Melody c680c02007 Update grammar in comment. 2020-03-15 11:36:47 -07:00
William Melody fa9ba543f5 Set `noglob` to disable filename expansion (globbing). 2020-03-15 11:30:08 -07:00
William Melody 850f210a4f Assign command parameters as quoted array.
Satisfies Shellcheck SC2206
https://github.com/koalaman/shellcheck/wiki/SC2206
2020-03-14 19:03:50 -07:00
William Melody fe48de86f1 Update version to 3.1.1 2019-11-21 19:28:51 -08:00
William Melody e10d116510 Handle platform-specific `sed -i` requirements.
`sed -i` on macOS requires an extension, but on Linux that extension can
cause errors. Use a platform check to only include the extension on
macOS.

resolves gh-5
2019-11-21 19:17:45 -08:00
William Melody 060306f28b Use { ..; } instead of (..) to avoid subshell overhead.
ShellCheck SC2235: https://github.com/koalaman/shellcheck/wiki/SC2235
2019-11-04 17:01:16 -08:00
William Melody 1ed6504bc2 Update version to 3.1.0 2018-08-14 11:31:02 -07:00
William Melody 789a0797c2 Improve search behavior.
Search comments in addition to IPs and hostnames.

Call grep twice, excluding commented lines first before searching.
2018-08-14 11:31:02 -07:00
William Melody af412412f2 Bump version to 3.0.1 2018-05-15 10:11:32 -07:00
William Melody 5ee732006d Add comments about option parsing. 2018-05-15 08:37:24 -07:00
William Melody 647c4329c9 Update version to 3.0.0 2018-05-14 21:27:39 -07:00
William Melody 1738ba9ad3 Use double square brackets for option parsing `while` test. 2018-05-14 21:22:26 -07:00
William Melody 0c11a1939e Simplify `help` argument handling. 2018-05-14 21:19:36 -07:00
William Melody dfcb37b3b6 Remove unused option normalization.
This program only really accepts one short argument, `-h`, so this
extra complexity isn't necessary.
2018-05-14 20:45:35 -07:00
William Melody 79a5aa95fc Update boilerplate to latest from bash-boilerplate.
Source: https://github.com/alphabetum/bash-boilerplate
2018-05-14 20:22:35 -07:00
William Melody 7f3f6a95ab Simplify regular expression variable handling.
- Remove '_target' prefixes.
- Declare and assign separately.
2018-05-14 20:12:19 -07:00
William Melody 6da4b7475f Update coding style.
- Quote variables throughout.
- Use underscore prefixes to denote script-local variables and avoid
  identifier conflicts.
- Add spacing for better statement grouping.
2018-05-14 20:03:32 -07:00
William Melody 661fa3b114 Use updated `_join()` implementation.
Source: https://github.com/alphabetum/bash-boilerplate
2018-05-14 18:46:52 -07:00
William Melody dd25af8c99 Use updated `_load_commands()` implementation.
Source: https://github.com/alphabetum/bash-boilerplate
2018-05-14 18:42:21 -07:00
William Melody 2a5a18cf85 Bump version to 2.4.3
Fix misalignment between version numbers in package.json and
`$_VERSION`.
2018-05-09 10:36:18 -07:00
William Melody 746780fbbe Use explicit escaping for backslashes in all contexts.
ShellCheck SC1117
https://github.com/koalaman/shellcheck/wiki/SC1117
2018-04-15 15:01:34 -07:00
William Melody 80edd464b6 Use explicit escaping for "\\n" newlines.
Backslash is literal, so explicitly escape it rather than rely on
fallback behavior.

ShellCheck SC1117
https://github.com/koalaman/shellcheck/wiki/SC1117
2018-04-15 14:55:26 -07:00
William Melody d02f46a99e Bump version to 2.4.1. 2017-03-01 20:55:06 -08:00
William Melody d700037f18 Simplify header comment. 2017-03-01 20:54:08 -08:00
William Melody 32c5be4dcc Revert "Prefix `_debug` strings with '•' to avoid highlighting bugs."
This reverts commit 921afd7155.

The reverted commit did not succeed in fixing highlighting bugs.
2017-03-01 20:51:36 -08:00
William Melody 921afd7155 Prefix `_debug` strings with '•' to avoid highlighting bugs.
GitHub's syntax highlighting doesn't work well when strings contain
function names with parentheses, so add character to the beginning of
the string to see whether this makes it better.
2017-03-01 20:46:49 -08:00
William Melody f5d0f827e0 Bump version to 2.4.0. 2017-03-01 20:24:12 -08:00
William Melody d94af2c25b Update comments in "Globals" section. 2017-03-01 20:22:41 -08:00
William Melody db7ac727f1 Rename `$DEFAULT_COMMAND` to `$HOSTS_DEFAULT_COMMAND`. 2017-03-01 20:21:06 -08:00
William Melody 6c0254895f Support full `list` operations in `hosts` with no subcommand. 2017-03-01 20:18:26 -08:00
William Melody 05ba7bdc03 Quote argument to `unset`.
ShellCheck SC2184: Quote arguments to unset so they're not glob expanded.

https://github.com/koalaman/shellcheck/wiki/SC2184
2017-03-01 19:59:01 -08:00
William Melody 404ee19742 Add `_verify_write_permissions()` checks to `block` and `unblock`. 2017-03-01 19:32:49 -08:00
William Melody 3237818607 Expand documentation comment for `_verify_write_permissions()`. 2017-03-01 18:28:21 -08:00
William Melody b7b186267d Bump version to 2.3.1. 2017-03-01 16:29:36 -08:00
William Melody e0790f6a4c Bump version to 2.3.0. 2017-03-01 16:25:03 -08:00
William Melody 99b175f134 Add `--auto-sudo` option and expand option documentation.
Using `sudo` within a script is generally considered poor practice, so
by default an error message is printed when the user attempts to perform
a write operation without sufficient permissions.

One way to deal avoid this error message is to alias `hosts` to `sudo
hosts`, but this then requires `sudo` for all operations and not just
write operations.

The new `--auto-sudo` option flag provides a way to automatically invoke
a write command with `sudo` when the user doesn't have write
permissions.

In order to provide cleaner documentation for this option, include more
option documentation in README.md.
2017-03-01 16:18:05 -08:00
William Melody 0925735153 Bump version to 2.2.0. 2017-03-01 14:29:04 -08:00
William Melody 347189c059 Add `search` usage to primary `help` output.
Update README.md to more accurately reflect `help` output.
2017-03-01 13:45:19 -08:00
William Melody 265a1c732e Add `block` and `unblock` functions.
`block` and `unblock` set the given <hostname> to the IPv4 and IPv6
loopback addresses.
2017-03-01 13:43:10 -08:00
William Melody 73ffcffb38 Add `hosts search` function.
`hosts search` wraps `hosts list`, providing a slightly more intuitive
interface.
2017-03-01 12:46:27 -08:00
William Melody 703d665a32 Use `read` with `-r` option.
ShellCheck SC2162: read without -r mangle backslashes

https://github.com/koalaman/shellcheck/wiki/SC2162
2017-03-01 12:05:59 -08:00
William Melody 4194b25459 Use `HEREDOC` rather than `EOM` to define heredocs. 2017-03-01 11:42:26 -08:00
William Melody f7675f360e Bump version to 2.1.6. 2016-03-24 17:32:26 -07:00
William Melody 149a51662e Handle blank arguments string when assigning to `$_RAW_OPTIONS`.
When `$*` is specified with quotes and braces as `"${*}"`, a blank
argument array results in an error in older versions of bash. Remove
the braces to avoid this error.
2016-03-24 17:30:30 -07:00
William Melody 187222614a Use braces in all variable references.
Braces are only required in certain cases, but the cognitive overhead in
keeping track of which cases require braces can be reduced by simply
always using them.

Example: `${NAME}`

Retain more widely-used braces `$NAME` convention in documentation.
2016-02-23 18:14:21 -08:00
William Melody 9782f78f22 Bump version to 2.1.5. 2016-01-26 20:42:26 -08:00
William Melody f7c7aef173 Update help/usage formatting. 2016-01-26 20:25:16 -08:00
William Melody eeab6e8ba1 Alphabetize subcommands in Usage.
It's easier to find a subcommand in an alphabetical list.
2016-01-15 16:52:41 -08:00
William Melody 80a8e48c10 Use `if..then` in `show` to avoid non-zero exits.
The `[[ ... ]] && ...` conditional style results in non-zero exits when
the test is false. Moving this to a traditional `if..then` style avoids
this behavior while also being more explicit about the objective of the
code. `|| return 0` or `|| exit 0` could have been added as an
alternative way to avoid the non-zero exit behavior, but is not used in
this case because the traditional `if..then` style is more common.
2015-11-29 18:09:35 -08:00
William Melody 4d1adbdf52 Bump version to 2.1.4. 2015-11-25 18:03:19 -08:00
William Melody 21069fb68d Add quotes around strings in `list` command argument tests.
Quoting these strings more clearly communicates that these are not
variable or function names, and that the string itself is what is
being tested for.
2015-11-23 11:16:03 -08:00
William Melody 59e404f865 Add newlines around prompt section of `remove`.
The convention used in this project is to include newlines around
longer, grouped conditionals in order to provide better visual
separation.
2015-11-23 11:07:45 -08:00
William Melody 279573d2d0 Update regular expression comments in `remove`.
The regular expressions to which these comments apply were moved
around a few times without the comments being updated for these
changes.
2015-11-23 11:01:43 -08:00
William Melody e8e161f1e8 Bump version to 2.1.3. 2015-11-23 10:45:23 -08:00
William Melody 09d6d83093 Fix `--force` behavior in `remove`.
The `--force` option is passed to the function in `$_COMMAND_ARGV`,
which means that assigning arguments from positions in this array leads
to unexpected behavior depending on where in the argument list `--force`
is included. As a result, the `remove` function must parse the
arguments, removing the `--force` argument before assigning function
arguments to local variables.

resolves #2
2015-11-23 10:38:45 -08:00
William Melody a869da8e4e Clarify slicing example in `$_COMMAND_PARAMETERS` comment.
The example assignment should have balanced quotes to avoid any copy /
paste errors or reader confusion. Remove the 'not' since it's clearer if
this is used as a parenthetical example of slicing.
2015-10-19 16:50:48 -07:00
William Melody 0b955f41fa Bump version to 2.1.2. 2015-10-19 16:30:32 -07:00
William Melody e9b78ee15f Include debug statements in `add()` to print positional arguments. 2015-10-19 16:29:06 -07:00
William Melody b539cd6136 Use `unset` rather than slicing for bash 3.2 compatibility.
Use `unset` to remove the first element of `$_COMMAND_PARAMETERS` rather
than slicing because under bash 3.2 the resulting slice is treated as a
quoted string and doesn't easily get coaxed back into an array.
2015-10-19 16:26:21 -07:00
William Melody 6733260385 Bump version to 2.1.1 2015-09-15 11:33:39 -07:00
William Melody ab8934ae80 Default to 'N' in `remove` confirmation.
Rather than require a response, use 'N' as the default since this is a
destructive action.
2015-09-15 11:30:30 -07:00
William Melody 871e8fc131 Bump version to 2.1.0 2015-09-12 16:21:04 -07:00
William Melody db4df46caa Rename `search_term` to `search_string` in `enable` and `disable`.
`search_string` is slightly more descriptive, and it's the name that's
used in `remove`.
2015-09-12 16:18:53 -07:00
William Melody 1242d0b4bc Replace 'domain' with 'hostname' in regular expression variable names.
'hostname' is more accurate and consistent for this context.
2015-09-12 16:14:36 -07:00
William Melody 95fd43668a Assign `remove` regular expressions to variables.
Since the regular expressions are essentially repeated, with the only
difference being the capturing groups that have no impact on the delete
operations, assign them to a set of descriptive variables. This makes
things more organized and additionally provides some explanation for
what each regular expression is matching against.
2015-09-12 16:12:56 -07:00
William Melody ab9a08d954 Enhance `remove` to accept an IP and hostname pair.
This provides a mechanism for removing exact IP and hostname pairs.
The existing search string functionality should continue to function as
it did previously.
2015-09-12 15:41:28 -07:00
William Melody 1d1fa7ce9f Match IP at start of line in `disable`
The `disable` regular expressions avoid matching against commented lines
by excluding '#' characters at the beginning of the line. However, the
IP portion of the record is at the beginning of non-commented lines, so
in order to match these IPs the `target_regex_ip` needs to have the
search term checked against the beginning of the line. Therefore, the
`[^#]` at the beginning of `target_regex_ip` must be removed to make
`search_term` properly match these IPs.
2015-09-12 15:03:09 -07:00
William Melody b8811d42ba Bump version to 2.0.0 2015-09-12 14:16:43 -07:00
William Melody 84d77b8aec Export space and tab regular expression variables.
These variables are used within regular expressions, but as the program
evolves they are not always in constant use. Exporting these variables
silences ShellCheck warnings and makes them available to subshells.
2015-09-12 14:13:00 -07:00
William Melody a33b38b204 Make `sed` ops in `enable`/`disable` only match exact columns.
This is an application of the approach to `sed` calls that was included
in 94ca8bf

In order to make editing more precise, `sed` regular expressions only
match exact occurrences of the search string within each entry column.
2015-09-12 14:08:48 -07:00
William Melody 9cb7576bb5 Merge branch 'master' into exact-matches
* master:
  Exit with "Not found" message on failed search in `enable`/`disable`
  Bump version to 1.5.0
  Print confirmation feedback in `add` on success.
  Declare and assign separately to avoid masking return values.
  Add `brew tap` command to `brew install` in Readme.
2015-09-12 13:28:31 -07:00
William Melody 8465c2bdf8 Exit with "Not found" message on failed search in `enable`/`disable`
When the search string in `enable` or `disable` is not found, exit the
program and provide feedback rather than silently failing.
2015-09-12 13:19:17 -07:00
William Melody 62f200574d Bump version to 1.5.0 2015-09-11 19:51:02 -07:00
William Melody 590d75096f Print confirmation feedback in `add` on success. 2015-09-11 19:47:52 -07:00
William Melody 12d11c2dc3 Declare and assign separately to avoid masking return values.
ShellCheck SC2155
2015-09-11 19:43:56 -07:00
William Melody 94ca8bfb57 Make `sed` operations in `remove` only match exact column matches.
In order to make editing more precise, sed regular expressions only
match exact occurrences of the search string within each entry column.

In order to properly handle tab and space separators in a portable
manner, a set of global variables are included that provide strings
of those characters.
2015-06-28 22:09:46 -07:00
William Melody 46dcff947c Bump version to 1.4.3 2015-06-28 22:08:29 -07:00
William Melody 41a84bd0ed Uppercase `$_USE_DEBUG` reference in `_debug`
This variable was uppercased in a09d0fc but this occurrence was missed.
2015-06-28 17:32:42 -07:00
William Melody c80287f2bf Bump version to 1.4.2 2015-06-27 20:49:06 -07:00
William Melody fa920b0a97 Use `local` keyword more consistently. 2015-06-27 20:47:36 -07:00
William Melody a09d0fc893 Update boilerplate to reflect latest updates from source. 2015-06-27 20:43:19 -07:00
William Melody 8b0fc0fcfe Quote command names passed as arguments to `desc` 2015-06-27 20:33:11 -07:00
William Melody d2b92aec5a Prefer newlines instead of semicolons. 2015-06-27 20:31:26 -07:00
William Melody 66edbfd03c Update `_debug` and `_die` comments. 2015-06-27 20:21:58 -07:00
William Melody 3cfa2c8cf9 Use simpler `$IFS` setting. 2015-06-27 20:17:41 -07:00
William Melody 2693c62693 Bump version to 1.4.1 2015-06-25 17:19:23 -07:00
William Melody 02a87b0938 Add optional extension argument to `sed -i` calls
BSD sed requires the optional exention argument for the -i option, while
in GNU sed it's optional. Including a blank extension for portability.
2015-06-25 17:17:12 -07:00
William Melody d5b6456766 Bump version to 1.4.0 2015-05-20 16:49:46 -07:00
William Melody 08d899fee5 Fix inert indentation typo. 2015-04-28 16:54:25 -07:00
William Melody 9a395fd017 Bump version to 1.3.0 2015-04-18 21:23:45 -07:00
William Melody 26ad499fee Spelling correction: formmatted_comment -> formatted_comment 2015-04-18 21:21:46 -07:00
William Melody 2c83de9dff Declare and assign separately to avoid masking return values.
ShellCheck SC2155

https://github.com/koalaman/shellcheck/wiki/SC2155
2015-04-18 21:17:46 -07:00
William Melody cb9e20e554 Use capitalized form of $_ME variable.
The capitalized version of this variable makes it easier to override,
even though this is not technically part of the external API. There is
no known downside of using the capitalized version of the variable.
2015-04-18 21:06:26 -07:00
William Melody e62bede843 Bump version to 1.2.0 2015-03-20 18:11:42 -07:00
William Melody c2ca24b459 Verify that matching records have been found in `remove` before removing 2015-03-20 18:10:51 -07:00
William Melody 312f5909fa Add ability to add an option comment when adding a record
This addition requires a number of changes including:

- the addition of a new `_join` function
- reformatting of the `add` function to facilitate the longer lines
2015-03-20 18:06:00 -07:00
William Melody 023aa0503f Add `enabled` and `disabled` to command list in `--help` 2015-03-20 17:23:31 -07:00
William Melody f10be24da7 Bump version to 1.1.0 2015-03-20 16:49:24 -07:00
William Melody 7e32a799c2 Add `enabled` and `disabled` commands.
Both of these are just aliases for the `list ( enabled | disabled )`
commands, providing a shorter way to access the same functionality.
2015-03-20 16:49:08 -07:00
William Melody 52529dd029 Correct typo in `help` usage + add info about `--force` in Readme 2015-03-19 17:14:04 -07:00
William Melody 0830fa43b8 Simplify "strict mode" section in order to avoid too much noise
These comments can be found in the bash-boilerplate project and aren't
necessary here. Since the $DEFAULT_IFS doesn't appear to be used,
either, the $IFS setting can be reduced to a since simple assignment.
2015-03-19 14:30:51 -07:00
William Melody bcf308ffd3 Bump version to 1.0.0 2015-03-18 18:45:00 -07:00
William Melody db5cf5a97b Use more consistent spacing in usage statements. 2015-03-18 18:41:04 -07:00
William Melody b5a2b28884 Remove redundant command usage entry in `help` 2015-03-18 18:28:38 -07:00
William Melody 5844aa19a3 Use 'search string' consistently instead of mixing with 'search term' 2015-03-18 18:22:37 -07:00
William Melody 4d8c9a3385 Prompt user in `remove`
In order to avoid mistakes, prompt the user when removing records. Allow
skipping of the prompt using the --force option.
2015-03-18 18:20:24 -07:00
William Melody 00e94c8042 Verify write permissions before running write commands
When the user doesn't have write permissions, display a helpful error
message.
2015-03-18 18:02:09 -07:00
William Melody a330d17862 Add `$HOSTS_PATH` variable
This variable makes it possible to configure the hosts path to something
other than /etc/hosts.
2015-03-18 17:47:20 -07:00
William Melody bc36182773 Print disabled matches in `show` 2015-03-18 17:41:04 -07:00
William Melody 522d189f2e Update `help` with latest usage information. 2015-03-18 17:25:34 -07:00
William Melody 10704e2bd7 Use $DEFAULT_COMMAND pattern from bash-boilerplate
This is more verbose, but consistent with bash-boilerplate's approach.
2015-03-18 17:23:15 -07:00
William Melody 8889e26255 Run the `list` command by default when no command is provided 2015-03-18 17:13:25 -07:00
William Melody e9dec32c90 Display disabled records in `list` when present
When disabled records are present, include them in a new section in the
default output of `list`. If no disabled records are present, don't
include the section.
2015-03-18 17:10:21 -07:00
William Melody 7d8a94d4ca Add 'enabled' and 'disabled' options to `list`
These return records that have the status specified in the option.
2015-03-18 17:05:33 -07:00
William Melody 9de0edda07 Add `disable` and `enable` commands
These commands make it possible to disable records without fully
removing them. To 'disable' a record, it simply is commented out with
the following pattern `#disabled: ` prepended to the line.
2015-03-18 16:53:03 -07:00
William Melody 124520efc4 Add `show` command and use that for `list` searches
Displaying a record pair for a hostname or IP address is likely a common
operation, and using the `show` name makes it clear what the primary
function of the command is. Since record pairs are very simple, the
easiest way to allow both hostname and ip address arguments is to use
grep, which also provides general search-like functionality. In order to
avoid doubling this functionality, use the `show` command in the `list`
command for search.
2015-03-18 16:07:49 -07:00
William Melody cdf7793d7e Make `list` search terms match anywhere on the line
Making list more flexible enables searching for hostnames as well as
partial ip addresses.
2015-03-18 16:01:32 -07:00
William Melody a8461f48fd Use alpha ordering for function definitions 2015-03-18 15:45:23 -07:00
William Melody 235be1f151 Expand per-command usage info.
Including descriptions of each command, since the functionality might
not always be immediately obvious.
2015-03-16 19:53:50 -07:00
William Melody bafa955308 Add `file` command
This command simply runs `cat` on /etc/hosts
2015-03-16 19:48:16 -07:00
William Melody 2853d6fd33 Add per-command usage info to program help 2015-03-16 19:38:10 -07:00
William Melody e867492064 In `remove`, delete the link in place rather than blanking first
When using the pattern of blanking the lines first and then deleting
the blank lines, the program ends up deleting potentially intentional
blank lines. In order to avoid this, use a simple delete rather than the
blanking + blank line deleting pattern.
2015-03-16 17:51:28 -07:00
William Melody 7607dfd262 Add `edit` command
`edit` open the hosts file in the editor specified in the $EDITOR
environment variable.
2015-03-16 17:39:50 -07:00
William Melody fc60f3a0d7 Clarify usage information 2015-03-16 17:26:58 -07:00
William Melody f714965f0f Add acknowledgements sections for script and Readme 2015-03-16 17:19:17 -07:00
William Melody 42465bd461 Rewrite as command-based program using bash boilerplate
This program contains the same functionality as previous iterations,
with several changes:

- move to command-based structure using the bash boilerplate template,
- inlcude basic error handling,
- revise variable naming and options for clarity,
- prefer `printf` over `echo`,
- misc other revisions.
2015-03-16 16:58:35 -07:00
William Melody b88046cd4b Rename script from `host-manager` to `hosts` 2015-03-12 20:35:24 -07:00