]> git.ipfire.org Git - people/stevee/network.git/blame - src/bash-completion/network
Remove the function keyword which is a bashism
[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
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
75 local commands="discover monitor status unlock ussd"
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
250 local commands="create down edit remove status up"
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
261 local commands="add list remove"
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 add)
270 if [[ "${cur}" = -* ]]; then
271 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
272 --prohibit --unreachable" -- "${cur}") )
273 fi
274 ;;
275 list)
276 # TODO auto-complete options like --protocol here
277 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
278 ;;
279 esac
280}
281
1c6a4e30 282_network_settings() {
bae37360
MT
283 local words=( $@ )
284
285 local key keys
286 for key in $(network raw list-settings); do
287 keys="${keys} ${key}="
288 done
289 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
290}
291
1c6a4e30 292_network_zone() {
bae37360
MT
293 local words=( $@ )
294
295 local commands="new destroy $(network raw list-zones)"
296 local cmd="$(_network_find_on_cmdline "${commands}")"
297 if [[ -z "${cmd}" ]]; then
298 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
299 return 0
300 fi
301
302 local args="${words[@]:1}"
303 case "${cmd}" in
304 new)
305 # TODO
306 return 0
307 ;;
308 destroy)
309 _network_complete_zones
310 ;;
311 *)
312 local zone="${cmd}"
313 local args="${words[@]:1}"
314 _network_zone_subcommand "${zone}" ${args}
315 ;;
316 esac
317}
318
1c6a4e30 319_network_zone_subcommand() {
bae37360
MT
320 local zone="${1}"
321 shift
322
323 local words=( $@ )
324
325 local commands="config disable down edit enable port status up"
326 local cmd="$(_network_find_on_cmdline "${commands}")"
327 if [[ -z "${cmd}" ]]; then
328 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
329 return 0
330 fi
331
332 local args="${words[@]:1}"
333 case "${cmd}" in
334 config)
335 # TODO
336 ;;
337 port)
338 _network_zone_subcommand_port "${zone}" ${args}
339 ;;
340 esac
341}
342
1c6a4e30 343_network_zone_subcommand_port() {
bae37360
MT
344 local zone="${1}"
345 shift
346
347 local words=( $@ )
348
349 local commands="attach detach $(network raw list-ports-of-zone "${zone}")"
350 local cmd="$(_network_find_on_cmdline "${commands}")"
351 if [[ -z "${cmd}" ]]; then
352 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
353 return 0
354 fi
355
356 case "${cmd}" in
357 attach)
358 COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \
359 -- "${cur}") )
360 ;;
361 detach)
362 COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \
363 -- "${cur}") )
364 ;;
365 *)
366 local port="${cmd}"
367 local args="${words[@]:1}"
368 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
369 ;;
370 esac
371}
372
1c6a4e30 373_network_zone_subcommand_port_subcommand() {
bae37360
MT
374 local zone="${1}"
375 local port="${2}"
376 shift 2
377
378 local words=( $@ )
379
380 local commands="edit"
381 local cmd="$(_network_find_on_cmdline "${commands}")"
382 if [[ -z "${cmd}" ]]; then
383 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
384 return 0
385 fi
386
387 case "${cmd}" in
388 edit)
389 # TODO auto-complete the zone-port hook
390 ;;
391 esac
392}
393
1c6a4e30 394_network() {
bae37360
MT
395 local cur prev words cword
396 _init_completion || return
397
398 local cmd i
399 for (( i=1; i < ${#words[@]} - 1; i++ )); do
400 [[ "${words[i]}" = -* ]] && continue
401 cmd="${words[i]}" && break
402 done
403
404 if [[ -z "${cmd}" ]]; then
405 case "${cur}" in
406 -*)
407 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
408 ;;
409 *)
cc02f6be
MT
410 COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \
411 help hostname port reset route settings status zone" \
412 -- "${cur}") )
bae37360
MT
413 ;;
414 esac
415
416 return 0
417 fi
418
419 local args="${words[@]:i+1}"
420 case "${cmd}" in
421 device)
422 _network_device ${args}
423 ;;
cc02f6be
MT
424 dhcpv[64])
425 _network_dhcpd "${cmd/dhcpv/ipv}" ${args}
426 ;;
8d1d2745
MT
427 dns-server)
428 _network_dns_server ${args}
429 ;;
bae37360
MT
430 port)
431 _network_port ${args}
432 ;;
8d1d2745
MT
433 route)
434 _network_route ${args}
435 ;;
bae37360
MT
436 settings)
437 _network_settings ${args}
438 ;;
439 start|stop|status)
440 # start, stop and status optionally take a zone
441 _network_complete_zones
442 ;;
443 zone)
444 _network_zone ${args}
445 ;;
446 esac
447} && complete -F _network network