1 # Copyright (c) 2010-2016, 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 __ZFS_CMD="@sbindir@/zfs"
25 __ZPOOL_CMD="@sbindir@/zpool"
27 # Disable bash's built-in hostname completion, as this makes it impossible to
28 # provide completions containing an @-sign, which is necessary for completing
29 # snapshot names. If bash_completion is in use, this will already be disabled
30 # and replaced with better completions anyway.
35 $__ZFS_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | cut -f1 -d '|' | uniq
38 __zfs_get_properties()
40 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all name space
43 __zfs_get_editable_properties()
45 $__ZFS_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
48 __zfs_get_inheritable_properties()
50 $__ZFS_CMD get 2>&1 | awk '$3 == "YES" {print $1}'
55 $__ZFS_CMD list -H -o name -s name -t filesystem,volume "$@"
58 __zfs_list_filesystems()
60 $__ZFS_CMD list -H -o name -s name -t filesystem
63 __zfs_match_snapshot()
65 local base_dataset="${cur%@*}"
66 if [[ "$base_dataset" != "$cur" ]]
68 $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 "$base_dataset"
70 if [[ "$cur" != "" ]] && __zfs_list_datasets "$cur" &> /dev/null
72 $__ZFS_CMD list -H -o name -s name -t filesystem,volume -r "$cur" | tail -n +2
73 # We output the base dataset name even though we might be
74 # completing a command that can only take a snapshot, because it
75 # prevents bash from considering the completion finished when it
81 datasets="$(__zfs_list_datasets)"
86 # If the current command ends with a slash, then the only way
87 # it can be completed with a single tab press (ie. in this pass)
88 # is if it has exactly one child, so that's the only time we
89 # need to offer a suggestion with an @ appended.
91 # This is actually off by one as zfs list includes the named
92 # dataset in addition to its children
93 num_children=$(__zfs_list_datasets -d 1 "${cur%/}" 2> /dev/null | wc -l)
94 if [[ $num_children != 2 ]]
99 echo "$datasets" | awk '{print $1 "@"}'
104 __zfs_match_snapshot_or_bookmark()
106 local base_dataset="${cur%[#@]*}"
107 if [[ "$base_dataset" != "$cur" ]]
111 $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 "$base_dataset"
113 $__ZFS_CMD list -H -o name -s name -t bookmark -d 1 "$base_dataset"
116 $__ZFS_CMD list -H -o name -s name -t filesystem,volume
117 if [[ -e "$cur" ]] && $__ZFS_CMD list -H -o name -s name -t filesystem,volume "$cur" &> /dev/null
125 __zfs_match_multiple_snapshots()
128 existing_opts="$(expr "$cur" : '\(.*\)[%,]')"
129 if [[ -e "$existing_opts" ]]
131 local base_dataset="${cur%@*}"
132 if [[ "$base_dataset" != "$cur" ]]
134 local cur="${cur##*,}"
135 if [[ $cur =~ ^%|%.*% ]]
137 # correct range syntax is start%end
141 range_start="$(expr "$cur" : '\(.*%\)')"
142 # shellcheck disable=SC2016
143 $__ZFS_CMD list -H -o name -s name -t snapshot -d 1 "$base_dataset" | sed 's$.*@$'"$range_start"'$g'
146 __zfs_match_snapshot_or_bookmark
152 $__ZFS_CMD list -H -o name -s name -t volume
155 __zfs_argument_chosen()
158 for word in $(seq $((COMP_CWORD-1)) -1 2)
160 local prev="${COMP_WORDS[$word]}"
161 if [[ ${COMP_WORDS[$word-1]} != -[tos] ]]
163 if [[ "$prev" == [^,]*,* ]] || [[ "$prev" == *[@:\#]* ]]
169 if [[ $prev == "$property"* ]]
179 __zfs_complete_ordered_arguments()
185 # shellcheck disable=SC2086
186 if __zfs_argument_chosen $list1
188 mapfile -t COMPREPLY < <(compgen -W "$list2 $extra" -- "$cur")
190 mapfile -t COMPREPLY < <(compgen -W "$list1 $extra" -- "$cur")
194 __zfs_complete_multiple_options()
200 mapfile -t COMPREPLY < <(compgen -W "$options" -- "${cur##*,}")
201 existing_opts=$(expr "$cur" : '\(.*,\)')
202 if [[ -n "$existing_opts" ]]
204 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
208 __zfs_complete_switch()
211 if [[ ${cur:0:1} == - ]]
213 mapfile -t COMPREPLY < <(compgen -W "-{$options}" -- "$cur")
220 __zfs_complete_nospace()
222 # Google indicates that there may still be bash versions out there that
223 # don't have compopt.
224 if type compopt &> /dev/null
232 local cur prev cmd cmds
234 if type _get_comp_words_by_ref &> /dev/null
236 # Don't split on colon
237 _get_comp_words_by_ref -n : -c cur -p prev -w COMP_WORDS -i COMP_CWORD
239 cur="${COMP_WORDS[COMP_CWORD]}"
240 prev="${COMP_WORDS[COMP_CWORD-1]}"
242 cmd="${COMP_WORDS[1]}"
244 if [[ ${prev##*/} == zfs ]]
246 cmds=$(__zfs_get_commands)
247 mapfile -t COMPREPLY < <(compgen -W "$cmds -?" -- "$cur")
253 if __zfs_argument_chosen
255 mapfile -t COMPREPLY < <(compgen -W "${prev%@*}# ${prev/@/#}" -- "$cur")
257 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
263 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_get_editable_properties)" -- "$cur")
264 __zfs_complete_nospace
267 if ! __zfs_complete_switch "o,p"
269 if __zfs_argument_chosen
271 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_list_datasets)" -- "$cur")
273 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
282 mapfile -t COMPREPLY < <(compgen -W "" -- "$cur")
285 __zfs_complete_multiple_options "filesystem volume snapshot bookmark all" "$cur"
288 __zfs_complete_multiple_options "local default inherited temporary received none" "$cur"
291 __zfs_complete_multiple_options "name property value source received all" "$cur"
294 if ! __zfs_complete_switch "H,r,p,d,o,t,s"
296 # shellcheck disable=SC2046
297 if __zfs_argument_chosen $(__zfs_get_properties)
299 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
301 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
308 if ! __zfs_complete_switch "r"
310 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_match_snapshot)" "$cur"
316 mapfile -t COMPREPLY < <(compgen -W "" -- "$cur")
319 __zfs_complete_multiple_options "filesystem volume snapshot bookmark all" "$cur"
322 __zfs_complete_multiple_options "$(__zfs_get_properties)" "$cur"
325 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_get_properties)" -- "$cur")
328 if ! __zfs_complete_switch "H,r,d,o,t,s,S"
330 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
336 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_list_filesystems)" -- "$cur")
339 if ! __zfs_complete_switch "r,R,f"
341 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
345 if ! __zfs_complete_switch "D,n,P,p,R,v,e,L,i,I"
347 if __zfs_argument_chosen
349 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
351 if [[ $prev == -*i* ]]
353 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot_or_bookmark)" -- "$cur")
355 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
363 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_get_editable_properties)" -- "$cur")
364 __zfs_complete_nospace
367 if ! __zfs_complete_switch "o,r"
369 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
370 __zfs_complete_nospace
376 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_match_snapshot)" "$cur"
377 __zfs_complete_nospace
382 mapfile -t COMPREPLY < <(compgen -W "" -- "$cur")
385 if ! __zfs_complete_switch "a,V,v,r"
387 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_list_filesystems)" -- "$cur")
393 if ! __zfs_complete_switch "d,f,n,p,R,r,v"
395 __zfs_complete_multiple_options "$(__zfs_match_multiple_snapshots)" "$cur"
396 __zfs_complete_nospace
400 mapfile -t COMPREPLY < <(compgen -W "$(__zfs_match_snapshot)" -- "$cur")
403 if type __ltrim_colon_completions &> /dev/null
405 __ltrim_colon_completions "$cur"
410 __zpool_get_commands()
412 $__ZPOOL_CMD 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
415 __zpool_get_properties()
417 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
420 __zpool_get_editable_properties()
422 $__ZPOOL_CMD get 2>&1 | awk '$2 == "YES" {print $1"="}'
427 $__ZPOOL_CMD list -H -o name
432 local cur prev cmd cmds pools
434 cur="${COMP_WORDS[COMP_CWORD]}"
435 prev="${COMP_WORDS[COMP_CWORD-1]}"
436 cmd="${COMP_WORDS[1]}"
438 if [[ ${prev##*/} == zpool ]]
440 cmds=$(__zpool_get_commands)
441 mapfile -t COMPREPLY < <(compgen -W "$cmds" -- "$cur")
447 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" "$cur"
455 mapfile -t COMPREPLY < <(compgen -W "$(__zpool_list_pools) -d" -- "$cur")
460 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" "$cur"
461 __zfs_complete_nospace
464 add|attach|clear|create|detach|offline|online|remove|replace)
465 pools="$(__zpool_list_pools)"
466 # shellcheck disable=SC2086
467 if __zfs_argument_chosen $pools
471 mapfile -t COMPREPLY < <(compgen -W "$pools" -- "$cur")
476 mapfile -t COMPREPLY < <(compgen -W "$(__zpool_list_pools)" -- "$cur")
483 complete -F __zfs_complete zfs
484 complete -F __zpool_complete zpool