mirror of
https://github.com/octoleo/hosts.git
synced 2024-11-22 12:55:11 +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:
parent
00e94c8042
commit
4d8c9a3385
@ -5,8 +5,9 @@ A command line program with shortcuts for managing hosts file entries.
|
||||
## Usage
|
||||
|
||||
Usage:
|
||||
hosts
|
||||
hosts add <ip> <hostname>
|
||||
hosts remove <hostname>
|
||||
hosts remove ( <ip> | <hostname> | <search term> ) [--force]
|
||||
hosts list [enabled | disabled | <search string>]
|
||||
hosts show ( <ip> | <hostname> | <search string> )
|
||||
hosts disable ( <ip> | <hostname> | <search term> )
|
||||
|
34
hosts
34
hosts
@ -809,19 +809,43 @@ list() {
|
||||
|
||||
desc remove <<EOM
|
||||
Usage:
|
||||
$_me remove <hostname>
|
||||
$_me remove ( <ip> | <hostname> | <search term> ) [--force]
|
||||
|
||||
Options:
|
||||
--force Skip the confirmation prompt.
|
||||
|
||||
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
|
||||
remove() {
|
||||
_verify_write_permissions
|
||||
local hostname=${1:-}
|
||||
if [[ -z $hostname ]]; then
|
||||
local search_string=${1:-}
|
||||
if [[ -z $search_string ]]; then
|
||||
$_me help remove
|
||||
exit 1
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user