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
Date: Sun, 01 Jul 2012 12:30:24
Message-Id: 20120701122944.655462004C@flycatcher.gentoo.org
1 ryao 12/07/01 12:29:44
2
3 Added: bash-completion
4 Log:
5 Import bash-completion from zfs-fuse
6
7 (Portage version: 2.1.10.65/cvs/Linux x86_64)
8
9 Revision Changes Path
10 1.1 sys-fs/zfs/files/bash-completion
11
12 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-fs/zfs/files/bash-completion?rev=1.1&view=markup
13 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-fs/zfs/files/bash-completion?rev=1.1&content-type=text/plain
14
15 Index: bash-completion
16 ===================================================================
17 # Copyright (c) 2010, 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 __zfs_get_commands()
41 {
42 zfs 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
43 }
44
45 __zfs_get_properties()
46 {
47 zfs get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
48 }
49
50 __zfs_get_editable_properties()
51 {
52 zfs get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
53 }
54
55 __zfs_get_inheritable_properties()
56 {
57 zfs get 2>&1 | awk '$3 == "YES" {print $1}'
58 }
59
60 __zfs_list_datasets()
61 {
62 zfs list -H -o name
63 }
64
65 __zfs_list_filesystems()
66 {
67 zfs list -H -o name -t filesystem
68 }
69
70 __zfs_list_snapshots()
71 {
72 zfs list -H -o name -t snapshot
73 }
74
75 __zfs_list_volumes()
76 {
77 zfs list -H -o name -t volume
78 }
79
80 __zfs_argument_chosen()
81 {
82 for word in $(seq $((COMP_CWORD-1)) -1 2)
83 do
84 local prev="${COMP_WORDS[$word]}"
85 for property in $@
86 do
87 if [ "x$prev" = "x$property" ]
88 then
89 return 0
90 fi
91 done
92 done
93 return 1
94 }
95
96 __zfs_complete_ordered_arguments()
97 {
98 local list1=$1
99 local list2=$2
100 local cur=$3
101 local extra=$4
102 if __zfs_argument_chosen $list1
103 then
104 COMPREPLY=($(compgen -W "$list2 $extra" -- "$cur"))
105 else
106 COMPREPLY=($(compgen -W "$list1 $extra" -- "$cur"))
107 fi
108 }
109
110 __zfs_complete()
111 {
112 local cur prev cmd cmds
113 COMPREPLY=()
114 cur="${COMP_WORDS[COMP_CWORD]}"
115 prev="${COMP_WORDS[COMP_CWORD-1]}"
116 cmd="${COMP_WORDS[1]}"
117 cmds=$(__zfs_get_commands)
118
119 if [ "${prev##*/}" = "zfs" ]
120 then
121 COMPREPLY=($(compgen -W "$cmds -?" -- "$cur"))
122 return 0
123 fi
124
125 case "${cmd}" in
126 clone)
127 __zfs_complete_ordered_arguments "$(__zfs_list_snapshots)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
128 return 0
129 ;;
130 get)
131 __zfs_complete_ordered_arguments "$(__zfs_get_properties)" "$(__zfs_list_datasets)" "$cur" "-H -r -p"
132 return 0
133 ;;
134 inherit)
135 __zfs_complete_ordered_arguments "$(__zfs_get_inheritable_properties)" "$(__zfs_list_datasets)" $cur
136 return 0
137 ;;
138 list)
139 if [ "x$prev" = "x-o" ]
140 then
141 COMPREPLY=($(compgen -W "$(__zfs_get_properties)" -- "${cur##*,}"))
142 local existing_opts=$(expr "$cur" : '\(.*,\)')
143 if [ ! "x$existing_opts" = "x" ]
144 then
145 COMPREPLY=( "${COMPREPLY[@]/#/${existing_opts}}" )
146 fi
147 else
148 COMPREPLY=($(compgen -W "$(__zfs_list_datasets) -H -r -o" -- "$cur"))
149 fi
150 return 0
151 ;;
152 promote)
153 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems)" -- "$cur"))
154 return 0
155 ;;
156 rollback|send)
157 COMPREPLY=($(compgen -W "$(__zfs_list_snapshots)" -- "$cur"))
158 return 0
159 ;;
160 snapshot)
161 COMPREPLY=($(compgen -W "$(__zfs_list_filesystems) $(__zfs_list_volumes)" -- "$cur"))
162 return 0
163 ;;
164 set)
165 __zfs_complete_ordered_arguments "$(__zfs_get_editable_properties)" "$(__zfs_list_filesystems) $(__zfs_list_volumes)" $cur
166 return 0
167 ;;
168 *)
169 COMPREPLY=($(compgen -W "$(__zfs_list_datasets)" -- "$cur"))
170 return 0
171 ;;
172 esac
173
174 }
175
176 __zpool_get_commands()
177 {
178 zpool 2>&1 | awk '/^\t[a-z]/ {print $1}' | uniq
179 }
180
181 __zpool_get_properties()
182 {
183 zpool get 2>&1 | awk '$2 == "YES" || $2 == "NO" {print $1}'; echo all
184 }
185
186 __zpool_get_editable_properties()
187 {
188 zpool get 2>&1 | awk '$2 == "YES" {printf("%s=\n", $1)}'
189 }
190
191 __zpool_list_pools()
192 {
193 zpool list -H -o name
194 }
195
196 __zpool_complete()
197 {
198 local cur prev cmd cmds
199 COMPREPLY=()
200 cur="${COMP_WORDS[COMP_CWORD]}"
201 prev="${COMP_WORDS[COMP_CWORD-1]}"
202 cmd="${COMP_WORDS[1]}"
203 cmds=$(__zpool_get_commands)
204
205 if [ "${prev##*/}" = "zpool" ]
206 then
207 COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
208 return 0
209 fi
210
211 case "${cmd}" in
212 get)
213 __zfs_complete_ordered_arguments "$(__zpool_get_properties)" "$(__zpool_list_pools)" $cur
214 return 0
215 ;;
216 import)
217 if [ "x$prev" = "x-d" ]
218 then
219 _filedir -d
220 else
221 COMPREPLY=($(compgen -W "$(__zpool_list_pools) -d" -- "$cur"))
222 fi
223 return 0
224 ;;
225 set)
226 __zfs_complete_ordered_arguments "$(__zpool_get_editable_properties)" "$(__zpool_list_pools)" $cur
227 return 0
228 ;;
229 add|attach|clear|create|detach|offline|online|remove|replace)
230 local pools="$(__zpool_list_pools)"
231 if __zfs_argument_chosen $pools
232 then
233 _filedir
234 else
235 COMPREPLY=($(compgen -W "$pools" -- "$cur"))
236 fi
237 return 0
238 ;;
239 *)
240 COMPREPLY=($(compgen -W "$(__zpool_list_pools)" -- "$cur"))
241 return 0
242 ;;
243 esac
244
245 }
246
247 complete -F __zfs_complete zfs
248 complete -o filenames -F __zpool_complete zpool