added final plugin and completion
parent
3897e3ac6b
commit
b97818b976
|
|
@ -1,15 +1,13 @@
|
||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
|
|
||||||
if command -v hosts > /dev/null; then
|
__hosts_completions() {
|
||||||
__hosts_completions() {
|
local OPTS=("add list remove help")
|
||||||
local OPTS=("add list remove")
|
COMPREPLY=()
|
||||||
COMPREPLY=()
|
for _opt_ in ${OPTS[@]}; do
|
||||||
for _opt_ in ${OPTS[@]}; do
|
if [[ "$_opt_" == "$2"* ]]; then
|
||||||
if [[ "$_opt_" == "$2"* ]]; then
|
COMPREPLY+=("$_opt_")
|
||||||
COMPREPLY+=("$_opt_")
|
fi
|
||||||
fi
|
done
|
||||||
done
|
}
|
||||||
}
|
|
||||||
|
complete -F __hosts_completions hosts
|
||||||
complete -F __hosts_completions
|
|
||||||
fi
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
cite about-plugin
|
cite about-plugin
|
||||||
about-plugin '/etc/hosts based operations'
|
about-plugin '/etc/hosts based operations'
|
||||||
|
|
||||||
FILE="/etc/hosts"
|
___HOST_FILE="/etc/hosts"
|
||||||
|
|
||||||
function hosts_add {
|
function ___hosts_add {
|
||||||
local ENTRY=`grep "$2" $FILE`
|
local ENTRY=`grep "$2" $_HOST_FILE`
|
||||||
if [[ $ENTRY != "" ]]
|
if [[ $ENTRY != "" ]]
|
||||||
then
|
then
|
||||||
echo "Host "$2" already added"
|
echo "Host "$2" already added"
|
||||||
|
|
@ -18,15 +18,15 @@ function hosts_add {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function hosts_list {
|
function ___hosts_list {
|
||||||
cat $FILE
|
cat $___HOST_FILE
|
||||||
}
|
}
|
||||||
|
|
||||||
function hosts_remove {
|
function ___hosts_remove {
|
||||||
local ENTRY=`grep "$1" $FILE`
|
local ENTRY=`grep "$1" $_HOST_FILE`
|
||||||
if [[ $ENTRY != "" ]]
|
if [[ $ENTRY != "" ]]
|
||||||
then
|
then
|
||||||
DELETED=`grep -w -v $1 $FILE`
|
DELETED=`grep -w -v $1 $_HOST_FILE`
|
||||||
echo "$DELETED" | sudo tee /etc/hosts > /dev/null
|
echo "$DELETED" | sudo tee /etc/hosts > /dev/null
|
||||||
echo "Removed \""$ENTRY"\" from hosts file"
|
echo "Removed \""$ENTRY"\" from hosts file"
|
||||||
else
|
else
|
||||||
|
|
@ -35,13 +35,13 @@ function hosts_remove {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function help {
|
function ___hosts_help {
|
||||||
echo -e "usage: hosts [add|list|remove] ip_address host_name"
|
echo -e "usage: hosts [add|list|remove] ip_address host_name"
|
||||||
echo
|
echo
|
||||||
echo -e "commands:"
|
echo -e "commands:"
|
||||||
echo -e "\tadd\tMaps the hostname with ip and adds it to $FILE"
|
echo -e "\tadd\tMaps the hostname with ip and adds it to $___HOST_FILE"
|
||||||
echo -e "\tlist\tList all the hostname and ip in $FILE"
|
echo -e "\tlist\tList all the hostname and ip in $___HOST_FILE"
|
||||||
echo -e "\tremove\tRemoved the entry from $FILE based on host_name"
|
echo -e "\tremove\tRemoved the entry from $___HOST_FILE based on host_name"
|
||||||
echo
|
echo
|
||||||
echo -e "examples:"
|
echo -e "examples:"
|
||||||
echo -e "\t$ host add 127.0.0.1 my_localhost"
|
echo -e "\t$ host add 127.0.0.1 my_localhost"
|
||||||
|
|
@ -49,7 +49,7 @@ function help {
|
||||||
}
|
}
|
||||||
|
|
||||||
# the validator function
|
# the validator function
|
||||||
function validate {
|
function ___hosts_validate {
|
||||||
if [[ $1 == "" || $2 == "" ]]
|
if [[ $1 == "" || $2 == "" ]]
|
||||||
then
|
then
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -59,32 +59,34 @@ function validate {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# entry point
|
function hosts {
|
||||||
case $1 in
|
# entry point
|
||||||
"add")
|
case $1 in
|
||||||
validate $2 $3
|
"add")
|
||||||
if [[ $? == 0 ]]
|
___hosts_validate $2 $3
|
||||||
then
|
if [[ $? == 0 ]]
|
||||||
help
|
then
|
||||||
exit 1
|
___hosts_help
|
||||||
else
|
exit 1
|
||||||
hosts_add $2 $3
|
else
|
||||||
fi
|
___hosts_add $2 $3
|
||||||
;;
|
fi
|
||||||
"list")
|
;;
|
||||||
hosts_list
|
"list")
|
||||||
;;
|
___hosts_list
|
||||||
"remove")
|
;;
|
||||||
validate $2 "dummy" # sending dummy to verify only one argument
|
"remove")
|
||||||
if [[ $? == 0 ]]
|
___hosts_validate $2 "dummy" # sending dummy to verify only one argument
|
||||||
then
|
if [[ $? == 0 ]]
|
||||||
help
|
then
|
||||||
exit 1
|
___hosts_help
|
||||||
else
|
exit 1
|
||||||
hosts_remove $2
|
else
|
||||||
fi
|
___hosts_remove $2
|
||||||
;;
|
fi
|
||||||
*)
|
;;
|
||||||
help
|
*)
|
||||||
;;
|
___hosts_help
|
||||||
esac
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue