]> git.ipfire.org Git - people/stevee/network.git/blame - src/bash-completion/network
bash-autocompletion: fix typos
[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
93097eca
JS
343_network_vpn() {
344 local words=( $@ )
345
346 local commands="ipsec"
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
353
354 local args="${words[@]:1}"
355 case "${cmd}" in
356 ipsec)
357 _network_vpn_ipsec ${args}
358 ;;
359 esac
360}
361
362_network_vpn_ipsec() {
363 local words=( $@ )
364
365 local commands="connection"
366 local cmd="$(_network_find_on_cmdline "${commands}")"
367 if [[ -z "${cmd}" ]]; then
368 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
369 return 0
370 fi
371
372
373 local args="${words[@]:1}"
374 case "${cmd}" in
375 connection)
376 _network_vpn_ipsec_connection ${args}
377 ;;
378 esac
379}
380
381_network_vpn_ipsec_connection() {
382 local words=( $@ )
383
384 local commands="destroy new $(network raw list-ipsec-connections)"
385 local cmd="$(_network_find_on_cmdline "${commands}")"
386 if [[ -z "${cmd}" ]]; then
387 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
388 return 0
389 fi
390
391
392 local args="${words[@]:1}"
393 case "${cmd}" in
394 destroy)
395 :
396 ;;
397 new)
398 :
399 ;;
400 *)
401 if network raw ipsec-connection-exists ${cmd}; then
402 _network_vpn_ipsec_connection_subcommands ${cmd} ${args}
403 fi
404 ;;
405 esac
406}
407
408_network_vpn_ipsec_connection_subcommands() {
409 local connection=${1}
410 shift
411 local words=( $@ )
412
faf53925 413 local commands="authentication inactivity-timeout local mode peer remote security-policy show"
93097eca
JS
414 local cmd="$(_network_find_on_cmdline "${commands}")"
415 if [[ -z "${cmd}" ]]; then
416 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
417 return 0
418 fi
419
420
421 local args="${words[@]:1}"
422 case "${cmd}" in
423 authentication)
424 _network_vpn_ipsec_connection_subcommands_authentication ${connection} ${args}
425 ;;
426 local)
427 _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "local" ${args}
428 ;;
429 mode)
430 _network_vpn_ipsec_connection_subcommands_mode ${args}
431 ;;
432 remote)
faf53925 433 _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "remote" ${args}
93097eca
JS
434 ;;
435 security-policy)
436 _network_vpn_ipsec_connection_subcommands_security_policy ${args}
437 ;;
438 esac
439}
440
441_network_vpn_ipsec_connection_subcommands_authentication() {
442 local connection=${1}
443 shift
444 local words=( $@ )
445
446 local commands="psk mode"
447 local cmd="$(_network_find_on_cmdline "${commands}")"
448 if [[ -z "${cmd}" ]]; then
449 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
450 return 0
451 fi
452
453
454 local args="${words[@]:1}"
455 case "${cmd}" in
456 mode)
457 _network_vpn_ipsec_connection_subcommands_authentication_mode ${connection} ${args}
458 ;;
459 esac
460}
461
462_network_vpn_ipsec_connection_subcommands_local_remote() {
463 local connection=${1}
464 local type=${2}
465 shift 2
466 local words=( $@ )
467
468 local commands="id prefix"
469 local cmd="$(_network_find_on_cmdline "${commands}")"
470 if [[ -z "${cmd}" ]]; then
471 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
472 return 0
473 fi
474
475
476 local args="${words[@]:1}"
477 case "${cmd}" in
478 prefix)
479 _network_vpn_ipsec_connection_subcommands_prefix "${connection}" "${type}" ${args}
480 ;;
481 esac
482}
483
484_network_vpn_ipsec_connection_subcommands_prefix() {
485 :
486}
487
488_network_vpn_ipsec_connection_subcommands_authentication_mode() {
489 local connection=${1}
490 shift
491 local words=( $@ )
492
493 local commands="PSK"
494 local cmd="$(_network_find_on_cmdline "${commands}")"
495 if [[ -z "${cmd}" ]]; then
496 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
497 return 0
498 fi
499}
500
501_network_vpn_ipsec_connection_subcommands_mode() {
502 local words=( $@ )
503
504 local commands="gre-transport tunnel vti"
505 local cmd="$(_network_find_on_cmdline "${commands}")"
506 if [[ -z "${cmd}" ]]; then
507 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
508 return 0
509 fi
510}
511
512_network_vpn_ipsec_connection_subcommands_security_policy() {
513 local words=( $@ )
514
515 local commands="$(network raw list-vpn-security-policies-all)"
516 local cmd="$(_network_find_on_cmdline "${commands}")"
517 if [[ -z "${cmd}" ]]; then
518 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
519 return 0
520 fi
521}
522
1c6a4e30 523_network_zone() {
bae37360
MT
524 local words=( $@ )
525
526 local commands="new destroy $(network raw list-zones)"
527 local cmd="$(_network_find_on_cmdline "${commands}")"
528 if [[ -z "${cmd}" ]]; then
529 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
530 return 0
531 fi
532
1ca5fe9f 533
bae37360
MT
534 local args="${words[@]:1}"
535 case "${cmd}" in
536 new)
1ca5fe9f 537 _network_zone_new ${args}
bae37360
MT
538 ;;
539 destroy)
540 _network_complete_zones
541 ;;
542 *)
543 local zone="${cmd}"
544 local args="${words[@]:1}"
545 _network_zone_subcommand "${zone}" ${args}
546 ;;
547 esac
548}
549
1ca5fe9f
JS
550_network_zone_new() {
551 local words=( $@ )
552 local cmd=${words[@]:0:1}
553
554 # Suggest useful zone names
555 if [[ -z "${cmd}" ]]; then
556 local commands="$(network raw list-next-free-zones)"
557 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
558
559 # If a valid zone name was entered, we can move on
560 elif network raw zone-name-is-valid ${cmd}; then
561 local args="${words[@]:1}"
562 _network_complete_hooks zone ${args}
563 fi
564
565 return 0
566}
567
1c6a4e30 568_network_zone_subcommand() {
bae37360
MT
569 local zone="${1}"
570 shift
571
572 local words=( $@ )
573
9d3ac4e4 574 local commands="color config description disable down edit enable identify port rename status up"
bae37360
MT
575 local cmd="$(_network_find_on_cmdline "${commands}")"
576 if [[ -z "${cmd}" ]]; then
577 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
578 return 0
579 fi
b7fbb0cd 580
bae37360
MT
581 local args="${words[@]:1}"
582 case "${cmd}" in
583 config)
2a6b2397 584 _network_zone_subcommand_config "${zone}" ${args}
bae37360
MT
585 ;;
586 port)
587 _network_zone_subcommand_port "${zone}" ${args}
588 ;;
b7fbb0cd
JS
589 color)
590 _network_color ${args}
591 ;;
9d3ac4e4
JS
592 description)
593 _network_description ${args}
594 ;;
bae37360
MT
595 esac
596}
597
2a6b2397
MT
598_network_zone_subcommand_config() {
599 local zone="${1}"
600 shift
601
602 local words=( $@ )
603
c8aafc4e 604 local commands="destroy list new $(network raw list-zone-config-hids ${zone})"
cbf7473c 605
2a6b2397
MT
606 local cmd="$(_network_find_on_cmdline "${commands}")"
607 if [[ -z "${cmd}" ]]; then
608 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
609 return 0
610 fi
611
cbf7473c 612 local args="${words[@]:1}"
2a6b2397
MT
613 case "${cmd}" in
614 new)
615 _network_complete_hooks "config"
616 ;;
cbf7473c
JS
617 destroy)
618 _network_zone_subcommand_config_destroy ${zone} ${args}
619 ;;
cbf7473c 620 *)
c8aafc4e 621 if network raw zone-config-id-is-valid ${zone} ${cmd} || network raw zone-config-hid-is-valid ${zone} ${cmd}; then
cbf7473c
JS
622 _network_zone_subcommand_config_subcommand ${zone} ${args}
623 fi
624 ;;
2a6b2397
MT
625 esac
626}
627
cbf7473c
JS
628_network_zone_subcommand_config_subcommand() {
629 local zone="${1}"
630 shift
631
632 local words=( $@ )
633
634 local commands="edit"
635
636 local cmd="$(_network_find_on_cmdline "${commands}")"
637 if [[ -z "${cmd}" ]]; then
638 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
639 return 0
640 fi
641}
642
643_network_zone_subcommand_config_destroy() {
644 local zone="${1}"
645 shift
646
647 local words=( $@ )
648
649 local commands="$(network raw list-zone-config-ids ${zone})"
650
651 local cmd="$(_network_find_on_cmdline "${commands}")"
652 if [[ -z "${cmd}" ]]; then
653 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
654 return 0
655 fi
656}
657
1c6a4e30 658_network_zone_subcommand_port() {
bae37360
MT
659 local zone="${1}"
660 shift
661
662 local words=( $@ )
663
664 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
665 local cmd="$(_network_find_on_cmdline "${commands}")"
666 if [[ -z "${cmd}" ]]; then
667 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
668 return 0
669 fi
670
671 case "${cmd}" in
672 attach)
673 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
674 -- "${cur}") )
675 ;;
676 detach)
677 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
678 -- "${cur}") )
679 ;;
680 *)
681 local port="${cmd}"
682 local args="${words[@]:1}"
683 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
684 ;;
685 esac
686}
687
1c6a4e30 688_network_zone_subcommand_port_subcommand() {
bae37360
MT
689 local zone="${1}"
690 local port="${2}"
691 shift 2
692
693 local words=( $@ )
694
695 local commands="edit"
696 local cmd="$(_network_find_on_cmdline "${commands}")"
697 if [[ -z "${cmd}" ]]; then
698 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
699 return 0
700 fi
701
702 case "${cmd}" in
703 edit)
704 # TODO auto-complete the zone-port hook
705 ;;
706 esac
707}
708
1c6a4e30 709_network() {
bae37360
MT
710 local cur prev words cword
711 _init_completion || return
712
713 local cmd i
714 for (( i=1; i < ${#words[@]} - 1; i++ )); do
715 [[ "${words[i]}" = -* ]] && continue
716 cmd="${words[i]}" && break
717 done
718
719 if [[ -z "${cmd}" ]]; then
720 case "${cur}" in
721 -*)
722 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
723 ;;
724 *)
cc02f6be 725 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
93097eca 726 help hostname port reset route settings status vpn zone" \
cc02f6be 727 -- "${cur}") )
bae37360
MT
728 ;;
729 esac
730
731 return 0
732 fi
733
734 local args="${words[@]:i+1}"
735 case "${cmd}" in
736 device)
737 _network_device ${args}
738 ;;
cc02f6be
MT
739 dhcpv[64])
740 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
741 ;;
8d1d2745
MT
742 dns-server)
743 _network_dns_server ${args}
744 ;;
bae37360
MT
745 port)
746 _network_port ${args}
747 ;;
8d1d2745
MT
748 route)
749 _network_route ${args}
750 ;;
bae37360
MT
751 settings)
752 _network_settings ${args}
753 ;;
754 start|stop|status)
755 # start, stop and status optionally take a zone
756 _network_complete_zones
757 ;;
93097eca
JS
758 vpn)
759 _network_vpn ${args}
760 ;;
bae37360
MT
761 zone)
762 _network_zone ${args}
763 ;;
764 esac
765} && complete -F _network network