]> git.ipfire.org Git - people/ms/network.git/blame - src/bash-completion/network
route: rename route to route static
[people/ms/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
1c6a4e30 51_network_device() {
bae37360
MT
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
1c6a4e30 72_network_device_subcommand() {
bae37360
MT
73 local words=( $@ )
74
f5ee091e 75 local commands="discover identify monitor status unlock ussd"
bae37360 76 local cmd="$(_network_find_on_cmdline "${commands}")"
bae37360 77 if [[ -z "${cmd}" ]]; then
8d1d2745 78 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
bae37360
MT
79 return 0
80 fi
81
82 case "${cmd}" in
83 ussd)
84 # TODO
85 ;;
86 esac
87}
88
1c6a4e30 89_network_dhcpd() {
cc02f6be
MT
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
1c6a4e30 110_network_dhcpd_subnet() {
cc02f6be
MT
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
1c6a4e30 138_network_dhcpd_subnet_subcommand() {
cc02f6be
MT
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
1c6a4e30 166_network_dhcpd_subnet_subcommand_options() {
cc02f6be
MT
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
1c6a4e30 179_network_dhcpd_subnet_subcommand_range() {
cc02f6be
MT
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
1c6a4e30 204_network_dns_server() {
8d1d2745
MT
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
1c6a4e30 222_network_port() {
bae37360
MT
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
1c6a4e30 247_network_port_subcommand() {
bae37360
MT
248 local words=( $@ )
249
f5ee091e 250 local commands="create down edit identify remove status up"
bae37360
MT
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
1c6a4e30 258_network_route() {
8d1d2745
MT
259 local words=( $@ )
260
240367ba
JS
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
86c225b8 280 local commands="add list remove reload"
8d1d2745
MT
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
1c6a4e30 301_network_settings() {
bae37360
MT
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
1c6a4e30 311_network_zone() {
bae37360
MT
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 local args="${words[@]:1}"
322 case "${cmd}" in
323 new)
324 # TODO
325 return 0
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
1c6a4e30 338_network_zone_subcommand() {
bae37360
MT
339 local zone="${1}"
340 shift
341
342 local words=( $@ )
343
f5ee091e 344 local commands="config disable down edit enable identify port rename status up"
bae37360
MT
345 local cmd="$(_network_find_on_cmdline "${commands}")"
346 if [[ -z "${cmd}" ]]; then
347 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
348 return 0
349 fi
350
351 local args="${words[@]:1}"
352 case "${cmd}" in
353 config)
2a6b2397 354 _network_zone_subcommand_config "${zone}" ${args}
bae37360
MT
355 ;;
356 port)
357 _network_zone_subcommand_port "${zone}" ${args}
358 ;;
359 esac
360}
361
2a6b2397
MT
362_network_zone_subcommand_config() {
363 local zone="${1}"
364 shift
365
366 local words=( $@ )
367
368 local commands="new destroy"
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 case "${cmd}" in
376 new)
377 _network_complete_hooks "config"
378 ;;
379 esac
380}
381
1c6a4e30 382_network_zone_subcommand_port() {
bae37360
MT
383 local zone="${1}"
384 shift
385
386 local words=( $@ )
387
388 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
389 local cmd="$(_network_find_on_cmdline "${commands}")"
390 if [[ -z "${cmd}" ]]; then
391 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
392 return 0
393 fi
394
395 case "${cmd}" in
396 attach)
397 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
398 -- "${cur}") )
399 ;;
400 detach)
401 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
402 -- "${cur}") )
403 ;;
404 *)
405 local port="${cmd}"
406 local args="${words[@]:1}"
407 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
408 ;;
409 esac
410}
411
1c6a4e30 412_network_zone_subcommand_port_subcommand() {
bae37360
MT
413 local zone="${1}"
414 local port="${2}"
415 shift 2
416
417 local words=( $@ )
418
419 local commands="edit"
420 local cmd="$(_network_find_on_cmdline "${commands}")"
421 if [[ -z "${cmd}" ]]; then
422 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
423 return 0
424 fi
425
426 case "${cmd}" in
427 edit)
428 # TODO auto-complete the zone-port hook
429 ;;
430 esac
431}
432
1c6a4e30 433_network() {
bae37360
MT
434 local cur prev words cword
435 _init_completion || return
436
437 local cmd i
438 for (( i=1; i < ${#words[@]} - 1; i++ )); do
439 [[ "${words[i]}" = -* ]] && continue
440 cmd="${words[i]}" && break
441 done
442
443 if [[ -z "${cmd}" ]]; then
444 case "${cur}" in
445 -*)
446 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
447 ;;
448 *)
cc02f6be
MT
449 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
450 help hostname port reset route settings status zone" \
451 -- "${cur}") )
bae37360
MT
452 ;;
453 esac
454
455 return 0
456 fi
457
458 local args="${words[@]:i+1}"
459 case "${cmd}" in
460 device)
461 _network_device ${args}
462 ;;
cc02f6be
MT
463 dhcpv[64])
464 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
465 ;;
8d1d2745
MT
466 dns-server)
467 _network_dns_server ${args}
468 ;;
bae37360
MT
469 port)
470 _network_port ${args}
471 ;;
8d1d2745
MT
472 route)
473 _network_route ${args}
474 ;;
bae37360
MT
475 settings)
476 _network_settings ${args}
477 ;;
478 start|stop|status)
479 # start, stop and status optionally take a zone
480 _network_complete_zones
481 ;;
482 zone)
483 _network_zone ${args}
484 ;;
485 esac
486} && complete -F _network network