1 # Completion for the vserver command. Source this file (or on some systems
2 # add it to ~/.bash_completion and start a new shell) and bash's completion
3 # mechanism will know all about vserver's options!
5 # Copyright (C) Thomas Champagne <lafeuil@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software Foundation,
19 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # The latest version of this software can be obtained here:
23 # http://linux-vserver.org/util-vserver:Bash_Completion
27 have vserver-info && {
29 : ${UTIL_VSERVER_VARS:=$(vserver-info - SYSINFO | awk '/prefix:/{ print $2}')/$(test -d /lib64 && echo lib64 || echo lib)/util-vserver/util-vserver-vars}
30 test -e "$UTIL_VSERVER_VARS" && {
32 if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
34 . "$UTIL_VSERVER_VARS"
39 local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
42 cur=${COMP_WORDS[COMP_CWORD]}
44 # find available v-servers:
45 # call function getAllVservers in vserver library
47 names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
50 cmds='start stop restart condrestart suexec exec enter chkconfig \
51 running status unify pkg apt-get apt-config apt-cache \
54 # options (long and short name)
55 cmdOpts='--help --version --debug --defaulttty -s --sync -v \
56 --verbose --silent --'
58 cmdMethodOpts='-m -n --context --confdir --lockfile \
59 --hostname --netdev --netbcast --netmask \
60 --netprefix --interface --cpuset \
61 --cpusetcpus --cpusetmems --cpusetvirt \
62 --initstyle --flags --help --'
64 # if the previous option is a single option
65 helpCmds='--help|--version'
66 if [[ ${COMP_WORDS[1]} == @($helpCmds) ]]
71 # search a verser's name
72 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
74 if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
81 #a vserver has been found
82 if (( $i < ${#COMP_WORDS[@]}-1 ))
84 # Show the vserver command without build
85 case "${COMP_WORDS[i+1]}" in
87 COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" -- $cur ) )
89 # No completion for apt-config
90 stop|restart|condrestart|enter|running|status|apt-config|delete)
93 # I don't know how to do
94 COMPREPLY=( $( compgen -W -- $cur ) )
97 #I don't know how to do
98 COMPREPLY=( $( compgen -W "" -- $cur ) )
101 COMPREPLY=( $( compgen -W "-R" -- $cur ) )
104 func=${COMP_WORDS[i+1]}
105 COMP_WORDS=( ${COMP_WORDS[@]:$((i+1))} )
106 COMP_CWORD=$((COMP_CWORD-i-1))
107 declare -f _${func//-/_} > /dev/null && _${func//-/_}
110 COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
115 #no vserver name found
116 prev=${COMP_WORDS[COMP_CWORD-1]}
118 #search the new name of vserver
119 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )) ; do
120 if [[ ${COMP_WORDS[i]} == !(vserver|-*) ]] ; then
126 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
129 for (( ; j < ${#COMP_WORDS[@]}-1; j++ )) ; do
130 if [[ ${COMP_WORDS[j]} == "--" ]]; then
136 COMPREPLY=( $( compgen -W "-d" -- $cur ) )
139 COMPREPLY=( $( compgen -W "-d" -- $cur ) )
142 COMPREPLY=( $( compgen -W "-d --empty --force --nodeps" -- $cur ) )
147 COMPREPLY=( $( compgen -W "-d -m -s --" -- $cur ) )
151 #the method is not defined
157 if [[ ${COMP_WORDS[j]} == @(build|-m) ]]; then
159 if (( $j+1 < ${#COMP_WORDS[@]}-1 )) ; then
160 method=${COMP_WORDS[j+1]}
165 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
170 -n|--context|--confdir|--lockfile|--hostname|--netdev|--netbcast|--netmask|--netprefix|--interface|--cpuset|--cpusetcpus|--cpusetmems|--cpusetvirt|--initstyle|--flags)
171 COMPREPLY=( $( compgen -W "" -- $cur ) )
174 COMPREPLY=( $( compgen -W "legacy copy apt-rpm yum rpm skeleton debootstrap" -- $cur ) )
177 COMPREPLY=( $( compgen -W "$cmdMethodOpts" -- $cur ) )
182 COMPREPLY=( $( compgen -W "build" -- $cur ) )
185 COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
192 complete -F _vserver vserver
196 local cur cmds cmdOpts helpCmds names func i
199 cur=${COMP_WORDS[COMP_CWORD]}
201 # options (long and short name)
202 cmdOpts='--help --version --quiet -q --all'
204 # if the previous option is a single option
205 helpCmds='--help|--version'
206 if [[ "${COMP_WORDS[1]}" == "@($helpCmds)" ]] ; then
211 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )) ; do
212 if [[ ${COMP_WORDS[i]} = "--" ]] ; then
218 # find available v-servers:
219 # call function getAllVservers in vserver library
221 names_pipe=`echo ${names[@]}" --all" | sed 's/ /|/g'`
223 if (( $i < ${#COMP_WORDS[@]}-1 )) && (( $i < $COMP_CWORD )) ; then
224 func=${COMP_WORDS[0]:1}
225 COMP_WORDS=( $func ${COMP_WORDS[@]:$((i+1))} )
226 COMP_CWORD=$((COMP_CWORD-i))
227 declare -f _${func//-/_} > /dev/null && _${func//-/_}
229 # search a verser's name
230 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )) ; do
231 if [[ ${COMP_WORDS[i]} == @($names_pipe) ]] ; then
237 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
238 if [[ "${COMP_WORDS[i]}" = "--all" ]] ; then
240 COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
243 COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
246 COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
251 complete -F _vapt_rpm_yum vapt-get
252 complete -F _vapt_rpm_yum vrpm
253 complete -F _vapt_rpm_yum vyum
257 local cur prev cmdOpts helpCmds confCmds names names_pipe i
260 cur=${COMP_WORDS[COMP_CWORD]}
262 # find available v-servers:
263 # call function getAllVservers in vserver library
265 names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
267 # options (long and short name)
268 cmdOpts='--help -h --version -V --verbose -v --quiet -q \
269 --vsroot -r --rsh -R --stopstart -s \
272 # if the previous option is a single option
273 helpCmds='--help|-h|--version|-V'
274 if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] ; then
278 confCmds='--ip|-i|--domain|-d'
279 prev=${COMP_WORDS[COMP_CWORD-1]}
280 if [[ $prev == @($confCmds) ]] ; then
284 # search a verser's name
285 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
286 if [[ ${COMP_WORDS[i]} == @($names_pipe) ]] ; then
292 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
295 COMPREPLY=( $( compgen -W "${names[@]} $cmdOpts" -- $cur ) )
299 complete -F _vserver_copy vserver-copy