1 # Copyright (c) 2010, 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.
26 zfs 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
29 __zfs_get_properties()
31 zfs get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
34 __zfs_get_editable_properties()
36 zfs get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
39 __zfs_get_inheritable_properties()
41 zfs get 2>&1 | awk '$3 == "YES" {print $1}'
49 __zfs_list_filesystems()
51 zfs list -H -o name -t filesystem
54 __zfs_list_snapshots()
56 zfs list -H -o name -t snapshot
61 zfs list -H -o name -t volume
64 __zfs_argument_chosen()
66 for word in $(seq $((COMP_CWORD-1)) -1 2)
68 local prev="${COMP_WORDS[$word]}"
71 if [ "x$prev" = "x$property" ]
80 __zfs_complete_ordered_arguments()
86 if __zfs_argument_chosen $list1
88 COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
90 COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
96 local cur prev cmd cmds
98 cur="${COMP_WORDS[COMP_CWORD]}"
99 prev="${COMP_WORDS[COMP_CWORD-1]}"
100 cmd="${COMP_WORDS[1]}"
101 cmds=$(__zfs_get_commands)
103 if [ "${prev##*/}" = "zfs" ]
105 COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
111 __zfs_complete_ordered_arguments "$(__zfs_list_snapshots)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
115 __zfs_complete_ordered_arguments "$(__zfs_get_properties)" "$(__zfs_list_datasets)" "$cur" "-H -r -p"
119 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_list_datasets)" $cur
123 if [ "x$prev" = "x-o" ]
125 COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "${cur##*,}"))
126 local existing_opts=$(expr "$cur" : '\(.*,\)')
127 if [ ! "x$existing_opts" = "x" ]
129 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
132 COMPREPLY=($(compgen -W "$(__zfs_list_datasets) -H -r -o" -- "$cur"))
137 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
141 COMPREPLY=($(compgen -W "$(__zfs_list_snapshots)" -- "$cur"))
145 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems) $(__zfs_list_volumes)" -- "$cur"))
149 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
153 COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
160 __zpool_get_commands()
162 zpool 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
165 __zpool_get_properties()
167 zpool get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
170 __zpool_get_editable_properties()
172 zpool get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
177 zpool list -H -o name
182 local cur prev cmd cmds
184 cur="${COMP_WORDS[COMP_CWORD]}"
185 prev="${COMP_WORDS[COMP_CWORD-1]}"
186 cmd="${COMP_WORDS[1]}"
187 cmds=$(__zpool_get_commands)
189 if [ "${prev##*/}" = "zpool" ]
191 COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
197 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
201 if [ "x$prev" = "x-d" ]
205 COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
210 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
213 add|attach|clear|create|detach|offline|online|remove|replace)
214 local pools="$(__zpool_list_pools)"
215 if __zfs_argument_chosen $pools
219 COMPREPLY=($(compgen -W "$pools" -- "$cur"))
224 COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
231 complete -F __zfs_complete zfs
232 complete -o filenames -F __zpool_complete zpool