]> git.ipfire.org Git - people/ms/network.git/blob - src/bash-completion/network
4b5e34d30bea82125c946ab9c7592e3138cca071
[people/ms/network.git] / src / bash-completion / network
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 Michael Tremer #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21 # network(8) completion
22
23 _network_find_on_cmdline () {
24 local word subcommand c=0
25 while [ ${c} -lt ${cword} ]; do
26 word="${words[c]}"
27 for subcommand in ${1}; do
28 if [ "${subcommand}" = "${word}" ]; then
29 echo "${subcommand}"
30 return
31 fi
32 done
33 ((c++))
34 done
35 }
36
37 _network_complete_hooks() {
38 local type="${1}"
39
40 COMPREPLY=( $(compgen -W "$(network raw list-hooks "${type}")" -- "${cur}") )
41 }
42
43 _network_complete_ports() {
44 COMPREPLY=( $(compgen -W "$(network raw list-ports)" -- "${cur}") )
45 }
46
47 _network_complete_zones() {
48 COMPREPLY=( $(compgen -W "$(network raw list-zones)" -- "${cur}") )
49 }
50
51 _network_device() {
52 local words=( $@ )
53
54 local commands="list $(network raw list-devices)"
55 local cmd="$(_network_find_on_cmdline "${commands}")"
56 if [[ -z "${cmd}" ]]; then
57 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
58 return 0
59 fi
60
61 local args="${words[@]:1}"
62 case "${cmd}" in
63 list)
64 return 0
65 ;;
66 *)
67 _network_device_subcommand ${args}
68 ;;
69 esac
70 }
71
72 _network_device_subcommand() {
73 local words=( $@ )
74
75 local commands="discover identify monitor status unlock ussd"
76 local cmd="$(_network_find_on_cmdline "${commands}")"
77 if [[ -z "${cmd}" ]]; then
78 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
79 return 0
80 fi
81
82 case "${cmd}" in
83 ussd)
84 # TODO
85 ;;
86 esac
87 }
88
89 _network_dhcpd() {
90 local proto="${1}"
91 shift
92
93 local words=( $@ )
94
95 local commands="edit reload restart show start stop subnet"
96 local cmd="$(_network_find_on_cmdline "${commands}")"
97 if [[ -z "${cmd}" ]]; then
98 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
99 return 0
100 fi
101
102 local args="${words[@]:1}"
103 case "${cmd}" in
104 subnet)
105 _network_dhcpd_subnet "${proto}" ${args}
106 ;;
107 esac
108 }
109
110 _network_dhcpd_subnet() {
111 local proto="${1}"
112 shift
113
114 local words=( $@ )
115
116 local commands="new remove show $(network raw list-dhcpd-subnets "${proto}")"
117 local cmd="$(_network_find_on_cmdline "${commands}")"
118 if [[ -z "${cmd}" ]]; then
119 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
120 return 0
121 fi
122
123 local subnet="${words[0]}"
124 local args="${words[@]:1}"
125 case "${cmd}" in
126 new)
127 : # TODO
128 ;;
129 remove)
130 : # TODO
131 ;;
132 [0-9]*)
133 _network_dhcpd_subnet_subcommand "${proto}" "${subnet}" ${args}
134 ;;
135 esac
136 }
137
138 _network_dhcpd_subnet_subcommand() {
139 local proto="${1}"
140 local subnet="${2}"
141 shift 2
142
143 local words=( $@ )
144
145 local commands="edit options range show"
146 local cmd="$(_network_find_on_cmdline "${commands}")"
147 if [[ -z "${cmd}" ]]; then
148 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
149 return 0
150 fi
151
152 local args="${words[@]:1}"
153 case "${cmd}" in
154 edit)
155 : # TODO
156 ;;
157 options)
158 _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args}
159 ;;
160 range)
161 _network_dhcpd_subnet_subcommand_range "${proto}" "${subnet}" ${args}
162 ;;
163 esac
164 }
165
166 _network_dhcpd_subnet_subcommand_options() {
167 local proto="${1}"
168 local subnet="${2}"
169 shift 2
170
171 local options option
172 for option in $(network raw list-dhcpd-subnet-options "${proto}"); do
173 options="${options} ${option}="
174 done
175
176 COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
177 }
178
179 _network_dhcpd_subnet_subcommand_range() {
180 local proto="${1}"
181 local subnet="${2}"
182 shift 2
183
184 local words=( $@ )
185
186 local commands="new remove"
187 local cmd="$(_network_find_on_cmdline "${commands}")"
188 if [[ -z "${cmd}" ]]; then
189 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
190 return 0
191 fi
192
193 case "${cmd}" in
194 new)
195 COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") )
196 ;;
197 remove)
198 COMPREPLY=( $(compgen -W "$(network raw list-dhcpd-ranges-of-subnet "${proto}" "${subnet}")" \
199 -- "${cur}") )
200 ;;
201 esac
202 }
203
204 _network_dns_server() {
205 local words=( $@ )
206
207 local commands="add list remove update"
208 local cmd="$(_network_find_on_cmdline "${commands}")"
209 if [[ -z "${cmd}" ]]; then
210 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
211 return 0
212 fi
213
214 case "${cmd}" in
215 remove)
216 COMPREPLY=( $(compgen -W "$(network raw list-dns-servers)" \
217 -- "${cur}") )
218 ;;
219 esac
220 }
221
222 _network_port() {
223 local words=( $@ )
224
225 local commands="new destroy $(network raw list-ports)"
226 local cmd="$(_network_find_on_cmdline "${commands}")"
227 if [[ -z "${cmd}" ]]; then
228 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
229 return 0
230 fi
231
232 local args="${words[@]:1}"
233 case "${cmd}" in
234 new)
235 _network_complete_hooks "port"
236 ;;
237 destroy)
238 _network_complete_ports
239 ;;
240 *)
241 local args="${words[@]:1}"
242 _network_port_subcommand ${args}
243 ;;
244 esac
245 }
246
247 _network_port_subcommand() {
248 local words=( $@ )
249
250 local commands="create down edit identify remove status up"
251 local cmd="$(_network_find_on_cmdline "${commands}")"
252 if [[ -z "${cmd}" ]]; then
253 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
254 return 0
255 fi
256 }
257
258 _network_route() {
259 local words=( $@ )
260
261 local commands="static"
262 local cmd="$(_network_find_on_cmdline "${commands}")"
263 if [[ -z "${cmd}" ]]; then
264 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
265 return 0
266 fi
267
268 case "${cmd}" in
269 static)
270 local args="${words[@]}"
271 _network_route_static ${args}
272 ;;
273 esac
274
275 }
276
277 _network_route_static() {
278 local words=( $@ )
279
280 local commands="add list remove reload"
281 local cmd="$(_network_find_on_cmdline "${commands}")"
282 if [[ -z "${cmd}" ]]; then
283 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
284 return 0
285 fi
286
287 case "${cmd}" in
288 add)
289 if [[ "${cur}" = -* ]]; then
290 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
291 --prohibit --unreachable" -- "${cur}") )
292 fi
293 ;;
294 list)
295 # TODO auto-complete options like --protocol here
296 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
297 ;;
298 esac
299 }
300
301 _network_settings() {
302 local words=( $@ )
303
304 local key keys
305 for key in $(network raw list-settings); do
306 keys="${keys} ${key}="
307 done
308 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
309 }
310
311 _network_zone() {
312 local words=( $@ )
313
314 local commands="new destroy $(network raw list-zones)"
315 local cmd="$(_network_find_on_cmdline "${commands}")"
316 if [[ -z "${cmd}" ]]; then
317 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
318 return 0
319 fi
320
321
322 local args="${words[@]:1}"
323 case "${cmd}" in
324 new)
325 _network_zone_new ${args}
326 ;;
327 destroy)
328 _network_complete_zones
329 ;;
330 *)
331 local zone="${cmd}"
332 local args="${words[@]:1}"
333 _network_zone_subcommand "${zone}" ${args}
334 ;;
335 esac
336 }
337
338 _network_zone_new() {
339 local words=( $@ )
340 local cmd=${words[@]:0:1}
341
342 # Suggest useful zone names
343 if [[ -z "${cmd}" ]]; then
344 local commands="$(network raw list-next-free-zones)"
345 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
346
347 # If a valid zone name was entered, we can move on
348 elif network raw zone-name-is-valid ${cmd}; then
349 local args="${words[@]:1}"
350 _network_complete_hooks zone ${args}
351 fi
352
353 return 0
354 }
355
356 _network_zone_subcommand() {
357 local zone="${1}"
358 shift
359
360 local words=( $@ )
361
362 local commands="config disable down edit enable identify port rename status up"
363 local cmd="$(_network_find_on_cmdline "${commands}")"
364 if [[ -z "${cmd}" ]]; then
365 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
366 return 0
367 fi
368
369 local args="${words[@]:1}"
370 case "${cmd}" in
371 config)
372 _network_zone_subcommand_config "${zone}" ${args}
373 ;;
374 port)
375 _network_zone_subcommand_port "${zone}" ${args}
376 ;;
377 esac
378 }
379
380 _network_zone_subcommand_config() {
381 local zone="${1}"
382 shift
383
384 local words=( $@ )
385
386 local commands="new destroy"
387 local cmd="$(_network_find_on_cmdline "${commands}")"
388 if [[ -z "${cmd}" ]]; then
389 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
390 return 0
391 fi
392
393 case "${cmd}" in
394 new)
395 _network_complete_hooks "config"
396 ;;
397 esac
398 }
399
400 _network_zone_subcommand_port() {
401 local zone="${1}"
402 shift
403
404 local words=( $@ )
405
406 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
407 local cmd="$(_network_find_on_cmdline "${commands}")"
408 if [[ -z "${cmd}" ]]; then
409 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
410 return 0
411 fi
412
413 case "${cmd}" in
414 attach)
415 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
416 -- "${cur}") )
417 ;;
418 detach)
419 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
420 -- "${cur}") )
421 ;;
422 *)
423 local port="${cmd}"
424 local args="${words[@]:1}"
425 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
426 ;;
427 esac
428 }
429
430 _network_zone_subcommand_port_subcommand() {
431 local zone="${1}"
432 local port="${2}"
433 shift 2
434
435 local words=( $@ )
436
437 local commands="edit"
438 local cmd="$(_network_find_on_cmdline "${commands}")"
439 if [[ -z "${cmd}" ]]; then
440 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
441 return 0
442 fi
443
444 case "${cmd}" in
445 edit)
446 # TODO auto-complete the zone-port hook
447 ;;
448 esac
449 }
450
451 _network() {
452 local cur prev words cword
453 _init_completion || return
454
455 local cmd i
456 for (( i=1; i < ${#words[@]} - 1; i++ )); do
457 [[ "${words[i]}" = -* ]] && continue
458 cmd="${words[i]}" && break
459 done
460
461 if [[ -z "${cmd}" ]]; then
462 case "${cur}" in
463 -*)
464 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
465 ;;
466 *)
467 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
468 help hostname port reset route settings status zone" \
469 -- "${cur}") )
470 ;;
471 esac
472
473 return 0
474 fi
475
476 local args="${words[@]:i+1}"
477 case "${cmd}" in
478 device)
479 _network_device ${args}
480 ;;
481 dhcpv[64])
482 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
483 ;;
484 dns-server)
485 _network_dns_server ${args}
486 ;;
487 port)
488 _network_port ${args}
489 ;;
490 route)
491 _network_route ${args}
492 ;;
493 settings)
494 _network_settings ${args}
495 ;;
496 start|stop|status)
497 # start, stop and status optionally take a zone
498 _network_complete_zones
499 ;;
500 zone)
501 _network_zone ${args}
502 ;;
503 esac
504 } && complete -F _network network