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.
This commit is contained in:
William Melody 2017-03-01 16:00:08 -08:00
parent 0925735153
commit 99b175f134
2 changed files with 54 additions and 8 deletions

View File

@ -54,6 +54,17 @@ Usage:
hosts show (<ip> | <hostname> | <search string>)
hosts remove (<ip> | <hostname> | <search string>) [--force]
hosts unblock <hostname>
hosts --auto-sudo
hosts -h | --help
hosts --version
Options:
--auto-sudo Run write commands with `sudo` automatically.
-h --help Display this help information.
--version Display version information.
Help:
hosts help [<command>]
```
For full usage, run:
@ -244,6 +255,29 @@ Description:
Display the current program version.
```
## Options
### `--auto-sudo`
When specified, all write operations that require `sudo` will automatically
rerun the command using `sudo` when the current user does not have write the
permissions to write to the hosts file.
To have this option always enabled, add the following line to your shell
configuration (`.bashrc`, `.zshrc`, or similar):
```bash
alias hosts="hosts --auto-sudo"
```
### `-h` `--help`
Display help information.
### `--version`
Display version information.
## Tests
To run the test suite, install [Bats](https://github.com/sstephenson/bats) and

28
hosts
View File

@ -224,6 +224,7 @@ _COMMAND_ARGV=("${0}")
# on what the program needs.
_CMD=""
_USE_DEBUG=0
_AUTO_SUDO=0
while [ ${#} -gt 0 ]
do
@ -239,6 +240,9 @@ do
--debug)
_USE_DEBUG=1
;;
--auto-sudo|--sudo)
_AUTO_SUDO=1
;;
*)
# The first non-option argument is assumed to be the command name.
# All subsequent arguments are added to $_COMMAND_ARGV.
@ -483,9 +487,15 @@ _present() {
_verify_write_permissions() {
if ! test -w "${HOSTS_PATH}"
then
_die printf \
if ((_AUTO_SUDO))
then
sudo "${_ME}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}"
exit $?
else
_die printf \
"You don't have permission to perform this operation. Try again with:
sudo !!\n"
fi
fi
}
@ -608,12 +618,14 @@ Usage:
${_ME} show (<ip> | <hostname> | <search string>)
${_ME} remove (<ip> | <hostname> | <search string>) [--force]
${_ME} unblock <hostname>
${_ME} --auto-sudo
${_ME} -h | --help
${_ME} --version
Options:
-h --help Display this help information.
--version Display version information.
--auto-sudo Run write commands with \`sudo\` automatically.
-h --help Display this help information.
--version Display version information.
Help:
${_ME} help [<command>]
@ -688,7 +700,7 @@ add() {
_debug printf "add() \${2}: %s\n" "${2:-}"
_debug printf "add() \${3}: %s\n" "${3:-}"
_verify_write_permissions
_verify_write_permissions "$@"
local ip=${1:-}
local hostname=${2:-}
local comment=${*:3}
@ -766,7 +778,7 @@ Description:
search string.
HEREDOC
disable() {
_verify_write_permissions
_verify_write_permissions "$@"
local search_string="${1}"
if [[ -z "${search_string}" ]]
then
@ -832,7 +844,7 @@ Description:
Open the ${HOSTS_PATH} file in your \$EDITOR.
HEREDOC
edit() {
_verify_write_permissions
_verify_write_permissions "$@"
if [[ -z "${EDITOR}" ]]
then
_die printf "\$EDITOR not set.\n"
@ -852,7 +864,7 @@ Description:
or search string.
HEREDOC
enable() {
_verify_write_permissions
_verify_write_permissions "$@"
local search_string="${1}"
if [[ -z "${search_string}" ]]
then
@ -977,7 +989,7 @@ Description:
IP and hostname pair will be removed.
HEREDOC
remove() {
_verify_write_permissions
_verify_write_permissions "$@"
local is_search_pair=0
local force_skip_prompt=0
local arguments=()