]> git.pld-linux.org Git - packages/bash-completion-vserver.git/blob - bash-completion-vserver.sh
- use full path for vserver-info
[packages/bash-completion-vserver.git] / bash-completion-vserver.sh
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!
4 #
5 # Copyright (C) Thomas Champagne <lafeuil@gmail.com>
6 #
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)
10 # any later version.
11 #
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.
16 #
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.
20 #
21 # The latest version of this software can be obtained here:
22 #
23 # http://linux-vserver.org/util-vserver:Bash_Completion
24 #
25 # version 0.4.1
26
27 have vserver-info && {
28
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" && {
31
32 if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
33 then
34 . "$UTIL_VSERVER_VARS"
35 . "$_LIB_FUNCTIONS"
36 fi
37
38 _vserver() {
39         local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
40
41         COMPREPLY=()
42         cur=${COMP_WORDS[COMP_CWORD]}
43
44     # find available v-servers: 
45         # call function getAllVservers in vserver library
46         getAllVservers names
47         names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
48
49         # available commands
50         cmds='start stop restart condrestart suexec exec enter chkconfig \
51         running status unify pkg apt-get apt-config apt-cache \
52         rpm pkgmgmt delete'
53
54         # options (long and short name)
55         cmdOpts='--help --version --debug --defaulttty -s --sync -v \
56         --verbose --silent --'
57
58         cmdMethodOpts='-m -n --context --confdir --lockfile \
59         --hostname --netdev --netbcast --netmask \
60         --netprefix --interface --cpuset \
61         --cpusetcpus --cpusetmems --cpusetvirt \
62         --initstyle --flags --help --'
63
64         # if the previous option is a single option
65         helpCmds='--help|--version'
66     if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] 
67     then
68                 return 0
69         fi
70
71     # search a verser's name
72     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
73     do
74         if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
75         then
76           # it's found
77                         break
78                 fi
79         done
80
81         #a vserver has been found
82     if (( $i < ${#COMP_WORDS[@]}-1 )) 
83     then 
84                 # Show the vserver command without build
85                 case "${COMP_WORDS[i+1]}" in
86                         start)
87                                 COMPREPLY=( $( compgen -W "--rescue --rescue-cmd" -- $cur ) )
88                                 ;;
89                         # No completion for apt-config
90                         stop|restart|condrestart|enter|running|status|apt-config|delete)
91                                 ;;
92                         suexec)
93                                 # I don't know how to do
94                                 COMPREPLY=( $( compgen -W  -- $cur ) )
95                                 ;;
96                         exec)
97                                 #I don't know how to do
98                                 COMPREPLY=( $( compgen -W "" -- $cur ) )
99                                 ;;
100                         unify)
101                                 COMPREPLY=( $( compgen -W "-R" -- $cur ) )
102                                 ;;
103                         apt-get|apt-cache)
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//-/_}
108                                 ;;
109                         *)
110                                 COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
111                                 ;;
112                 esac
113                 return 0
114         else
115                 #no vserver name found
116                 prev=${COMP_WORDS[COMP_CWORD-1]}
117
118                 #search the new name of vserver
119                 for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )) ; do
120                         if [[ ${COMP_WORDS[i]} == !(vserver|-*) ]] ; then
121                 # it's found
122                                 break
123                         fi
124                 done
125
126                 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
127                         j=$i
128                         i=${#COMP_WORDS[@]}
129                         for (( ; j < ${#COMP_WORDS[@]}-1; j++ )) ; do
130                                 if [[ ${COMP_WORDS[j]} == "--" ]];  then
131                                         # method's parameter
132                                         case "$method" in
133                                                 legacy|copy)
134                                                         ;;
135                                                 apt-rpm)
136                                                         COMPREPLY=( $( compgen -W "-d" -- $cur ) )
137                                                         ;;
138                                                 yum)
139                                                         COMPREPLY=( $( compgen -W "-d" -- $cur ) )
140                                                         ;;
141                                                 rpm)
142                                                         COMPREPLY=( $( compgen -W "-d --empty --force --nodeps" -- $cur ) )
143                                                         ;;
144                                                 skeleton)
145                                                         ;;
146                                                 debootstrap)
147                                                         COMPREPLY=( $( compgen -W "-d -m -s --" -- $cur ) )
148                                                         ;;
149                                                 *)
150                             #do nothing 
151                             #the method is not defined
152                                                         ;;
153                                         esac
154                                         return 0
155                                         break
156                                 fi
157                                 if [[ ${COMP_WORDS[j]} == @(build|-m) ]];  then
158                                         i=$j
159                                         if (( $j+1 < ${#COMP_WORDS[@]}-1 )) ; then
160                                                 method=${COMP_WORDS[j+1]}
161                                         fi
162                                 fi
163                         done
164
165                         if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
166                                 case $prev in
167                                         --help)
168                         #do nothing
169                                                 ;;
170                                         -n|--context|--confdir|--lockfile|--hostname|--netdev|--netbcast|--netmask|--netprefix|--interface|--cpuset|--cpusetcpus|--cpusetmems|--cpusetvirt|--initstyle|--flags)
171                                                 COMPREPLY=( $( compgen -W "" -- $cur ) )
172                                                 ;;
173                                         -m)
174                                                 COMPREPLY=( $( compgen -W "legacy copy apt-rpm yum rpm skeleton debootstrap" -- $cur ) )
175                                                 ;;
176                                         *)
177                                                 COMPREPLY=( $( compgen -W "$cmdMethodOpts" -- $cur ) )
178                                                 ;;
179                                 esac
180                 
181                         else
182                                 COMPREPLY=( $( compgen -W "build" -- $cur ) )
183                         fi
184                 else
185                         COMPREPLY=( $( compgen -W "${names[*]} $cmdOpts" -- $cur ) )
186                 fi
187                 return 0
188         fi
189
190         return 0
191 }
192 complete -F _vserver vserver
193
194 _vapt_rpm_yum()
195 {
196         local cur cmds cmdOpts helpCmds names func i
197
198         COMPREPLY=()
199         cur=${COMP_WORDS[COMP_CWORD]}
200
201         # options (long and short name)
202         cmdOpts='--help --version --quiet -q --all'
203
204         # if the previous option is a single option
205         helpCmds='--help|--version'
206         if [[ "${COMP_WORDS[1]}" == "@($helpCmds)" ]] ; then
207                 return 0
208         fi
209
210         # search --
211         for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )) ; do
212                 if [[ ${COMP_WORDS[i]} = "--" ]] ; then
213             # it's found
214                         break
215                 fi
216         done
217
218     # find available v-servers: 
219         # call function getAllVservers in vserver library
220         getAllVservers names
221         names_pipe=`echo ${names[@]}" --all" | sed 's/ /|/g'`
222
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//-/_}
228         else
229         # search a verser's name
230                 for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )) ; do
231                         if [[ ${COMP_WORDS[i]} == @($names_pipe) ]] ; then
232                 # it's found
233                                 break
234                         fi
235                 done
236
237                 if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
238                         if [[ "${COMP_WORDS[i]}" = "--all"  ]] ; then
239                                 cmdOpts='--'
240                                 COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
241                         else
242                                 cmdOpts='--'
243                                 COMPREPLY=( $( compgen -W "${names[*]} $cmdOpts" -- $cur ) )
244                         fi
245                 else
246                         COMPREPLY=( $( compgen -W "${names[*]} $cmdOpts" -- $cur ) )
247                 fi
248         fi
249         return 0
250 }
251 complete -F _vapt_rpm_yum vapt-get
252 complete -F _vapt_rpm_yum vrpm
253 complete -F _vapt_rpm_yum vyum
254
255 _vserver_copy()
256 {
257         local cur prev cmdOpts helpCmds confCmds names names_pipe i
258
259         COMPREPLY=()
260         cur=${COMP_WORDS[COMP_CWORD]}
261
262     # find available v-servers: 
263         # call function getAllVservers in vserver library
264         getAllVservers names
265         names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
266
267         # options (long and short name)
268         cmdOpts='--help -h --version -V --verbose -v --quiet -q \
269                  --vsroot -r --rsh -R --stopstart -s \
270                  --domain -d --ip -i'
271
272         # if the previous option is a single option
273         helpCmds='--help|-h|--version|-V'
274         if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] ; then
275                 return 0
276         fi
277
278         confCmds='--ip|-i|--domain|-d'
279         prev=${COMP_WORDS[COMP_CWORD-1]}
280         if [[ $prev == @($confCmds) ]] ; then
281                 return 0
282         fi
283
284     # search a verser's name
285         for (( i=0; i <  ${#COMP_WORDS[@]}-1; i++ )); do
286                 if [[ ${COMP_WORDS[i]} == @($names_pipe) ]] ; then
287             # it's found
288                         break
289                 fi
290         done
291
292         if (( $i < ${#COMP_WORDS[@]}-1 )) ; then
293                 return 0
294         else
295                 COMPREPLY=( $( compgen -W "${names[*]} $cmdOpts" -- $cur ) )
296         fi
297         return 0
298 }
299 complete -F _vserver_copy vserver-copy
300
301 }
302 }
303
This page took 0.094518 seconds and 3 git commands to generate.