Gentoo Archives: gentoo-commits

From: "Richard Yao (ryao)" <ryao@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in sys-fs/zfs/files: bash-completion-r1
Date: Thu, 21 Nov 2013 16:21:30
Message-Id: 20131121162123.E84E92004B@flycatcher.gentoo.org
1 ryao 13/11/21 16:21:23
2
3 Added: bash-completion-r1
4 Log:
5 Python 3 support; Fix memory leak in libzfs; Import updated bash completion script from Ubuntu (with silent sudo functionality commented out)
6
7 (Portage version: 2.2.7/cvs/Linux x86_64, signed Manifest commit with key 0xBEE84C64)
8
9 Revision Changes Path
10 1.1 sys-fs/zfs/files/bash-completion-r1
11
12 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-fs/zfs/files/bash-completion-r1?rev=1.1&view=markup
13 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-fs/zfs/files/bash-completion-r1?rev=1.1&content-type=text/plain
14
15 Index: bash-completion-r1
16 ===================================================================
17 # Copyright (c) 2013, Aneurin Price <aneurin.price@×××××.com>
18
19 # Permission is hereby granted, free of charge, to any person
20 # obtaining a copy of this software and associated documentation
21 # files (the "Software"), to deal in the Software without
22 # restriction, including without limitation the rights to use,
23 # copy, modify, merge, publish, distribute, sublicense, and/or sell
24 # copies of the Software, and to permit persons to whom the
25 # Software is furnished to do so, subject to the following
26 # conditions:
27
28 # The above copyright notice and this permission notice shall be
29 # included in all copies or substantial portions of the Software.
30
31 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
33 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
35 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
36 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38 # OTHER DEALINGS IN THE SOFTWARE.
39
40 #if [[ -w /dev/zfs ]]; then
41 __ZFS_CMD="zfs"
42 __ZPOOL_CMD="zpool"
43 #else
44 # __ZFS_CMD="sudo zfs"
45 # __ZPOOL_CMD="sudo zpool"
46 #fi
47
48 __zfs_get_commands()
49 {
50 $__ZFS_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | cut -f1 -d '|' | uniq
51 }
52
53 __zfs_get_properties()
54 {
55 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all name space
56 }
57
58 __zfs_get_editable_properties()
59 {
60 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
61 }
62
63 __zfs_get_inheritable_properties()
64 {
65 $__ZFS_CMD get 2>&1 | awk '$3 == "YES" {print $1}'
66 }
67
68 __zfs_list_datasets()
69 {
70 $__ZFS_CMD list -H -o name -t filesystem,volume
71 }
72
73 __zfs_list_filesystems()
74 {
75 $__ZFS_CMD list -H -o name -t filesystem
76 }
77
78 __zfs_match_snapshot()
79 {
80 local base_dataset=${cur%@*}
81 if [[ $base_dataset != $cur ]]
82 then
83 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset
84 else
85 $__ZFS_CMD list -H -o name -t filesystem,volume | awk '{print $1"@"}'
86 fi
87 }
88
89 __zfs_match_explicit_snapshot()
90 {
91 local base_dataset=${cur%@*}
92 if [[ $base_dataset != $cur ]]
93 then
94 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset
95 fi
96 }
97
98 __zfs_match_multiple_snapshots()
99 {
100 local existing_opts=$(expr "$cur" : '\(.*\)[%,]')
101 if [[ $existing_opts ]]
102 then
103 local base_dataset=${cur%@*}
104 if [[ $base_dataset != $cur ]]
105 then
106 local cur=${cur##*,}
107 if [[ $cur =~ ^%|%.*% ]]
108 then
109 # correct range syntax is start%end
110 return 1
111 fi
112 local range_start=$(expr "$cur" : '\(.*%\)')
113 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset | sed 's$.*@$'$range_start'$g'
114 fi
115 else
116 __zfs_match_explicit_snapshot; __zfs_list_datasets
117 fi
118 }
119
120 __zfs_list_volumes()
121 {
122 $__ZFS_CMD list -H -o name -t volume
123 }
124
125 __zfs_argument_chosen()
126 {
127 local word property
128 for word in $(seq $((COMP_CWORD-1)) -1 2)
129 do
130 local prev="${COMP_WORDS[$word]}"
131 if [[ ${COMP_WORDS[$word-1]} != -[tos] ]]
132 then
133 if [[ "$prev" == [^,]*,* ]] || [[ "$prev" == *[@:]* ]]
134 then
135 return 0
136 fi
137 for property in $@
138 do
139 if [[ $prev == "$property" ]]
140 then
141 return 0
142 fi
143 done
144 fi
145 done
146 return 1
147 }
148
149 __zfs_complete_ordered_arguments()
150 {
151 local list1=$1
152 local list2=$2
153 local cur=$3
154 local extra=$4
155 if __zfs_argument_chosen $list1
156 then
157 COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
158 else
159 COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
160 fi
161 }
162
163 __zfs_complete_multiple_options()
164 {
165 local options=$1
166 local cur=$2
167
168 COMPREPLY=($(compgen -W "$options" -- "${cur##*,}"))
169 local existing_opts=$(expr "$cur" : '\(.*,\)')
170 if [[ $existing_opts ]]
171 then
172 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
173 fi
174 }
175
176 __zfs_complete_switch()
177 {
178 local options=$1
179 if [[ ${cur:0:1} == - ]]
180 then
181 COMPREPLY=($(compgen -W "-{$options}" -- "$cur"))
182 return 0
183 else
184 return 1
185 fi
186 }
187
188 __zfs_complete()
189 {
190 local cur prev cmd cmds
191 COMPREPLY=()
192 # Don't split on colon
193 _get_comp_words_by_ref -n : -c cur -p prev -w COMP_WORDS -i COMP_CWORD
194 cmd="${COMP_WORDS[1]}"
195
196 if [[ ${prev##*/} == zfs ]]
197 then
198 cmds=$(__zfs_get_commands)
199 COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
200 return 0
201 fi
202
203 case "${cmd}" in
204 clone)
205 case "${prev}" in
206 -o)
207 COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
208 ;;
209 *)
210 if ! __zfs_complete_switch "o,p"
211 then
212 if __zfs_argument_chosen
213 then
214 COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
215 else
216 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
217 fi
218 fi
219 ;;
220 esac
221 ;;
222 get)
223 case "${prev}" in
224 -d)
225 COMPREPLY=($(compgen -W "" -- "$cur"))
226 ;;
227 -t)
228 __zfs_complete_multiple_options "filesystem volume snapshot all" "$cur"
229 ;;
230 -s)
231 __zfs_complete_multiple_options "local default inherited temporary none" "$cur"
232 ;;
233 -o)
234 __zfs_complete_multiple_options "name property value source received all" "$cur"
235 ;;
236 *)
237 if ! __zfs_complete_switch "H,r,p,d,o,t,s"
238 then
239 if __zfs_argument_chosen $(__zfs_get_properties)
240 then
241 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
242 else
243 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
244 fi
245 fi
246 ;;
247 esac
248 ;;
249 inherit)
250 if ! __zfs_complete_switch "r"
251 then
252 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" $cur
253 fi
254 ;;
255 list)
256 case "${prev}" in
257 -d)
258 COMPREPLY=($(compgen -W "" -- "$cur"))
259 ;;
260 -t)
261 __zfs_complete_multiple_options "filesystem volume snapshot all" "$cur"
262 ;;
263 -o)
264 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
265 ;;
266 -s|-S)
267 COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "$cur"))
268 ;;
269 *)
270 if ! __zfs_complete_switch "H,r,d,o,t,s,S"
271 then
272 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
273 fi
274 ;;
275 esac
276 ;;
277 promote)
278 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
279 ;;
280 rollback)
281 if ! __zfs_complete_switch "r,R,f"
282 then
283 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
284 fi
285 ;;
286 send)
287 if ! __zfs_complete_switch "d,n,P,p,R,v,i,I"
288 then
289 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
290 fi
291 ;;
292 snapshot)
293 case "${prev}" in
294 -o)
295 COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
296 ;;
297 *)
298 if ! __zfs_complete_switch "o,r"
299 then
300 COMPREPLY=($(compgen -W "$(__zfs_list_datasets | awk '{print $1"@"}')" -- "$cur"))
301 fi
302 ;;
303 esac
304 ;;
305 set)
306 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" $cur
307 ;;
308 upgrade)
309 case "${prev}" in
310 -a|-V|-v)
311 COMPREPLY=($(compgen -W "" -- "$cur"))
312 ;;
313 *)
314 if ! __zfs_complete_switch "a,V,v,r"
315 then
316 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
317 fi
318 ;;
319 esac
320 ;;
321 destroy)
322 if ! __zfs_complete_switch "d,f,n,p,R,r,v"
323 then
324 __zfs_complete_multiple_options "$(__zfs_match_multiple_snapshots)" $cur
325 fi
326 ;;
327 *)
328 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
329 ;;
330 esac
331 __ltrim_colon_completions "$cur"
332 return 0
333 }
334
335 __zpool_get_commands()
336 {
337 $__ZPOOL_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
338 }
339
340 __zpool_get_properties()
341 {
342 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
343 }
344
345 __zpool_get_editable_properties()
346 {
347 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
348 }
349
350 __zpool_list_pools()
351 {
352 $__ZPOOL_CMD list -H -o name
353 }
354
355 __zpool_complete()
356 {
357 local cur prev cmd cmds
358 COMPREPLY=()
359 cur="${COMP_WORDS[COMP_CWORD]}"
360 prev="${COMP_WORDS[COMP_CWORD-1]}"
361 cmd="${COMP_WORDS[1]}"
362
363 if [[ ${prev##*/} == zpool ]]
364 then
365 cmds=$(__zpool_get_commands)
366 COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
367 return 0
368 fi
369
370 case "${cmd}" in
371 get)
372 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
373 return 0
374 ;;
375 import)
376 if [[ $prev == -d ]]
377 then
378 _filedir -d
379 else
380 COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
381 fi
382 return 0
383 ;;
384 set)
385 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
386 return 0
387 ;;
388 add|attach|clear|create|detach|offline|online|remove|replace)
389 local pools="$(__zpool_list_pools)"
390 if __zfs_argument_chosen $pools
391 then
392 _filedir
393 else
394 COMPREPLY=($(compgen -W "$pools" -- "$cur"))
395 fi
396 return 0
397 ;;
398 *)
399 COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
400 return 0
401 ;;
402 esac
403
404 }
405
406 complete -F __zfs_complete zfs
407 complete -F __zpool_complete zpool