From 39889dc2f2409c887fb18b4e430ef51ac992c66b Mon Sep 17 00:00:00 2001 From: Will Mendes Date: Sun, 26 Apr 2015 11:41:30 -0300 Subject: [PATCH] Add new plugin for virtual host management --- plugins/vhost/README.md | 54 ++++++++++++++++++++++++++++++ plugins/vhost/_vhost.sh | 60 ++++++++++++++++++++++++++++++++++ plugins/vhost/vhost.plugin.zsh | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 plugins/vhost/README.md create mode 100644 plugins/vhost/_vhost.sh create mode 100644 plugins/vhost/vhost.plugin.zsh diff --git a/plugins/vhost/README.md b/plugins/vhost/README.md new file mode 100644 index 000000000..09170ac8f --- /dev/null +++ b/plugins/vhost/README.md @@ -0,0 +1,54 @@ +# VHOST + +> Making virtual host maniputation tasks more easier + + +## Instalation ## + +Open your `.zshrc` file and load `vhost` plugin + +```bash +... +plugins=( ... 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 +``` + + +### rm + +Add a new virtual host in `/etc/hosts` file + +```bash +vhost rm +``` + + +## Author + +**Wilson Mendes (willmendesneto)** ++ ++ ++ + +New features comming soon. diff --git a/plugins/vhost/_vhost.sh b/plugins/vhost/_vhost.sh new file mode 100644 index 000000000..94e2cca4f --- /dev/null +++ b/plugins/vhost/_vhost.sh @@ -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 diff --git a/plugins/vhost/vhost.plugin.zsh b/plugins/vhost/vhost.plugin.zsh new file mode 100644 index 000000000..cf05051a4 --- /dev/null +++ b/plugins/vhost/vhost.plugin.zsh @@ -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 \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 " + echo "\t* add " + echo "\t* ls" + echo "\t* rm " + echo "" + + return 1 + fi + + case "$1" in + "ls") + cat $hosts_file + ;; + "add") + if [[ $# -le 2 ]]; then + echo "This method should receive 2 params " + 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 " + 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 + +}