Add new plugin for virtual host management

This commit is contained in:
Will Mendes 2015-04-26 11:41:30 -03:00
parent ff4663a6b8
commit 39889dc2f2
3 changed files with 174 additions and 0 deletions

54
plugins/vhost/README.md Normal file
View File

@ -0,0 +1,54 @@
# VHOST
> Making virtual host maniputation tasks more easier
## Instalation ##
Open your `.zshrc` file and load `vhost` plugin
```bash
...
plugins=( <your-plugins-list>... vhost)
...
```
## Commands ##
### ls
List all virtual hosts in `/etc/hosts` file
```bash
vhost ls
```
### add
Add a new virtual host in `/etc/hosts` file
```bash
vhost add <ip> <host>
```
### rm
Add a new virtual host in `/etc/hosts` file
```bash
vhost rm <ip> <host>
```
## Author
**Wilson Mendes (willmendesneto)**
+ <https://plus.google.com/+WilsonMendes>
+ <https://twitter.com/willmendesneto>
+ <http://github.com/willmendesneto>
New features comming soon.

60
plugins/vhost/_vhost.sh Normal file
View File

@ -0,0 +1,60 @@
#compdef vhost
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion::complete:vhost:*:commands' group-name commands
zstyle ':completion::complete:vhost:*:vhost_points' group-name vhost_points
zstyle ':completion::complete:vhost::' list-grouped
zmodload zsh/mapfile
function _vhost() {
local CONFIG=$HOME/.vhost
local ret=1
local -a commands
local -a vhost_points
vhost_points=( "${(f)mapfile[$CONFIG]//$HOME/~}" )
commands=(
'ls:List all virtual host of you OS'
'add:Add new virtual host'
'rm:Remove a specific virtual host'
)
_arguments -C \
'1: :->first_arg' \
'2: :->second_arg' && ret=0
case $state in
first_arg)
_describe -t vhost_points "Warp points" vhost_points && ret=0
_describe -t commands "Commands" commands && ret=0
;;
second_arg)
case $words[2] in
add)
_message 'Write the name of your warp point' && ret=0
;;
rm)
_describe -t points "Warp points" vhost_points && ret=0
;;
ls)
_describe -t points "Warp points" vhost_points && ret=0
;;
esac
;;
esac
return $ret
}
_vhost "$@"
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

View File

@ -0,0 +1,60 @@
# frontend from terminal
function vhost() {
local hosts_file
hosts_file='/etc/hosts'
# no keyword provided, simply show how call methods
if [[ "$#" -le 1 && $1 -ne "ls" ]]; then
echo "Please provide a command with params.\nEx:\nvhost <command> <params>\n"
return 1
fi
# check whether the search engine is supported
if [[ ! $1 =~ "(add|ls|rm)" ]];
then
echo "Option '$1' is not supported."
echo "Usage: vhost <ip> <host>"
echo "\t* add <ip> <host>"
echo "\t* ls"
echo "\t* rm <ip> <host>"
echo ""
return 1
fi
case "$1" in
"ls")
cat $hosts_file
;;
"add")
if [[ $# -le 2 ]]; then
echo "This method should receive 2 params <ip> <host>"
fi
vhost="$2 $3"
sudo echo "$vhost" >> $hosts_file
echo "New virtual host '$3' created!"
;;
"rm")
if [[ $# -le 2 ]]; then
echo "This method should receive 2 params <ip> <host>"
fi
sudo -v
vhost="$2 $3"
echo -n " - Removing '$vhost' from $hosts_file... "
cat $hosts_file | grep -v $vhost > /tmp/hosts.tmp
if [[ -s /tmp/hosts.tmp ]]; then
sudo mv /tmp/hosts.tmp $hosts_file
fi
echo "Virtual host '$3' removed!"
;;
*) echo "Command '$1' doesn't exist, sir."
return 1
;;
esac
}