1 # Copyright (c) 2013, Aneurin Price <aneurin.price@gmail.com>
3 # Permission is hereby granted, free of charge, to any person
4 # obtaining a copy of this software and associated documentation
5 # files (the "Software"), to deal in the Software without
6 # restriction, including without limitation the rights to use,
7 # copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 # OTHER DEALINGS IN THE SOFTWARE.
24 if [[ -w /dev/zfs ]]; then
29 __ZPOOL_CMD="sudo zpool"
34 $__ZFS_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | cut -f1 -d '|' | uniq
37 __zfs_get_properties()
39 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all name space
42 __zfs_get_editable_properties()
44 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
47 __zfs_get_inheritable_properties()
49 $__ZFS_CMD get 2>&1 | awk '$3 == "YES" {print $1}'
54 $__ZFS_CMD list -H -o name -t filesystem,volume
57 __zfs_list_filesystems()
59 $__ZFS_CMD list -H -o name -t filesystem
62 __zfs_match_snapshot()
64 local base_dataset=${cur%@*}
65 if [[ $base_dataset != $cur ]]
67 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset
69 $__ZFS_CMD list -H -o name -t filesystem,volume | awk '{print $1"@"}'
73 __zfs_match_explicit_snapshot()
75 local base_dataset=${cur%@*}
76 if [[ $base_dataset != $cur ]]
78 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset
82 __zfs_match_multiple_snapshots()
84 local existing_opts=$(expr "$cur" : '\(.*\)[%,]')
85 if [[ $existing_opts ]]
87 local base_dataset=${cur%@*}
88 if [[ $base_dataset != $cur ]]
91 if [[ $cur =~ ^%|%.*% ]]
93 # correct range syntax is start%end
96 local range_start=$(expr "$cur" : '\(.*%\)')
97 $__ZFS_CMD list -H -o name -t snapshot -d 1 $base_dataset | sed 's$.*@$'$range_start'$g'
100 __zfs_match_explicit_snapshot; __zfs_list_datasets
106 $__ZFS_CMD list -H -o name -t volume
109 __zfs_argument_chosen()
112 for word in $(seq $((COMP_CWORD-1)) -1 2)
114 local prev="${COMP_WORDS[$word]}"
115 if [[ ${COMP_WORDS[$word-1]} != -[tos] ]]
117 if [[ "$prev" == [^,]*,* ]] || [[ "$prev" == *[@:]* ]]
123 if [[ $prev == "$property" ]]
133 __zfs_complete_ordered_arguments()
139 if __zfs_argument_chosen $list1
141 COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
143 COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
147 __zfs_complete_multiple_options()
152 COMPREPLY=($(compgen -W "$options" -- "${cur##*,}"))
153 local existing_opts=$(expr "$cur" : '\(.*,\)')
154 if [[ $existing_opts ]]
156 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
160 __zfs_complete_switch()
163 if [[ ${cur:0:1} == - ]]
165 COMPREPLY=($(compgen -W "-{$options}" -- "$cur"))
174 local cur prev cmd cmds
176 # Don't split on colon
177 _get_comp_words_by_ref -n : -c cur -p prev -w COMP_WORDS -i COMP_CWORD
178 cmd="${COMP_WORDS[1]}"
180 if [[ ${prev##*/} == zfs ]]
182 cmds=$(__zfs_get_commands)
183 COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
191 COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
194 if ! __zfs_complete_switch "o,p"
196 if __zfs_argument_chosen
198 COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
200 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
209 COMPREPLY=($(compgen -W "" -- "$cur"))
212 __zfs_complete_multiple_options "filesystem volume snapshot all" "$cur"
215 __zfs_complete_multiple_options "local default inherited temporary none" "$cur"
218 __zfs_complete_multiple_options "name property value source received all" "$cur"
221 if ! __zfs_complete_switch "H,r,p,d,o,t,s"
223 if __zfs_argument_chosen $(__zfs_get_properties)
225 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
227 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
234 if ! __zfs_complete_switch "r"
236 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" $cur
242 COMPREPLY=($(compgen -W "" -- "$cur"))
245 __zfs_complete_multiple_options "filesystem volume snapshot all" "$cur"
248 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
251 COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "$cur"))
254 if ! __zfs_complete_switch "H,r,d,o,t,s,S"
256 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
262 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
265 if ! __zfs_complete_switch "r,R,f"
267 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
271 if ! __zfs_complete_switch "d,n,P,p,R,v,i,I"
273 COMPREPLY=($(compgen -W "$(__zfs_match_snapshot)" -- "$cur"))
279 COMPREPLY=($(compgen -W "$(__zfs_get_editable_properties)" -- "$cur"))
282 if ! __zfs_complete_switch "o,r"
284 COMPREPLY=($(compgen -W "$(__zfs_list_datasets | awk '{print $1"@"}')" -- "$cur"))
290 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" $cur
295 COMPREPLY=($(compgen -W "" -- "$cur"))
298 if ! __zfs_complete_switch "a,V,v,r"
300 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
306 if ! __zfs_complete_switch "d,f,n,p,R,r,v"
308 __zfs_complete_multiple_options "$(__zfs_match_multiple_snapshots)" $cur
312 COMPREPLY=($(compgen -W "$(__zfs_match_explicit_snapshot) $(__zfs_list_datasets)" -- "$cur"))
315 __ltrim_colon_completions "$cur"
319 __zpool_get_commands()
321 $__ZPOOL_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
324 __zpool_get_properties()
326 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
329 __zpool_get_editable_properties()
331 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
336 $__ZPOOL_CMD list -H -o name
341 local cur prev cmd cmds
343 cur="${COMP_WORDS[COMP_CWORD]}"
344 prev="${COMP_WORDS[COMP_CWORD-1]}"
345 cmd="${COMP_WORDS[1]}"
347 if [[ ${prev##*/} == zpool ]]
349 cmds=$(__zpool_get_commands)
350 COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
356 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
364 COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
369 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
372 add|attach|clear|create|detach|offline|online|remove|replace)
373 local pools="$(__zpool_list_pools)"
374 if __zfs_argument_chosen $pools
378 COMPREPLY=($(compgen -W "$pools" -- "$cur"))
383 COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
390 complete -F __zfs_complete zfs
391 complete -F __zpool_complete zpool