1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-22 12:55:11 +00:00

Verify write permissions before running write commands

When the user doesn't have write permissions, display a helpful error
message.
This commit is contained in:
William Melody 2015-03-18 18:02:09 -07:00
parent a330d17862
commit 00e94c8042

17
hosts
View File

@ -472,6 +472,18 @@ _command_argv_includes() {
_contains "$1" "${command_argv[*]}" _contains "$1" "${command_argv[*]}"
} }
# _verify_write_permissions
#
# Print a helpful error message when the specified operation can't be
# performed due to the lack of write permissions.
_verify_write_permissions() {
if ! test -w "${HOSTS_PATH}"; then
_die printf \
"You don't have permission to perform this operation. Try again with:
sudo !!\n"
fi
}
############################################################################### ###############################################################################
# desc # desc
############################################################################### ###############################################################################
@ -659,6 +671,7 @@ Description:
Add a given IP address and hostname pair. Add a given IP address and hostname pair.
EOM EOM
add() { add() {
_verify_write_permissions
local ip=${1:-} local ip=${1:-}
local hostname=${2:-} local hostname=${2:-}
if [[ -z ${ip} ]]; then if [[ -z ${ip} ]]; then
@ -688,6 +701,7 @@ Description:
search term. search term.
EOM EOM
disable() { disable() {
_verify_write_permissions
local search_term=$1 local search_term=$1
if [[ -z "${search_term}" ]]; then if [[ -z "${search_term}" ]]; then
$_me help disable $_me help disable
@ -711,6 +725,7 @@ Description:
Open the ${HOSTS_PATH} file in your \$EDITOR. Open the ${HOSTS_PATH} file in your \$EDITOR.
EOM EOM
edit() { edit() {
_verify_write_permissions
if [[ -z "$EDITOR" ]]; then if [[ -z "$EDITOR" ]]; then
_die printf "\$EDITOR not set.\n" _die printf "\$EDITOR not set.\n"
else else
@ -729,6 +744,7 @@ Description:
or search term. or search term.
EOM EOM
enable() { enable() {
_verify_write_permissions
local search_term=$1 local search_term=$1
if [[ -z "${search_term}" ]]; then if [[ -z "${search_term}" ]]; then
$_me help enable $_me help enable
@ -799,6 +815,7 @@ Description:
Remove all IP / hostname pairs for a given hostname. Remove all IP / hostname pairs for a given hostname.
EOM EOM
remove() { remove() {
_verify_write_permissions
local hostname=${1:-} local hostname=${1:-}
if [[ -z $hostname ]]; then if [[ -z $hostname ]]; then
$_me help remove $_me help remove