]>
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 | ||
23 | function _network_find_on_cmdline () { | |
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 | ||
37 | function _network_complete_hooks() { | |
38 | local type="${1}" | |
39 | ||
40 | COMPREPLY=( $(compgen -W "$(network raw list-hooks "${type}")" -- "${cur}") ) | |
41 | } | |
42 | ||
43 | function _network_complete_ports() { | |
44 | COMPREPLY=( $(compgen -W "$(network raw list-ports)" -- "${cur}") ) | |
45 | } | |
46 | ||
47 | function _network_complete_zones() { | |
48 | COMPREPLY=( $(compgen -W "$(network raw list-zones)" -- "${cur}") ) | |
49 | } | |
50 | ||
51 | function _network_device() { | |
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 | ||
72 | function _network_device_subcommand() { | |
73 | local words=( $@ ) | |
74 | ||
75 | local commands="discover monitor status unlock ussd" | |
76 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
77 | ||
78 | if [[ -z "${cmd}" ]]; then | |
79 | COMPREPLY=( $(compgen -W "discover monitor status unlock ussd" \ | |
80 | -- "${cur}") ) | |
81 | return 0 | |
82 | fi | |
83 | ||
84 | case "${cmd}" in | |
85 | ussd) | |
86 | # TODO | |
87 | ;; | |
88 | esac | |
89 | } | |
90 | ||
91 | function _network_port() { | |
92 | local words=( $@ ) | |
93 | ||
94 | local commands="new destroy $(network raw list-ports)" | |
95 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
96 | if [[ -z "${cmd}" ]]; then | |
97 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
98 | return 0 | |
99 | fi | |
100 | ||
101 | local args="${words[@]:1}" | |
102 | case "${cmd}" in | |
103 | new) | |
104 | _network_complete_hooks "port" | |
105 | ;; | |
106 | destroy) | |
107 | _network_complete_ports | |
108 | ;; | |
109 | *) | |
110 | local args="${words[@]:1}" | |
111 | _network_port_subcommand ${args} | |
112 | ;; | |
113 | esac | |
114 | } | |
115 | ||
116 | function _network_port_subcommand() { | |
117 | local words=( $@ ) | |
118 | ||
119 | local commands="create down edit remove status up" | |
120 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
121 | if [[ -z "${cmd}" ]]; then | |
122 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
123 | return 0 | |
124 | fi | |
125 | } | |
126 | ||
127 | function _network_settings() { | |
128 | local words=( $@ ) | |
129 | ||
130 | local key keys | |
131 | for key in $(network raw list-settings); do | |
132 | keys="${keys} ${key}=" | |
133 | done | |
134 | COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") ) | |
135 | } | |
136 | ||
137 | function _network_zone() { | |
138 | local words=( $@ ) | |
139 | ||
140 | local commands="new destroy $(network raw list-zones)" | |
141 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
142 | if [[ -z "${cmd}" ]]; then | |
143 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
144 | return 0 | |
145 | fi | |
146 | ||
147 | local args="${words[@]:1}" | |
148 | case "${cmd}" in | |
149 | new) | |
150 | # TODO | |
151 | return 0 | |
152 | ;; | |
153 | destroy) | |
154 | _network_complete_zones | |
155 | ;; | |
156 | *) | |
157 | local zone="${cmd}" | |
158 | local args="${words[@]:1}" | |
159 | _network_zone_subcommand "${zone}" ${args} | |
160 | ;; | |
161 | esac | |
162 | } | |
163 | ||
164 | function _network_zone_subcommand() { | |
165 | local zone="${1}" | |
166 | shift | |
167 | ||
168 | local words=( $@ ) | |
169 | ||
170 | local commands="config disable down edit enable port status up" | |
171 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
172 | if [[ -z "${cmd}" ]]; then | |
173 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
174 | return 0 | |
175 | fi | |
176 | ||
177 | local args="${words[@]:1}" | |
178 | case "${cmd}" in | |
179 | config) | |
180 | # TODO | |
181 | ;; | |
182 | port) | |
183 | _network_zone_subcommand_port "${zone}" ${args} | |
184 | ;; | |
185 | esac | |
186 | } | |
187 | ||
188 | function _network_zone_subcommand_port() { | |
189 | local zone="${1}" | |
190 | shift | |
191 | ||
192 | local words=( $@ ) | |
193 | ||
194 | local commands="attach detach $(network raw list-ports-of-zone "${zone}")" | |
195 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
196 | if [[ -z "${cmd}" ]]; then | |
197 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
198 | return 0 | |
199 | fi | |
200 | ||
201 | case "${cmd}" in | |
202 | attach) | |
203 | COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \ | |
204 | -- "${cur}") ) | |
205 | ;; | |
206 | detach) | |
207 | COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \ | |
208 | -- "${cur}") ) | |
209 | ;; | |
210 | *) | |
211 | local port="${cmd}" | |
212 | local args="${words[@]:1}" | |
213 | _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args} | |
214 | ;; | |
215 | esac | |
216 | } | |
217 | ||
218 | function _network_zone_subcommand_port_subcommand() { | |
219 | local zone="${1}" | |
220 | local port="${2}" | |
221 | shift 2 | |
222 | ||
223 | local words=( $@ ) | |
224 | ||
225 | local commands="edit" | |
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 | case "${cmd}" in | |
233 | edit) | |
234 | # TODO auto-complete the zone-port hook | |
235 | ;; | |
236 | esac | |
237 | } | |
238 | ||
239 | function _network() { | |
240 | local cur prev words cword | |
241 | _init_completion || return | |
242 | ||
243 | local cmd i | |
244 | for (( i=1; i < ${#words[@]} - 1; i++ )); do | |
245 | [[ "${words[i]}" = -* ]] && continue | |
246 | cmd="${words[i]}" && break | |
247 | done | |
248 | ||
249 | if [[ -z "${cmd}" ]]; then | |
250 | case "${cur}" in | |
251 | -*) | |
252 | COMPREPLY=( $(compgen -W "--debug" -- "${cur}") ) | |
253 | ;; | |
254 | *) | |
255 | COMPREPLY=( $(compgen -W "device hostname port settings status zone" \ | |
256 | -- "${cur}") ) | |
257 | ;; | |
258 | esac | |
259 | ||
260 | return 0 | |
261 | fi | |
262 | ||
263 | local args="${words[@]:i+1}" | |
264 | case "${cmd}" in | |
265 | device) | |
266 | _network_device ${args} | |
267 | ;; | |
268 | port) | |
269 | _network_port ${args} | |
270 | ;; | |
271 | settings) | |
272 | _network_settings ${args} | |
273 | ;; | |
274 | start|stop|status) | |
275 | # start, stop and status optionally take a zone | |
276 | _network_complete_zones | |
277 | ;; | |
278 | zone) | |
279 | _network_zone ${args} | |
280 | ;; | |
281 | esac | |
282 | } && complete -F _network network |