Difference between revisions of "util-vserver:Bash Completion"

From Linux-VServer

Jump to: navigation, search
m (Vserver Completion moved to util-vserver:Bash Completion: wrong namespace!)
(One intermediate revision by one user not shown)
Line 7: Line 7:
 
For zsh completion, the Debian zsh package has the vserver completion included, see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=365238 for the source, or https://intrigeri.boum.org/svn/pub/home/modules/base/.zsh/functions/_vserver for the upstream SVN repository.
 
For zsh completion, the Debian zsh package has the vserver completion included, see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=365238 for the source, or https://intrigeri.boum.org/svn/pub/home/modules/base/.zsh/functions/_vserver for the upstream SVN repository.
  
 +
<pre><nowiki>
 +
# Completion for the vserver command. Source this file (or on some systems
 +
# add it to ~/.bash_completion and start a new shell) and bash's completion
 +
# mechanism will know all about vserver's options!
 +
#
 +
# Copyright (C) Thomas Champagne <lafeuil@gmail.com>
 +
#
 +
# This program is free software; you can redistribute it and/or modify
 +
# it under the terms of the GNU General Public License as published by
 +
# the Free Software Foundation; either version 2, or (at your option)
 +
# any later version.
 +
#
 +
# This program is distributed in the hope that it will be useful,
 +
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 +
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +
# GNU General Public License for more details.
 +
#
 +
# You should have received a copy of the GNU General Public License
 +
# along with this program; if not, write to the Free Software Foundation,
 +
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 +
#
 +
# The latest version of this software can be obtained here:
 +
#
 +
# http://linux-vserver.org/util-vserver:Bash_Completion
 +
#
 +
# version 0.4.1
  
# Completion for the vserver command. Source this file (or on some systems
+
have vserver-info && {
# add it to ~/.bash_completion and start a new shell) and bash's completion
+
 
# mechanism will know all about vserver's options!
+
: ${UTIL_VSERVER_VARS:=$(vserver-info - SYSINFO |grep prefix: | awk '{ print $2}')/lib/util-vserver/util-vserver-vars}
#
+
test -e "$UTIL_VSERVER_VARS" && {
# Copyright (C) Thomas Champagne <lafeuil@gmail.com>
+
 
#
+
if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
# This program is free software; you can redistribute it and/or modify
+
then
# it under the terms of the GNU General Public License as published by
+
. "$UTIL_VSERVER_VARS"
# the Free Software Foundation; either version 2, or (at your option)
+
. "$_LIB_FUNCTIONS"
# any later version.
+
fi
#
+
 
# This program is distributed in the hope that it will be useful,
+
_vserver() {
# but WITHOUT ANY WARRANTY; without even the implied warranty of
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+
# GNU General Public License for more details.
+
#
+
# You should have received a copy of the GNU General Public License
+
# along with this program; if not, write to the Free Software Foundation,
+
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
#
+
# The latest version of this software can be obtained here:
+
#
+
# http://linux-vserver.org/Vserver_Completion
+
#
+
# version 0.4.1
+
+
have vserver-info && {
+
+
: ${UTIL_VSERVER_VARS:=$(vserver-info - SYSINFO |grep prefix: | awk '{ print $2}')/lib/util-vserver/util-vserver-vars}
+
test -e "$UTIL_VSERVER_VARS" && {
+
       
+
if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
+
then
+
        . "$UTIL_VSERVER_VARS"
+
        . "$_LIB_FUNCTIONS"
+
fi
+
+
_vserver() {
+
 
     local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
 
     local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
 
      
 
      
Line 58: Line 58:
 
     # available commands
 
     # available commands
 
     cmds='start stop restart condrestart suexec exec enter chkconfig \
 
     cmds='start stop restart condrestart suexec exec enter chkconfig \
        running status unify pkg apt-get apt-config apt-cache \
+
running status unify pkg apt-get apt-config apt-cache \
        rpm pkgmgmt delete'
+
rpm pkgmgmt delete'
 
      
 
      
 
     # options (long and short name)
 
     # options (long and short name)
 
     cmdOpts='--help --version --debug --defaulttty -s --sync -v \
 
     cmdOpts='--help --version --debug --defaulttty -s --sync -v \
        --verbose --silent --'
+
--verbose --silent --'
 
      
 
      
 
     cmdMethodOpts='-m -n --context --confdir --lockfile \
 
     cmdMethodOpts='-m -n --context --confdir --lockfile \
        --hostname --netdev --netbcast --netmask \
+
--hostname --netdev --netbcast --netmask \
        --netprefix --interface --cpuset \
+
--netprefix --interface --cpuset \
        --cpusetcpus --cpusetmems --cpusetvirt \
+
--cpusetcpus --cpusetmems --cpusetvirt \
        --initstyle --flags --help --'
+
--initstyle --flags --help --'
 
      
 
      
 
     # if the previous option is a single option  
 
     # if the previous option is a single option  
Line 75: Line 75:
 
     if [[ ${COMP_WORDS[1]} == @($helpCmds) ]]  
 
     if [[ ${COMP_WORDS[1]} == @($helpCmds) ]]  
 
     then
 
     then
        return 0
+
return 0
 
     fi
 
     fi
 
      
 
      
Line 81: Line 81:
 
     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
 
     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
 
     do
 
     do
        if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
+
if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
        then
+
then
 
           # it's found
 
           # it's found
            break
+
    break
        fi
+
fi
 
     done
 
     done
 
      
 
      
        #a vserver has been found  
+
#a vserver has been found  
 
     if (( $i < ${#COMP_WORDS[@]}-1 ))  
 
     if (( $i < ${#COMP_WORDS[@]}-1 ))  
 
     then  
 
     then  
        #Show the vserver command without build
+
#Show the vserver command without build
        case "${COMP_WORDS[i+1]}" in
+
case "${COMP_WORDS[i+1]}" in
            start)
+
    start)
                COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" -- $cur ) )
+
COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" -- $cur ) )
                ;;
+
;;
            # No completion for apt-config
+
    # No completion for apt-config
            stop|restart|condrestart|enter|running|status|apt-config|delete)
+
    stop|restart|condrestart|enter|running|status|apt-config|delete)
                ;;
+
;;
            suexec)
+
    suexec)
                # I don't know how to do
+
# I don't know how to do
                COMPREPLY=( $( compgen -W  -- $cur ) )
+
COMPREPLY=( $( compgen -W  -- $cur ) )
                ;;
+
;;
            exec)
+
    exec)
                #I don't know how to do
+
#I don't know how to do
                COMPREPLY=( $( compgen -W "" -- $cur ) )
+
COMPREPLY=( $( compgen -W "" -- $cur ) )
                ;;
+
;;
            unify)
+
    unify)
                COMPREPLY=( $( compgen -W "-R" -- $cur ) )
+
COMPREPLY=( $( compgen -W "-R" -- $cur ) )
                ;;
+
;;
            apt-get|apt-cache)
+
    apt-get|apt-cache)
                func=${COMP_WORDS[i+1]}
+
func=${COMP_WORDS[i+1]}
                COMP_WORDS=( ${COMP_WORDS[@]:$((i+1))} )
+
COMP_WORDS=( ${COMP_WORDS[@]:$((i+1))} )
                COMP_CWORD=$((COMP_CWORD-i-1))
+
COMP_CWORD=$((COMP_CWORD-i-1))
                declare -f _${func//-/_} > /dev/null && _${func//-/_}
+
declare -f _${func//-/_} > /dev/null && _${func//-/_}
                ;;
+
;;
            *)
+
    *)
                COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )  
+
COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
                ;;
+
;;
        esac
+
esac
        return 0
+
return 0
 
     else
 
     else
        #no vserver name found
+
#no vserver name found
        prev=${COMP_WORDS[COMP_CWORD-1]}
+
prev=${COMP_WORDS[COMP_CWORD-1]}
       
+
        #search the new name of vserver  
+
#search the new name of vserver  
        for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
+
for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
 
             if [[ ${COMP_WORDS[i]} == !(vserver|-*) ]];  then
 
             if [[ ${COMP_WORDS[i]} == !(vserver|-*) ]];  then
                # it's found
+
        # it's found
                break
+
break
            fi
+
    fi
        done
+
done
               
+
        if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
+
if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
            j=$i
+
    j=$i
            i=${#COMP_WORDS[@]}
+
    i=${#COMP_WORDS[@]}
            for (( ; j < ${#COMP_WORDS[@]}-1; j++ )); do
+
    for (( ; j < ${#COMP_WORDS[@]}-1; j++ )); do
 
                 if [[ ${COMP_WORDS[j]} == "--" ]];  then
 
                 if [[ ${COMP_WORDS[j]} == "--" ]];  then
                    # method's parameter
+
    # method's parameter
                    case "$method" in
+
    case "$method" in
                        legacy|copy)
+
legacy|copy)
                            ;;
+
    ;;
                        apt-rpm)
+
apt-rpm)
                            COMPREPLY=( $( compgen -W "-d" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "-d" -- $cur ) )
                            ;;
+
    ;;
                        yum)
+
yum)
                            COMPREPLY=( $( compgen -W "-d" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "-d" -- $cur ) )
                            ;;
+
    ;;
                        rpm)  
+
rpm)  
                            COMPREPLY=( $( compgen -W "-d --empty --force --nodeps" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "-d --empty --force --nodeps" -- $cur ) )
                            ;;
+
    ;;
                        skeleton)
+
skeleton)
                            ;;
+
    ;;
                        debootstrap)
+
debootstrap)
                            COMPREPLY=( $( compgen -W "-d -m -s --" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "-d -m -s --" -- $cur ) )
                            ;;
+
    ;;
                        *)
+
*)
 
                             #do nothing  
 
                             #do nothing  
 
                             #the method is not defined
 
                             #the method is not defined
                            ;;
+
    ;;
                    esac
+
    esac
 
                     return 0
 
                     return 0
 
                     break
 
                     break
 
                 fi
 
                 fi
                if [[ ${COMP_WORDS[j]} == @(build|-m) ]];  then
+
if [[ ${COMP_WORDS[j]} == @(build|-m) ]];  then
                    i=$j
+
    i=$j
                    if (( $j+1 < ${#COMP_WORDS[@]}-1 )) ; then
+
    if (( $j+1 < ${#COMP_WORDS[@]}-1 )) ; then
                        method=${COMP_WORDS[j+1]}
+
method=${COMP_WORDS[j+1]}
                    fi
+
    fi
 
                 fi
 
                 fi
 
             done  
 
             done  
           
+
   
            if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
+
    if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
                case $prev in
+
case $prev in
                    --help)
+
    --help)
                        #do nothing
+
#do nothing
 +
;;
 +
    -n|--context|--confdir|--lockfile|--hostname|--netdev|--netbcast|--netmask|--netprefix|--interface|--cpuset|--cpusetcpus|--cpusetmems|--cpusetvirt|--initstyle|--flags)
 +
COMPREPLY=( $( compgen -W "" -- $cur ) )
 
                         ;;
 
                         ;;
                    -n|--context|--confdir|--lockfile|--hostname|--netdev|--netbcast|--netmask|--netprefix|--interface|--cpuset|--cpusetcpus|--cpusetmems|--cpusetvirt|--initstyle|--flags)
+
    -m)
                        COMPREPLY=( $( compgen -W "" -- $cur ) )
+
COMPREPLY=( $( compgen -W "legacy copy apt-rpm yum rpm skeleton debootstrap" -- $cur ) )
                        ;;
+
;;
                    -m)
+
    *)
                        COMPREPLY=( $( compgen -W "legacy copy apt-rpm yum rpm skeleton debootstrap" -- $cur ) )
+
COMPREPLY=( $( compgen -W "$cmdMethodOpts" -- $cur ) )
                        ;;
+
;;
                    *)
+
esac
                        COMPREPLY=( $( compgen -W "$cmdMethodOpts" -- $cur ) )
+
                        ;;
+
    else
                esac
+
COMPREPLY=( $( compgen -W "build" -- $cur ) )
               
+
    fi
            else
+
else
                COMPREPLY=( $( compgen -W "build" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
            fi
+
fi
        else
+
return 0
            COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
+
        fi
+
        return 0
+
 
     fi
 
     fi
 
      
 
      
 
     return 0
 
     return 0
}
+
}
complete -F _vserver vserver
+
complete -F _vserver vserver
 
+
 
_vapt_rpm_yum()
+
_vapt_rpm_yum()
{
+
{
 
     local cur cmds cmdOpts helpCmds names func i
 
     local cur cmds cmdOpts helpCmds names func i
+
 
 
     COMPREPLY=()
 
     COMPREPLY=()
 
     cur=${COMP_WORDS[COMP_CWORD]}
 
     cur=${COMP_WORDS[COMP_CWORD]}
Line 214: Line 214:
 
     helpCmds='--help|--version'
 
     helpCmds='--help|--version'
 
     if [[ "${COMP_WORDS[1]}" == "@($helpCmds)" ]] ; then
 
     if [[ "${COMP_WORDS[1]}" == "@($helpCmds)" ]] ; then
        return 0
+
return 0
 
     fi
 
     fi
 
      
 
      
Line 220: Line 220:
 
     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
 
     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
 
         if [[ ${COMP_WORDS[i]} = "--" ]];  then
 
         if [[ ${COMP_WORDS[i]} = "--" ]];  then
            # it's found
+
    # it's found
            break
+
    break
        fi
+
fi
 
     done
 
     done
 
      
 
      
Line 231: Line 231:
 
      
 
      
 
     if (( $i < ${#COMP_WORDS[@]}-1 )) && (( $i < $COMP_CWORD )) ; then  
 
     if (( $i < ${#COMP_WORDS[@]}-1 )) && (( $i < $COMP_CWORD )) ; then  
        func=${COMP_WORDS[0]:1}
+
func=${COMP_WORDS[0]:1}
        COMP_WORDS=( $func ${COMP_WORDS[@]:$((i+1))} )
+
COMP_WORDS=( $func ${COMP_WORDS[@]:$((i+1))} )
        COMP_CWORD=$((COMP_CWORD-i))
+
COMP_CWORD=$((COMP_CWORD-i))
        declare -f _${func//-/_} > /dev/null && _${func//-/_}
+
declare -f _${func//-/_} > /dev/null && _${func//-/_}
     else      
+
     else
        # search a verser's name
+
# search a verser's name
        for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
+
for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
            if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
+
    if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
                # it's found
+
# it's found
                break
+
break
            fi
+
    fi
        done
+
done
+
 
        if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
+
if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
            if [[ "${COMP_WORDS[i]}" = "--all"  ]]; then
+
    if [[ "${COMP_WORDS[i]}" = "--all"  ]]; then
                cmdOpts='--'
+
cmdOpts='--'
                COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
+
COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
            else
+
    else
                cmdOpts='--'
+
cmdOpts='--'
                COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
+
COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
            fi
+
    fi
        else
+
else
            COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
+
    COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
        fi
+
fi
 
     fi
 
     fi
 
     return 0
 
     return 0
}
+
}
complete -F _vapt_rpm_yum vapt-get
+
complete -F _vapt_rpm_yum vapt-get
complete -F _vapt_rpm_yum vrpm
+
complete -F _vapt_rpm_yum vrpm
complete -F _vapt_rpm_yum vyum
+
complete -F _vapt_rpm_yum vyum
+
 
_vserver_copy()
+
_vserver_copy()
{
+
{
 
     local cur prev cmdOpts helpCmds confCmds names names_pipe i
 
     local cur prev cmdOpts helpCmds confCmds names names_pipe i
 
      
 
      
Line 276: Line 276:
 
     # options (long and short name)
 
     # options (long and short name)
 
     cmdOpts='--help -h --version -V --verbose -v --quiet -q \
 
     cmdOpts='--help -h --version -V --verbose -v --quiet -q \
                  --vsroot -r --rsh -R --stopstart -s \
+
  --vsroot -r --rsh -R --stopstart -s \
                  --domain -d --ip -i'
+
  --domain -d --ip -i'
 
      
 
      
 
     # if the previous option is a single option  
 
     # if the previous option is a single option  
 
     helpCmds='--help|-h|--version|-V'
 
     helpCmds='--help|-h|--version|-V'
 
     if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] ; then
 
     if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] ; then
        return 0
+
return 0
 
     fi
 
     fi
 
      
 
      
Line 288: Line 288:
 
     prev=${COMP_WORDS[COMP_CWORD-1]}
 
     prev=${COMP_WORDS[COMP_CWORD-1]}
 
     if [[ $prev == @($confCmds) ]]; then
 
     if [[ $prev == @($confCmds) ]]; then
        return 0
+
return 0
 
     fi
 
     fi
 
      
 
      
Line 294: Line 294:
 
     for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
 
     for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
 
         if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
 
         if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
            # it's found
+
    # it's found
            break
+
    break
        fi
+
fi
 
     done
 
     done
 
      
 
      
 
     if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
 
     if (( $i < ${#COMP_WORDS[@]}-1 )) ; then  
        return 0
+
return 0
 
     else
 
     else
        COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
+
COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
 
     fi
 
     fi
 
     return 0
 
     return 0
}
+
}
complete -F _vserver_copy vserver-copy
+
complete -F _vserver_copy vserver-copy
+
}
+
}
+
  
 +
}
 +
}
 +
</nowiki></pre>
  
 
;Changelog :
 
;Changelog :
Line 318: Line 318:
 
:0.3.1 : Add the command vapt-get vrpm vyum vserver-copy
 
:0.3.1 : Add the command vapt-get vrpm vyum vserver-copy
 
:0.2.0 : First release with just the command vserver
 
:0.2.0 : First release with just the command vserver
 +
 +
[[Category:Configuration]]

Revision as of 22:19, 21 October 2011

To test this, put the source in your .bashrc or put it in the file /etc/bash_completion.d/vserver (Debian).

This release (0.4) supports the commands vserver vapt-get vrpm vyum vserver-copy.

This script is included in the util-vserver Debian package, and is automatically installed into /etc/bash_completion.d/vserver when you install this package.

For zsh completion, the Debian zsh package has the vserver completion included, see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=365238 for the source, or https://intrigeri.boum.org/svn/pub/home/modules/base/.zsh/functions/_vserver for the upstream SVN repository.

# Completion for the vserver command. Source this file (or on some systems
# add it to ~/.bash_completion and start a new shell) and bash's completion
# mechanism will know all about vserver's options!
#
# Copyright (C) Thomas Champagne <lafeuil@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# The latest version of this software can be obtained here:
#
# http://linux-vserver.org/util-vserver:Bash_Completion
#
# version 0.4.1

have vserver-info && {

: ${UTIL_VSERVER_VARS:=$(vserver-info - SYSINFO |grep prefix: | awk '{ print $2}')/lib/util-vserver/util-vserver-vars}
test -e "$UTIL_VSERVER_VARS" && {

if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
then
	. "$UTIL_VSERVER_VARS"
	. "$_LIB_FUNCTIONS"
fi

_vserver() {
    local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    
    # find available v-servers: 
    # call function getAllVservers in vserver library
    getAllVservers names
    names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
    
    # available commands
    cmds='start stop restart condrestart suexec exec enter chkconfig \
	running status unify pkg apt-get apt-config apt-cache \
	rpm pkgmgmt delete'
    
    # options (long and short name)
    cmdOpts='--help --version --debug --defaulttty -s --sync -v \
	--verbose --silent --'
    
    cmdMethodOpts='-m -n --context --confdir --lockfile \
	--hostname --netdev --netbcast --netmask \
	--netprefix --interface --cpuset \
	--cpusetcpus --cpusetmems --cpusetvirt \
	--initstyle --flags --help --'
    
    # if the previous option is a single option 
    helpCmds='--help|--version'
    if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] 
    then
	return 0
    fi
    
    # search a verser's name
    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
    do
	if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
	then
          # it's found
	    break
	fi
    done
    
	#a vserver has been found 
    if (( $i < ${#COMP_WORDS[@]}-1 )) 
    then 
	#Show the vserver command without build
	case "${COMP_WORDS[i+1]}" in
	    start)
		COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" -- $cur ) )
		;;
	    # No completion for apt-config
	    stop|restart|condrestart|enter|running|status|apt-config|delete)
		;;
	    suexec)
		# I don't know how to do
		COMPREPLY=( $( compgen -W  -- $cur ) )
		;;
	    exec)
		#I don't know how to do
		COMPREPLY=( $( compgen -W "" -- $cur ) )
		;;
	    unify)
		COMPREPLY=( $( compgen -W "-R" -- $cur ) )
		;;
	    apt-get|apt-cache)
		func=${COMP_WORDS[i+1]}
		COMP_WORDS=( ${COMP_WORDS[@]:$((i+1))} )
		COMP_CWORD=$((COMP_CWORD-i-1))
		declare -f _${func//-/_} > /dev/null && _${func//-/_}
		;;
	    *)
		COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )	
		;;
	esac
	return 0
    else
	#no vserver name found
	prev=${COMP_WORDS[COMP_CWORD-1]}
	
	#search the new name of vserver 
	for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
            if [[ ${COMP_WORDS[i]} == !(vserver|-*) ]];  then
	        # it's found
		break
	    fi
	done
		
	if (( $i < ${#COMP_WORDS[@]}-1 )) ; then 
	    j=$i
	    i=${#COMP_WORDS[@]}
	    for (( ; j < ${#COMP_WORDS[@]}-1; j++ )); do
                if [[ ${COMP_WORDS[j]} == "--" ]];  then
		    # method's parameter
		    case "$method" in
			legacy|copy)
			    ;;
			apt-rpm)
			    COMPREPLY=( $( compgen -W "-d" -- $cur ) )
			    ;;
			yum)
			    COMPREPLY=( $( compgen -W "-d" -- $cur ) )
			    ;;
			rpm) 
			    COMPREPLY=( $( compgen -W "-d --empty --force --nodeps" -- $cur ) )
			    ;;
			skeleton)
			    ;;
			debootstrap)
			    COMPREPLY=( $( compgen -W "-d -m -s --" -- $cur ) )
			    ;;
			*)
                            #do nothing 
                            #the method is not defined
			    ;;
		    esac
                    return 0
                    break
                fi
		if [[ ${COMP_WORDS[j]} == @(build|-m) ]];  then
		    i=$j
		    if (( $j+1 < ${#COMP_WORDS[@]}-1 )) ; then
			method=${COMP_WORDS[j+1]}
		    fi
                fi
            done 
	    
	    if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
		case $prev in
		    --help)
			#do nothing
			;;
		    -n|--context|--confdir|--lockfile|--hostname|--netdev|--netbcast|--netmask|--netprefix|--interface|--cpuset|--cpusetcpus|--cpusetmems|--cpusetvirt|--initstyle|--flags)
			COMPREPLY=( $( compgen -W "" -- $cur ) )
                        ;;
		    -m)
			COMPREPLY=( $( compgen -W "legacy copy apt-rpm yum rpm skeleton debootstrap" -- $cur ) )
			;;
		    *)
			COMPREPLY=( $( compgen -W "$cmdMethodOpts" -- $cur ) )
			;;
		esac
		
	    else
		COMPREPLY=( $( compgen -W "build" -- $cur ) )
	    fi
	else
	    COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
	fi
	return 0
    fi
    
    return 0
}
complete -F _vserver vserver

_vapt_rpm_yum()
{
    local cur cmds cmdOpts helpCmds names func i

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    
    # options (long and short name)
    cmdOpts='--help --version --quiet -q --all'
    
    # if the previous option is a single option 
    helpCmds='--help|--version'
    if [[ "${COMP_WORDS[1]}" == "@($helpCmds)" ]] ; then
	return 0
    fi
    
    # search --
    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
        if [[ ${COMP_WORDS[i]} = "--" ]];  then
	    # it's found
	    break
	fi
    done
    
    # find available v-servers: 
    # call function getAllVservers in vserver library
    getAllVservers names
    names_pipe=`echo ${names[@]}" --all" | sed 's/ /|/g'`
    
    if (( $i < ${#COMP_WORDS[@]}-1 )) && (( $i < $COMP_CWORD )) ; then 
	func=${COMP_WORDS[0]:1}
	COMP_WORDS=( $func ${COMP_WORDS[@]:$((i+1))} )
	COMP_CWORD=$((COMP_CWORD-i))
	declare -f _${func//-/_} > /dev/null && _${func//-/_}
    else	
	# search a verser's name
	for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
	    if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
		# it's found
		break
	    fi
	done

	if (( $i < ${#COMP_WORDS[@]}-1 )) ; then 
	    if [[ "${COMP_WORDS[i]}" = "--all"  ]]; then
		cmdOpts='--'
		COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
	    else
		cmdOpts='--'
		COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
	    fi
	else
	    COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
	fi
    fi
    return 0
}
complete -F _vapt_rpm_yum vapt-get
complete -F _vapt_rpm_yum vrpm
complete -F _vapt_rpm_yum vyum

_vserver_copy()
{
    local cur prev cmdOpts helpCmds confCmds names names_pipe i
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    
    # find available v-servers: 
    # call function getAllVservers in vserver library
    getAllVservers names
    names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
    
    # options (long and short name)
    cmdOpts='--help -h --version -V --verbose -v --quiet -q \
		   --vsroot -r --rsh -R --stopstart -s \
		   --domain -d --ip -i'
    
    # if the previous option is a single option 
    helpCmds='--help|-h|--version|-V'
    if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] ; then
	return 0
    fi
    
    confCmds='--ip|-i|--domain|-d'
    prev=${COMP_WORDS[COMP_CWORD-1]}
    if [[ $prev == @($confCmds) ]]; then
	return 0
    fi
    
    # search a verser's name
    for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
        if [[ ${COMP_WORDS[i]} == @($names_pipe) ]];  then
	    # it's found
	    break
	fi
    done
    
    if (( $i < ${#COMP_WORDS[@]}-1 )) ; then 
	return 0
    else
	COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
    fi
    return 0
}
complete -F _vserver_copy vserver-copy

}
}
Changelog 
0.4.1 : handle multiple sourcing scenarios
0.4.0 : Update command vserver build
0.3.2 : Add Licence
0.3.1 : Add the command vapt-get vrpm vyum vserver-copy
0.2.0 : First release with just the command vserver
Personal tools