]> git.ipfire.org Git - people/stevee/network.git/blame - src/bash-completion/network
raw: add command zone-config-hid-is-valid
[people/stevee/network.git] / src / bash-completion / network
CommitLineData
bae37360
MT
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
1c6a4e30 23_network_find_on_cmdline () {
bae37360
MT
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
1c6a4e30 37_network_complete_hooks() {
bae37360
MT
38 local type="${1}"
39
40 COMPREPLY=( $(compgen -W "$(network raw list-hooks "${type}")" -- "${cur}") )
41}
42
1c6a4e30 43_network_complete_ports() {
bae37360
MT
44 COMPREPLY=( $(compgen -W "$(network raw list-ports)" -- "${cur}") )
45}
46
1c6a4e30 47_network_complete_zones() {
bae37360
MT
48 COMPREPLY=( $(compgen -W "$(network raw list-zones)" -- "${cur}") )
49}
50
b7fbb0cd
JS
51_network_color() {
52 local words=( $@ )
53
54 local commands="set reset"
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
9d3ac4e4
JS
62_network_description() {
63 local words=( $@ )
64
65 local commands="edit show"
66 local cmd="$(_network_find_on_cmdline "${commands}")"
67 if [[ -z "${cmd}" ]]; then
68 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
69 return 0
70 fi
71}
1c6a4e30 72_network_device() {
bae37360
MT
73 local words=( $@ )
74
75 local commands="list $(network raw list-devices)"
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 local args="${words[@]:1}"
83 case "${cmd}" in
84 list)
85 return 0
86 ;;
87 *)
88 _network_device_subcommand ${args}
89 ;;
90 esac
91}
92
1c6a4e30 93_network_device_subcommand() {
bae37360
MT
94 local words=( $@ )
95
f5ee091e 96 local commands="discover identify monitor status unlock ussd"
bae37360 97 local cmd="$(_network_find_on_cmdline "${commands}")"
bae37360 98 if [[ -z "${cmd}" ]]; then
8d1d2745 99 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
bae37360
MT
100 return 0
101 fi
b7fbb0cd 102
bae37360
MT
103 case "${cmd}" in
104 ussd)
105 # TODO
106 ;;
107 esac
108}
109
1c6a4e30 110_network_dhcpd() {
cc02f6be
MT
111 local proto="${1}"
112 shift
113
114 local words=( $@ )
115
116 local commands="edit reload restart show start stop subnet"
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 args="${words[@]:1}"
124 case "${cmd}" in
125 subnet)
126 _network_dhcpd_subnet "${proto}" ${args}
127 ;;
128 esac
129}
130
1c6a4e30 131_network_dhcpd_subnet() {
cc02f6be
MT
132 local proto="${1}"
133 shift
134
135 local words=( $@ )
136
137 local commands="new remove show $(network raw list-dhcpd-subnets "${proto}")"
138 local cmd="$(_network_find_on_cmdline "${commands}")"
139 if [[ -z "${cmd}" ]]; then
140 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
141 return 0
142 fi
143
144 local subnet="${words[0]}"
145 local args="${words[@]:1}"
146 case "${cmd}" in
147 new)
148 : # TODO
149 ;;
150 remove)
151 : # TODO
152 ;;
153 [0-9]*)
154 _network_dhcpd_subnet_subcommand "${proto}" "${subnet}" ${args}
155 ;;
156 esac
157}
158
1c6a4e30 159_network_dhcpd_subnet_subcommand() {
cc02f6be
MT
160 local proto="${1}"
161 local subnet="${2}"
162 shift 2
163
164 local words=( $@ )
165
166 local commands="edit options range show"
167 local cmd="$(_network_find_on_cmdline "${commands}")"
168 if [[ -z "${cmd}" ]]; then
169 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
170 return 0
171 fi
172
173 local args="${words[@]:1}"
174 case "${cmd}" in
175 edit)
176 : # TODO
177 ;;
178 options)
179 _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args}
180 ;;
181 range)
182 _network_dhcpd_subnet_subcommand_range "${proto}" "${subnet}" ${args}
183 ;;
184 esac
185}
186
1c6a4e30 187_network_dhcpd_subnet_subcommand_options() {
cc02f6be
MT
188 local proto="${1}"
189 local subnet="${2}"
190 shift 2
191
192 local options option
193 for option in $(network raw list-dhcpd-subnet-options "${proto}"); do
194 options="${options} ${option}="
195 done
196
197 COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
198}
199
1c6a4e30 200_network_dhcpd_subnet_subcommand_range() {
cc02f6be
MT
201 local proto="${1}"
202 local subnet="${2}"
203 shift 2
204
205 local words=( $@ )
206
207 local commands="new remove"
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 new)
216 COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") )
217 ;;
218 remove)
219 COMPREPLY=( $(compgen -W "$(network raw list-dhcpd-ranges-of-subnet "${proto}" "${subnet}")" \
220 -- "${cur}") )
221 ;;
222 esac
223}
224
1c6a4e30 225_network_dns_server() {
8d1d2745
MT
226 local words=( $@ )
227
228 local commands="add list remove update"
229 local cmd="$(_network_find_on_cmdline "${commands}")"
230 if [[ -z "${cmd}" ]]; then
231 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
232 return 0
233 fi
234
235 case "${cmd}" in
236 remove)
237 COMPREPLY=( $(compgen -W "$(network raw list-dns-servers)" \
238 -- "${cur}") )
239 ;;
240 esac
241}
242
1c6a4e30 243_network_port() {
bae37360
MT
244 local words=( $@ )
245
246 local commands="new destroy $(network raw list-ports)"
247 local cmd="$(_network_find_on_cmdline "${commands}")"
248 if [[ -z "${cmd}" ]]; then
249 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
250 return 0
251 fi
252
253 local args="${words[@]:1}"
254 case "${cmd}" in
255 new)
256 _network_complete_hooks "port"
257 ;;
258 destroy)
259 _network_complete_ports
260 ;;
261 *)
262 local args="${words[@]:1}"
263 _network_port_subcommand ${args}
264 ;;
265 esac
266}
267
1c6a4e30 268_network_port_subcommand() {
bae37360
MT
269 local words=( $@ )
270
9d3ac4e4 271 local commands="color create description down edit identify remove status up"
bae37360
MT
272 local cmd="$(_network_find_on_cmdline "${commands}")"
273 if [[ -z "${cmd}" ]]; then
274 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
275 return 0
276 fi
b7fbb0cd
JS
277
278 local args="${words[@]:1}"
279 case "${cmd}" in
280 color)
281 _network_color ${args}
282 ;;
9d3ac4e4
JS
283 description)
284 _network_description ${args}
285 ;;
b7fbb0cd
JS
286 esac
287
bae37360
MT
288}
289
1c6a4e30 290_network_route() {
8d1d2745
MT
291 local words=( $@ )
292
240367ba
JS
293 local commands="static"
294 local cmd="$(_network_find_on_cmdline "${commands}")"
295 if [[ -z "${cmd}" ]]; then
296 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
297 return 0
298 fi
299
300 case "${cmd}" in
301 static)
302 local args="${words[@]}"
303 _network_route_static ${args}
304 ;;
305 esac
306
307}
308
309_network_route_static() {
310 local words=( $@ )
311
86c225b8 312 local commands="add list remove reload"
8d1d2745
MT
313 local cmd="$(_network_find_on_cmdline "${commands}")"
314 if [[ -z "${cmd}" ]]; then
315 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
316 return 0
317 fi
318
319 case "${cmd}" in
320 add)
321 if [[ "${cur}" = -* ]]; then
322 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
323 --prohibit --unreachable" -- "${cur}") )
324 fi
325 ;;
326 list)
327 # TODO auto-complete options like --protocol here
328 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
329 ;;
330 esac
331}
332
1c6a4e30 333_network_settings() {
bae37360
MT
334 local words=( $@ )
335
336 local key keys
337 for key in $(network raw list-settings); do
338 keys="${keys} ${key}="
339 done
340 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
341}
342
1c6a4e30 343_network_zone() {
bae37360
MT
344 local words=( $@ )
345
346 local commands="new destroy $(network raw list-zones)"
347 local cmd="$(_network_find_on_cmdline "${commands}")"
348 if [[ -z "${cmd}" ]]; then
349 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
350 return 0
351 fi
352
1ca5fe9f 353
bae37360
MT
354 local args="${words[@]:1}"
355 case "${cmd}" in
356 new)
1ca5fe9f 357 _network_zone_new ${args}
bae37360
MT
358 ;;
359 destroy)
360 _network_complete_zones
361 ;;
362 *)
363 local zone="${cmd}"
364 local args="${words[@]:1}"
365 _network_zone_subcommand "${zone}" ${args}
366 ;;
367 esac
368}
369
1ca5fe9f
JS
370_network_zone_new() {
371 local words=( $@ )
372 local cmd=${words[@]:0:1}
373
374 # Suggest useful zone names
375 if [[ -z "${cmd}" ]]; then
376 local commands="$(network raw list-next-free-zones)"
377 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
378
379 # If a valid zone name was entered, we can move on
380 elif network raw zone-name-is-valid ${cmd}; then
381 local args="${words[@]:1}"
382 _network_complete_hooks zone ${args}
383 fi
384
385 return 0
386}
387
1c6a4e30 388_network_zone_subcommand() {
bae37360
MT
389 local zone="${1}"
390 shift
391
392 local words=( $@ )
393
9d3ac4e4 394 local commands="color config description disable down edit enable identify port rename status up"
bae37360
MT
395 local cmd="$(_network_find_on_cmdline "${commands}")"
396 if [[ -z "${cmd}" ]]; then
397 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
398 return 0
399 fi
b7fbb0cd 400
bae37360
MT
401 local args="${words[@]:1}"
402 case "${cmd}" in
403 config)
2a6b2397 404 _network_zone_subcommand_config "${zone}" ${args}
bae37360
MT
405 ;;
406 port)
407 _network_zone_subcommand_port "${zone}" ${args}
408 ;;
b7fbb0cd
JS
409 color)
410 _network_color ${args}
411 ;;
9d3ac4e4
JS
412 description)
413 _network_description ${args}
414 ;;
bae37360
MT
415 esac
416}
417
2a6b2397
MT
418_network_zone_subcommand_config() {
419 local zone="${1}"
420 shift
421
422 local words=( $@ )
423
cbf7473c
JS
424 local commands="destroy list new $(network raw list-zone-config-ids ${zone})"
425
2a6b2397
MT
426 local cmd="$(_network_find_on_cmdline "${commands}")"
427 if [[ -z "${cmd}" ]]; then
428 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
429 return 0
430 fi
431
cbf7473c 432 local args="${words[@]:1}"
2a6b2397
MT
433 case "${cmd}" in
434 new)
435 _network_complete_hooks "config"
436 ;;
cbf7473c
JS
437 destroy)
438 _network_zone_subcommand_config_destroy ${zone} ${args}
439 ;;
440 # We use no better globbing like [:digit:] here because hids would not match the glob
441 # Also bash is silly here and does not unterstand things like ^[[:digit:]]+$ here.
442 *)
443 # Check if we get a valid id
444 # TODO: We should also accept a valid hid
445 if network raw zone-config-id-is-valid ${zone} ${cmd}; then
446 _network_zone_subcommand_config_subcommand ${zone} ${args}
447 fi
448 ;;
2a6b2397
MT
449 esac
450}
451
cbf7473c
JS
452_network_zone_subcommand_config_subcommand() {
453 local zone="${1}"
454 shift
455
456 local words=( $@ )
457
458 local commands="edit"
459
460 local cmd="$(_network_find_on_cmdline "${commands}")"
461 if [[ -z "${cmd}" ]]; then
462 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
463 return 0
464 fi
465}
466
467_network_zone_subcommand_config_destroy() {
468 local zone="${1}"
469 shift
470
471 local words=( $@ )
472
473 local commands="$(network raw list-zone-config-ids ${zone})"
474
475 local cmd="$(_network_find_on_cmdline "${commands}")"
476 if [[ -z "${cmd}" ]]; then
477 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
478 return 0
479 fi
480}
481
1c6a4e30 482_network_zone_subcommand_port() {
bae37360
MT
483 local zone="${1}"
484 shift
485
486 local words=( $@ )
487
488 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
489 local cmd="$(_network_find_on_cmdline "${commands}")"
490 if [[ -z "${cmd}" ]]; then
491 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
492 return 0
493 fi
494
495 case "${cmd}" in
496 attach)
497 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
498 -- "${cur}") )
499 ;;
500 detach)
501 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
502 -- "${cur}") )
503 ;;
504 *)
505 local port="${cmd}"
506 local args="${words[@]:1}"
507 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
508 ;;
509 esac
510}
511
1c6a4e30 512_network_zone_subcommand_port_subcommand() {
bae37360
MT
513 local zone="${1}"
514 local port="${2}"
515 shift 2
516
517 local words=( $@ )
518
519 local commands="edit"
520 local cmd="$(_network_find_on_cmdline "${commands}")"
521 if [[ -z "${cmd}" ]]; then
522 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
523 return 0
524 fi
525
526 case "${cmd}" in
527 edit)
528 # TODO auto-complete the zone-port hook
529 ;;
530 esac
531}
532
1c6a4e30 533_network() {
bae37360
MT
534 local cur prev words cword
535 _init_completion || return
536
537 local cmd i
538 for (( i=1; i < ${#words[@]} - 1; i++ )); do
539 [[ "${words[i]}" = -* ]] && continue
540 cmd="${words[i]}" && break
541 done
542
543 if [[ -z "${cmd}" ]]; then
544 case "${cur}" in
545 -*)
546 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
547 ;;
548 *)
cc02f6be
MT
549 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
550 help hostname port reset route settings status zone" \
551 -- "${cur}") )
bae37360
MT
552 ;;
553 esac
554
555 return 0
556 fi
557
558 local args="${words[@]:i+1}"
559 case "${cmd}" in
560 device)
561 _network_device ${args}
562 ;;
cc02f6be
MT
563 dhcpv[64])
564 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
565 ;;
8d1d2745
MT
566 dns-server)
567 _network_dns_server ${args}
568 ;;
bae37360
MT
569 port)
570 _network_port ${args}
571 ;;
8d1d2745
MT
572 route)
573 _network_route ${args}
574 ;;
bae37360
MT
575 settings)
576 _network_settings ${args}
577 ;;
578 start|stop|status)
579 # start, stop and status optionally take a zone
580 _network_complete_zones
581 ;;
582 zone)
583 _network_zone ${args}
584 ;;
585 esac
586} && complete -F _network network