[Vserver] Completion for vserver

From: Thomas Champagne <lafeuil_at_gmail.com>
Date: Fri 20 Jan 2006 - 16:04:52 GMT
Message-ID: <1d4c7fd50601200804y31af7a7bo@mail.gmail.com>

Hi

To learn bash, I decided to create the completion for vserver's commands.
So, I send you the first release here because I don't know where I can post
it.

This release support only the command "vserver".

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

In waiting of your comments.
Thomas

--------------------------------------

# 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!

# Known to work with bash 2.05a with programmable completion and extended
# pattern matching enabled (use 'shopt -s extglob progcomp' to enable
# these if they are not already enabled).

: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
test -e "$UTIL_VSERVER_VARS" && {

. "$UTIL_VSERVER_VARS"
. "$_LIB_FUNCTIONS"

_vserver()
{
        local cur cmds cmdOpts helpCmds names names_pipe func

        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 build unify pkg apt-get apt-config apt-cache\
                rpm pkgmgmt'

        # options (long and short name)
        cmdOpts='--help --version --debug --defaulttty -s --sync -v \
                   --verbose --silent --'

        # 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

        if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
                case ${COMP_WORDS[i+1]} in
                start)
                        COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" --
$cu r ) )
                        ;;
                # No completion for apt-config
                stop|restart|condrestart|enter|running|status|apt-config)
                        ;;
                suexec)
                        # I don't know how to do
                        COMPREPLY=( $( compgen -W -- $cur ) )
                        ;;
                exec)
                        # I don't know how to do
                        COMPREPLY=( $( compgen -W "" -- $cur ) )
                        ;;
                build)

                        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))
                        _${func}
                        ;;
                *)
                        COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
                        ;;
                esac
                return 0
        else
                COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
                return 0
        fi

        return 0
}
complete -F _vserver -o default vserver
}

_______________________________________________
Vserver mailing list
Vserver@list.linux-vserver.org
http://list.linux-vserver.org/mailman/listinfo/vserver
Received on Fri Jan 20 16:09:04 2006

[Next/Previous Months] [Main vserver Project Homepage] [Howto Subscribe/Unsubscribe] [Paul Sladen's vserver stuff]
Generated on Fri 20 Jan 2006 - 16:09:06 GMT by hypermail 2.1.8