]> git.ipfire.org Git - people/stevee/network.git/blame - src/bash-completion/network
autocompletion: add description support
[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
424 local commands="new destroy"
425 local cmd="$(_network_find_on_cmdline "${commands}")"
426 if [[ -z "${cmd}" ]]; then
427 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
428 return 0
429 fi
430
431 case "${cmd}" in
432 new)
433 _network_complete_hooks "config"
434 ;;
435 esac
436}
437
1c6a4e30 438_network_zone_subcommand_port() {
bae37360
MT
439 local zone="${1}"
440 shift
441
442 local words=( $@ )
443
444 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
445 local cmd="$(_network_find_on_cmdline "${commands}")"
446 if [[ -z "${cmd}" ]]; then
447 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
448 return 0
449 fi
450
451 case "${cmd}" in
452 attach)
453 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
454 -- "${cur}") )
455 ;;
456 detach)
457 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
458 -- "${cur}") )
459 ;;
460 *)
461 local port="${cmd}"
462 local args="${words[@]:1}"
463 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
464 ;;
465 esac
466}
467
1c6a4e30 468_network_zone_subcommand_port_subcommand() {
bae37360
MT
469 local zone="${1}"
470 local port="${2}"
471 shift 2
472
473 local words=( $@ )
474
475 local commands="edit"
476 local cmd="$(_network_find_on_cmdline "${commands}")"
477 if [[ -z "${cmd}" ]]; then
478 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
479 return 0
480 fi
481
482 case "${cmd}" in
483 edit)
484 # TODO auto-complete the zone-port hook
485 ;;
486 esac
487}
488
1c6a4e30 489_network() {
bae37360
MT
490 local cur prev words cword
491 _init_completion || return
492
493 local cmd i
494 for (( i=1; i < ${#words[@]} - 1; i++ )); do
495 [[ "${words[i]}" = -* ]] && continue
496 cmd="${words[i]}" && break
497 done
498
499 if [[ -z "${cmd}" ]]; then
500 case "${cur}" in
501 -*)
502 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
503 ;;
504 *)
cc02f6be
MT
505 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
506 help hostname port reset route settings status zone" \
507 -- "${cur}") )
bae37360
MT
508 ;;
509 esac
510
511 return 0
512 fi
513
514 local args="${words[@]:i+1}"
515 case "${cmd}" in
516 device)
517 _network_device ${args}
518 ;;
cc02f6be
MT
519 dhcpv[64])
520 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
521 ;;
8d1d2745
MT
522 dns-server)
523 _network_dns_server ${args}
524 ;;
bae37360
MT
525 port)
526 _network_port ${args}
527 ;;
8d1d2745
MT
528 route)
529 _network_route ${args}
530 ;;
bae37360
MT
531 settings)
532 _network_settings ${args}
533 ;;
534 start|stop|status)
535 # start, stop and status optionally take a zone
536 _network_complete_zones
537 ;;
538 zone)
539 _network_zone ${args}
540 ;;
541 esac
542} && complete -F _network network