1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-29 16:24:06 +00:00

Prompt user in remove

In order to avoid mistakes, prompt the user when removing records. Allow
skipping of the prompt using the --force option.
This commit is contained in:
William Melody 2015-03-18 18:19:52 -07:00
parent 00e94c8042
commit 4d8c9a3385
2 changed files with 31 additions and 6 deletions

View File

@ -5,8 +5,9 @@ A command line program with shortcuts for managing hosts file entries.
## Usage ## Usage
Usage: Usage:
hosts
hosts add <ip> <hostname> hosts add <ip> <hostname>
hosts remove <hostname> hosts remove ( <ip> | <hostname> | <search term> ) [--force]
hosts list [enabled | disabled | <search string>] hosts list [enabled | disabled | <search string>]
hosts show ( <ip> | <hostname> | <search string> ) hosts show ( <ip> | <hostname> | <search string> )
hosts disable ( <ip> | <hostname> | <search term> ) hosts disable ( <ip> | <hostname> | <search term> )

34
hosts
View File

@ -809,19 +809,43 @@ list() {
desc remove <<EOM desc remove <<EOM
Usage: Usage:
$_me remove <hostname> $_me remove ( <ip> | <hostname> | <search term> ) [--force]
Options:
--force Skip the confirmation prompt.
Description: Description:
Remove all IP / hostname pairs for a given hostname. Remove one or more disabled records based on a given IP address, hostname,
or search term.
EOM EOM
remove() { remove() {
_verify_write_permissions _verify_write_permissions
local hostname=${1:-} local search_string=${1:-}
if [[ -z $hostname ]]; then if [[ -z $search_string ]]; then
$_me help remove $_me help remove
exit 1 exit 1
else else
sed -i "/^[^#].*[^A-Za-z0-9\.]${hostname}$/d" "${HOSTS_PATH}" local target_records=$(sed -n "s/^\(.*${search_string}.*\)$/\1/p" "${HOSTS_PATH}")
if ! _command_argv_includes "--force"; then
printf "Removing the following records:\n%s\n" "$target_records"
while true; do
read -p "Are you sure you want to proceed? [y/n] " yn
case $yn in
[Yy]* )
break
;;
[Nn]* )
printf "Exiting...\n"
exit 0
;;
* )
printf "Please answer yes or no.\n"
;;
esac
done
fi
sed -i "/^.*${search_string}.*$/d" "${HOSTS_PATH}"
printf "Removed:\n%s\n" "${target_records}"
fi fi
} }