added final plugin and completion

pull/1385/head
Gurkirat Singh 2019-06-08 03:12:10 +05:30
parent 3897e3ac6b
commit b97818b976
2 changed files with 55 additions and 55 deletions

View File

@ -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 complete -F __hosts_completions hosts
fi

View File

@ -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
case $1 in
"add") "add")
validate $2 $3 ___hosts_validate $2 $3
if [[ $? == 0 ]] if [[ $? == 0 ]]
then then
help ___hosts_help
exit 1 exit 1
else else
hosts_add $2 $3 ___hosts_add $2 $3
fi fi
;; ;;
"list") "list")
hosts_list ___hosts_list
;; ;;
"remove") "remove")
validate $2 "dummy" # sending dummy to verify only one argument ___hosts_validate $2 "dummy" # sending dummy to verify only one argument
if [[ $? == 0 ]] if [[ $? == 0 ]]
then then
help ___hosts_help
exit 1 exit 1
else else
hosts_remove $2 ___hosts_remove $2
fi fi
;; ;;
*) *)
help ___hosts_help
;; ;;
esac esac
}