]>
Commit | Line | Data |
---|---|---|
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 | ||
98699861 | 346 | local commands="ipsec security-policies" |
93097eca JS |
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 | ;; | |
98699861 JS |
359 | security-policies) |
360 | _network_vpn_security_policies ${args} | |
361 | ;; | |
93097eca JS |
362 | esac |
363 | } | |
364 | ||
365 | _network_vpn_ipsec() { | |
366 | local words=( $@ ) | |
367 | ||
368 | local commands="connection" | |
369 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
370 | if [[ -z "${cmd}" ]]; then | |
371 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
372 | return 0 | |
373 | fi | |
374 | ||
375 | ||
376 | local args="${words[@]:1}" | |
377 | case "${cmd}" in | |
378 | connection) | |
379 | _network_vpn_ipsec_connection ${args} | |
380 | ;; | |
381 | esac | |
382 | } | |
383 | ||
384 | _network_vpn_ipsec_connection() { | |
385 | local words=( $@ ) | |
386 | ||
387 | local commands="destroy new $(network raw list-ipsec-connections)" | |
388 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
389 | if [[ -z "${cmd}" ]]; then | |
390 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
391 | return 0 | |
392 | fi | |
393 | ||
394 | ||
395 | local args="${words[@]:1}" | |
396 | case "${cmd}" in | |
397 | destroy) | |
398 | : | |
399 | ;; | |
400 | new) | |
401 | : | |
402 | ;; | |
403 | *) | |
404 | if network raw ipsec-connection-exists ${cmd}; then | |
405 | _network_vpn_ipsec_connection_subcommands ${cmd} ${args} | |
406 | fi | |
407 | ;; | |
408 | esac | |
409 | } | |
410 | ||
411 | _network_vpn_ipsec_connection_subcommands() { | |
412 | local connection=${1} | |
413 | shift | |
414 | local words=( $@ ) | |
415 | ||
5bcadc60 | 416 | local commands="authentication color down inactivity-timeout local mode peer remote security-policy show up" |
93097eca JS |
417 | local cmd="$(_network_find_on_cmdline "${commands}")" |
418 | if [[ -z "${cmd}" ]]; then | |
419 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
420 | return 0 | |
421 | fi | |
422 | ||
423 | ||
424 | local args="${words[@]:1}" | |
425 | case "${cmd}" in | |
426 | authentication) | |
427 | _network_vpn_ipsec_connection_subcommands_authentication ${connection} ${args} | |
428 | ;; | |
5bcadc60 JS |
429 | color) |
430 | _network_color ${args} | |
431 | ;; | |
93097eca JS |
432 | local) |
433 | _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "local" ${args} | |
434 | ;; | |
435 | mode) | |
436 | _network_vpn_ipsec_connection_subcommands_mode ${args} | |
437 | ;; | |
438 | remote) | |
faf53925 | 439 | _network_vpn_ipsec_connection_subcommands_local_remote ${connection} "remote" ${args} |
93097eca JS |
440 | ;; |
441 | security-policy) | |
442 | _network_vpn_ipsec_connection_subcommands_security_policy ${args} | |
443 | ;; | |
444 | esac | |
445 | } | |
446 | ||
447 | _network_vpn_ipsec_connection_subcommands_authentication() { | |
448 | local connection=${1} | |
449 | shift | |
450 | local words=( $@ ) | |
451 | ||
6fe4b2fd | 452 | local commands="mode pre-shared-key" |
93097eca JS |
453 | local cmd="$(_network_find_on_cmdline "${commands}")" |
454 | if [[ -z "${cmd}" ]]; then | |
455 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
456 | return 0 | |
457 | fi | |
458 | ||
459 | ||
460 | local args="${words[@]:1}" | |
461 | case "${cmd}" in | |
462 | mode) | |
463 | _network_vpn_ipsec_connection_subcommands_authentication_mode ${connection} ${args} | |
464 | ;; | |
465 | esac | |
466 | } | |
467 | ||
468 | _network_vpn_ipsec_connection_subcommands_local_remote() { | |
469 | local connection=${1} | |
470 | local type=${2} | |
471 | shift 2 | |
472 | local words=( $@ ) | |
473 | ||
474 | local commands="id prefix" | |
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 | ||
482 | local args="${words[@]:1}" | |
483 | case "${cmd}" in | |
484 | prefix) | |
485 | _network_vpn_ipsec_connection_subcommands_prefix "${connection}" "${type}" ${args} | |
486 | ;; | |
487 | esac | |
488 | } | |
489 | ||
490 | _network_vpn_ipsec_connection_subcommands_prefix() { | |
491 | : | |
492 | } | |
493 | ||
494 | _network_vpn_ipsec_connection_subcommands_authentication_mode() { | |
495 | local connection=${1} | |
496 | shift | |
497 | local words=( $@ ) | |
498 | ||
499 | local commands="PSK" | |
500 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
501 | if [[ -z "${cmd}" ]]; then | |
502 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
503 | return 0 | |
504 | fi | |
505 | } | |
506 | ||
507 | _network_vpn_ipsec_connection_subcommands_mode() { | |
508 | local words=( $@ ) | |
509 | ||
510 | local commands="gre-transport tunnel vti" | |
511 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
512 | if [[ -z "${cmd}" ]]; then | |
513 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
514 | return 0 | |
515 | fi | |
516 | } | |
517 | ||
518 | _network_vpn_ipsec_connection_subcommands_security_policy() { | |
519 | local words=( $@ ) | |
520 | ||
521 | local commands="$(network raw list-vpn-security-policies-all)" | |
522 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
523 | if [[ -z "${cmd}" ]]; then | |
524 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
525 | return 0 | |
526 | fi | |
527 | } | |
528 | ||
98699861 JS |
529 | _network_vpn_security_policies() { |
530 | local words=( $@ ) | |
531 | ||
532 | local commands="destroy new $(network raw list-vpn-security-policies-all)" | |
533 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
534 | if [[ -z "${cmd}" ]]; then | |
535 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
536 | return 0 | |
537 | fi | |
538 | ||
539 | ||
540 | local args="${words[@]:1}" | |
541 | case "${cmd}" in | |
542 | destroy) | |
543 | : | |
544 | ;; | |
545 | new) | |
546 | : | |
547 | ;; | |
548 | *) | |
549 | if network raw vpn-security-policy-exists ${cmd}; then | |
550 | _network_vpn_security_policies_subcommands ${cmd} ${args} | |
551 | fi | |
552 | ;; | |
553 | esac | |
554 | } | |
555 | ||
556 | _network_vpn_security_policies_subcommands() { | |
557 | local policy=${1} | |
558 | shift | |
559 | local words=( $@ ) | |
560 | ||
561 | local commands="cipher compression group-type integrity key-exchange lifetime pfs show" | |
562 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
563 | if [[ -z "${cmd}" ]]; then | |
564 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
565 | return 0 | |
566 | fi | |
567 | ||
568 | ||
569 | local args="${words[@]:1}" | |
570 | case "${cmd}" in | |
571 | cipher) | |
572 | _network_vpn_security_policies_subcommands_cipher ${policy} ${args} | |
573 | ;; | |
574 | compression) | |
575 | _network_vpn_security_policies_subcommands_compression ${policy} ${args} | |
576 | ;; | |
577 | group-type) | |
578 | _network_vpn_security_policies_subcommands_group_type ${policy} ${args} | |
579 | ;; | |
580 | integrity) | |
581 | _network_vpn_security_policies_subcommands_integrity ${policy} ${args} | |
582 | ;; | |
583 | key-exchange) | |
584 | _network_vpn_security_policies_subcommands_key_exchange ${policy} ${args} | |
585 | ;; | |
586 | pfs) | |
587 | _network_vpn_security_policies_subcommands_pfs ${policy} ${args} | |
588 | ;; | |
589 | esac | |
590 | } | |
591 | ||
592 | _network_vpn_security_policies_subcommands_cipher() { | |
593 | : | |
594 | } | |
595 | ||
596 | _network_vpn_security_policies_subcommands_compression() { | |
597 | : | |
598 | } | |
599 | ||
600 | _network_vpn_security_policies_subcommands_group_type() { | |
601 | : | |
602 | } | |
603 | ||
604 | _network_vpn_security_policies_subcommands_integrity() { | |
605 | : | |
606 | } | |
607 | ||
608 | _network_vpn_security_policies_subcommands_key_exchange() { | |
609 | : | |
610 | } | |
611 | ||
612 | _network_vpn_security_policies_subcommands_pfs() { | |
613 | : | |
614 | } | |
615 | ||
1c6a4e30 | 616 | _network_zone() { |
bae37360 MT |
617 | local words=( $@ ) |
618 | ||
619 | local commands="new destroy $(network raw list-zones)" | |
620 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
621 | if [[ -z "${cmd}" ]]; then | |
622 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
623 | return 0 | |
624 | fi | |
625 | ||
1ca5fe9f | 626 | |
bae37360 MT |
627 | local args="${words[@]:1}" |
628 | case "${cmd}" in | |
629 | new) | |
1ca5fe9f | 630 | _network_zone_new ${args} |
bae37360 MT |
631 | ;; |
632 | destroy) | |
633 | _network_complete_zones | |
634 | ;; | |
635 | *) | |
636 | local zone="${cmd}" | |
637 | local args="${words[@]:1}" | |
638 | _network_zone_subcommand "${zone}" ${args} | |
639 | ;; | |
640 | esac | |
641 | } | |
642 | ||
1ca5fe9f JS |
643 | _network_zone_new() { |
644 | local words=( $@ ) | |
645 | local cmd=${words[@]:0:1} | |
646 | ||
647 | # Suggest useful zone names | |
648 | if [[ -z "${cmd}" ]]; then | |
649 | local commands="$(network raw list-next-free-zones)" | |
650 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
651 | ||
652 | # If a valid zone name was entered, we can move on | |
653 | elif network raw zone-name-is-valid ${cmd}; then | |
654 | local args="${words[@]:1}" | |
655 | _network_complete_hooks zone ${args} | |
656 | fi | |
657 | ||
658 | return 0 | |
659 | } | |
660 | ||
1c6a4e30 | 661 | _network_zone_subcommand() { |
bae37360 MT |
662 | local zone="${1}" |
663 | shift | |
664 | ||
665 | local words=( $@ ) | |
666 | ||
9d3ac4e4 | 667 | local commands="color config description disable down edit enable identify port rename status up" |
bae37360 MT |
668 | local cmd="$(_network_find_on_cmdline "${commands}")" |
669 | if [[ -z "${cmd}" ]]; then | |
670 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
671 | return 0 | |
672 | fi | |
b7fbb0cd | 673 | |
bae37360 MT |
674 | local args="${words[@]:1}" |
675 | case "${cmd}" in | |
676 | config) | |
2a6b2397 | 677 | _network_zone_subcommand_config "${zone}" ${args} |
bae37360 MT |
678 | ;; |
679 | port) | |
680 | _network_zone_subcommand_port "${zone}" ${args} | |
681 | ;; | |
b7fbb0cd JS |
682 | color) |
683 | _network_color ${args} | |
684 | ;; | |
9d3ac4e4 JS |
685 | description) |
686 | _network_description ${args} | |
687 | ;; | |
bae37360 MT |
688 | esac |
689 | } | |
690 | ||
2a6b2397 MT |
691 | _network_zone_subcommand_config() { |
692 | local zone="${1}" | |
693 | shift | |
694 | ||
695 | local words=( $@ ) | |
696 | ||
c8aafc4e | 697 | local commands="destroy list new $(network raw list-zone-config-hids ${zone})" |
cbf7473c | 698 | |
2a6b2397 MT |
699 | local cmd="$(_network_find_on_cmdline "${commands}")" |
700 | if [[ -z "${cmd}" ]]; then | |
701 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
702 | return 0 | |
703 | fi | |
704 | ||
cbf7473c | 705 | local args="${words[@]:1}" |
2a6b2397 MT |
706 | case "${cmd}" in |
707 | new) | |
708 | _network_complete_hooks "config" | |
709 | ;; | |
cbf7473c JS |
710 | destroy) |
711 | _network_zone_subcommand_config_destroy ${zone} ${args} | |
712 | ;; | |
cbf7473c | 713 | *) |
c8aafc4e | 714 | if network raw zone-config-id-is-valid ${zone} ${cmd} || network raw zone-config-hid-is-valid ${zone} ${cmd}; then |
cbf7473c JS |
715 | _network_zone_subcommand_config_subcommand ${zone} ${args} |
716 | fi | |
717 | ;; | |
2a6b2397 MT |
718 | esac |
719 | } | |
720 | ||
cbf7473c JS |
721 | _network_zone_subcommand_config_subcommand() { |
722 | local zone="${1}" | |
723 | shift | |
724 | ||
725 | local words=( $@ ) | |
726 | ||
727 | local commands="edit" | |
728 | ||
729 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
730 | if [[ -z "${cmd}" ]]; then | |
731 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
732 | return 0 | |
733 | fi | |
734 | } | |
735 | ||
736 | _network_zone_subcommand_config_destroy() { | |
737 | local zone="${1}" | |
738 | shift | |
739 | ||
740 | local words=( $@ ) | |
741 | ||
742 | local commands="$(network raw list-zone-config-ids ${zone})" | |
743 | ||
744 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
745 | if [[ -z "${cmd}" ]]; then | |
746 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
747 | return 0 | |
748 | fi | |
749 | } | |
750 | ||
1c6a4e30 | 751 | _network_zone_subcommand_port() { |
bae37360 MT |
752 | local zone="${1}" |
753 | shift | |
754 | ||
755 | local words=( $@ ) | |
756 | ||
757 | local commands="attach detach $(network raw list-ports-of-zone "${zone}")" | |
758 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
759 | if [[ -z "${cmd}" ]]; then | |
760 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
761 | return 0 | |
762 | fi | |
763 | ||
764 | case "${cmd}" in | |
765 | attach) | |
766 | COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \ | |
767 | -- "${cur}") ) | |
768 | ;; | |
769 | detach) | |
770 | COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \ | |
771 | -- "${cur}") ) | |
772 | ;; | |
773 | *) | |
774 | local port="${cmd}" | |
775 | local args="${words[@]:1}" | |
776 | _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args} | |
777 | ;; | |
778 | esac | |
779 | } | |
780 | ||
1c6a4e30 | 781 | _network_zone_subcommand_port_subcommand() { |
bae37360 MT |
782 | local zone="${1}" |
783 | local port="${2}" | |
784 | shift 2 | |
785 | ||
786 | local words=( $@ ) | |
787 | ||
788 | local commands="edit" | |
789 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
790 | if [[ -z "${cmd}" ]]; then | |
791 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
792 | return 0 | |
793 | fi | |
794 | ||
795 | case "${cmd}" in | |
796 | edit) | |
797 | # TODO auto-complete the zone-port hook | |
798 | ;; | |
799 | esac | |
800 | } | |
801 | ||
1c6a4e30 | 802 | _network() { |
bae37360 MT |
803 | local cur prev words cword |
804 | _init_completion || return | |
805 | ||
806 | local cmd i | |
807 | for (( i=1; i < ${#words[@]} - 1; i++ )); do | |
808 | [[ "${words[i]}" = -* ]] && continue | |
809 | cmd="${words[i]}" && break | |
810 | done | |
811 | ||
812 | if [[ -z "${cmd}" ]]; then | |
813 | case "${cur}" in | |
814 | -*) | |
815 | COMPREPLY=( $(compgen -W "--debug" -- "${cur}") ) | |
816 | ;; | |
817 | *) | |
cc02f6be | 818 | COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \ |
93097eca | 819 | help hostname port reset route settings status vpn zone" \ |
cc02f6be | 820 | -- "${cur}") ) |
bae37360 MT |
821 | ;; |
822 | esac | |
823 | ||
824 | return 0 | |
825 | fi | |
826 | ||
827 | local args="${words[@]:i+1}" | |
828 | case "${cmd}" in | |
829 | device) | |
830 | _network_device ${args} | |
831 | ;; | |
cc02f6be MT |
832 | dhcpv[64]) |
833 | _network_dhcpd "${cmd/dhcpv/ipv}" ${args} | |
834 | ;; | |
8d1d2745 MT |
835 | dns-server) |
836 | _network_dns_server ${args} | |
837 | ;; | |
bae37360 MT |
838 | port) |
839 | _network_port ${args} | |
840 | ;; | |
8d1d2745 MT |
841 | route) |
842 | _network_route ${args} | |
843 | ;; | |
bae37360 MT |
844 | settings) |
845 | _network_settings ${args} | |
846 | ;; | |
847 | start|stop|status) | |
848 | # start, stop and status optionally take a zone | |
849 | _network_complete_zones | |
850 | ;; | |
93097eca JS |
851 | vpn) |
852 | _network_vpn ${args} | |
853 | ;; | |
bae37360 MT |
854 | zone) |
855 | _network_zone ${args} | |
856 | ;; | |
857 | esac | |
858 | } && complete -F _network network |