]> git.proxmox.com Git - mirror_zfs-debian.git/blob - debian/tree/zfsutils/etc/bash_completion.d/zfs
Add bash command completion to zfsutils.
[mirror_zfs-debian.git] / debian / tree / zfsutils / etc / bash_completion.d / zfs
1 # Copyright (c) 2010, Aneurin Price <aneurin.price@gmail.com>
2
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
10 # conditions:
11
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
14
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.
23
24 __zfs_get_commands()
25 {
26 zfs 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
27 }
28
29 __zfs_get_properties()
30 {
31 zfs get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
32 }
33
34 __zfs_get_editable_properties()
35 {
36 zfs get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
37 }
38
39 __zfs_get_inheritable_properties()
40 {
41 zfs get 2>&1 | awk '$3 == "YES" {print $1}'
42 }
43
44 __zfs_list_datasets()
45 {
46 zfs list -H -o name
47 }
48
49 __zfs_list_filesystems()
50 {
51 zfs list -H -o name -t filesystem
52 }
53
54 __zfs_list_snapshots()
55 {
56 zfs list -H -o name -t snapshot
57 }
58
59 __zfs_list_volumes()
60 {
61 zfs list -H -o name -t volume
62 }
63
64 __zfs_argument_chosen()
65 {
66 for word in $(seq $((COMP_CWORD-1)) -1 2)
67 do
68 local prev="${COMP_WORDS[$word]}"
69 for property in $@
70 do
71 if [ "x$prev" = "x$property" ]
72 then
73 return 0
74 fi
75 done
76 done
77 return 1
78 }
79
80 __zfs_complete_ordered_arguments()
81 {
82 local list1=$1
83 local list2=$2
84 local cur=$3
85 local extra=$4
86 if __zfs_argument_chosen $list1
87 then
88 COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
89 else
90 COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
91 fi
92 }
93
94 __zfs_complete()
95 {
96 local cur prev cmd cmds
97 COMPREPLY=()
98 cur="${COMP_WORDS[COMP_CWORD]}"
99 prev="${COMP_WORDS[COMP_CWORD-1]}"
100 cmd="${COMP_WORDS[1]}"
101 cmds=$(__zfs_get_commands)
102
103 if [ "${prev##*/}" = "zfs" ]
104 then
105 COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
106 return 0
107 fi
108
109 case "${cmd}" in
110 clone)
111 __zfs_complete_ordered_arguments "$(__zfs_list_snapshots)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
112 return 0
113 ;;
114 get)
115 __zfs_complete_ordered_arguments "$(__zfs_get_properties)" "$(__zfs_list_datasets)" "$cur" "-H -r -p"
116 return 0
117 ;;
118 inherit)
119 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_list_datasets)" $cur
120 return 0
121 ;;
122 list)
123 if [ "x$prev" = "x-o" ]
124 then
125 COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "${cur##*,}"))
126 local existing_opts=$(expr "$cur" : '\(.*,\)')
127 if [ ! "x$existing_opts" = "x" ]
128 then
129 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
130 fi
131 else
132 COMPREPLY=($(compgen -W "$(__zfs_list_datasets) -H -r -o" -- "$cur"))
133 fi
134 return 0
135 ;;
136 promote)
137 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
138 return 0
139 ;;
140 rollback|send)
141 COMPREPLY=($(compgen -W "$(__zfs_list_snapshots)" -- "$cur"))
142 return 0
143 ;;
144 snapshot)
145 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems) $(__zfs_list_volumes)" -- "$cur"))
146 return 0
147 ;;
148 set)
149 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
150 return 0
151 ;;
152 *)
153 COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
154 return 0
155 ;;
156 esac
157
158 }
159
160 __zpool_get_commands()
161 {
162 zpool 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
163 }
164
165 __zpool_get_properties()
166 {
167 zpool get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
168 }
169
170 __zpool_get_editable_properties()
171 {
172 zpool get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
173 }
174
175 __zpool_list_pools()
176 {
177 zpool list -H -o name
178 }
179
180 __zpool_complete()
181 {
182 local cur prev cmd cmds
183 COMPREPLY=()
184 cur="${COMP_WORDS[COMP_CWORD]}"
185 prev="${COMP_WORDS[COMP_CWORD-1]}"
186 cmd="${COMP_WORDS[1]}"
187 cmds=$(__zpool_get_commands)
188
189 if [ "${prev##*/}" = "zpool" ]
190 then
191 COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
192 return 0
193 fi
194
195 case "${cmd}" in
196 get)
197 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
198 return 0
199 ;;
200 import)
201 if [ "x$prev" = "x-d" ]
202 then
203 _filedir -d
204 else
205 COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
206 fi
207 return 0
208 ;;
209 set)
210 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
211 return 0
212 ;;
213 add|attach|clear|create|detach|offline|online|remove|replace)
214 local pools="$(__zpool_list_pools)"
215 if __zfs_argument_chosen $pools
216 then
217 _filedir
218 else
219 COMPREPLY=($(compgen -W "$pools" -- "$cur"))
220 fi
221 return 0
222 ;;
223 *)
224 COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
225 return 0
226 ;;
227 esac
228
229 }
230
231 complete -F __zfs_complete zfs
232 complete -o filenames -F __zpool_complete zpool