]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/scw/_scw
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / scw / _scw
1 #compdef scw
2 #
3 # zsh completion for scw (https://www.scaleway.com)
4 #
5 # Inspired by https://github.com/felixr/docker-zsh-completion
6
7 __scw_get_servers() {
8     local expl
9     declare -a servers
10     servers=(${(f)"$(_call_program commands scw _completion servers-names)"})
11     _describe -t servers "servers" servers
12 }
13
14 __scw_stoppedservers() {
15     __scw_get_servers
16 }
17
18 __scw_runningservers() {
19     __scw_get_servers
20 }
21
22 __scw_servers () {
23     __scw_get_servers
24 }
25
26 __scw_images () {
27     local expl
28     declare -a images
29     images=(${(f)"$(_call_program commands scw _completion images-names)"})
30     _describe -t images "images" images
31 }
32
33 __scw_images_and_snapshots () {
34     __scw_images
35     __scw_snapshots
36 }
37
38 __scw_snapshots () {
39     local expl
40     declare -a snapshots
41     snapshots=(${(f)"$(_call_program commands scw _completion --prefix snapshots-names)"})
42     _describe -t snapshots "snapshots" snapshots
43 }
44
45 __scw_bootscripts () {
46     local expl
47     declare -a bootscripts
48     bootscripts=(${(f)"$(_call_program commands scw _completion bootscripts-names)"})
49     _describe -t bootscripts "bootscripts" bootscripts
50 }
51
52 __scw_tags() {
53     __scw_images
54 }
55
56 __scw_repositories_with_tags() {
57     __scw_images
58 }
59
60 __scw_search() {
61     # declare -a scwsearch
62     local cache_policy
63     zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
64     if [[ -z "$cache_policy" ]]; then
65         zstyle ":completion:${curcontext}:" cache-policy __scw_caching_policy
66     fi
67
68     local searchterm cachename
69     searchterm="${words[$CURRENT]%/}"
70     cachename=_scw-search-$searchterm
71
72     local expl
73     local -a result
74     if ( [[ ${(P)+cachename} -eq 0 ]] || _cache_invalid ${cachename#_} ) \
75         && ! _retrieve_cache ${cachename#_}; then
76         _message "Searching for ${searchterm}..."
77         result=(${${${(f)"$(_call_program commands scw search ${searchterm})"}%% *}[2,-1]})
78         _store_cache ${cachename#_} result
79     fi
80     _wanted scwsearch expl 'available images' compadd -a result
81 }
82
83 __scw_caching_policy()
84 {
85   oldp=( "$1"(Nmh+1) )     # 1 hour
86   (( $#oldp ))
87 }
88
89
90 __scw_repositories () {
91     __scw_images
92 }
93
94 __scw_commands () {
95     # local -a  _scw_subcommands
96     local cache_policy
97
98     zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
99     if [[ -z "$cache_policy" ]]; then
100         zstyle ":completion:${curcontext}:" cache-policy __scw_caching_policy
101     fi
102
103     if ( [[ ${+_scw_subcommands} -eq 0 ]] || _cache_invalid scw_subcommands) \
104         && ! _retrieve_cache scw_subcommands;
105     then
106         local -a lines
107         lines=(${(f)"$(_call_program commands scw 2>&1)"})
108         _scw_subcommands=(${${${lines[$((${lines[(i)Commands:]} + 1)),${lines[(I)    *]}]}## #}/ ##/:})
109         _scw_subcommands=($_scw_subcommands 'help:Show help for a command')
110         _store_cache scw_subcommands _scw_subcommands
111     fi
112     _describe -t scw-commands "scw command" _scw_subcommands
113 }
114
115 __scw_subcommand () {
116     local -a _command_args
117     case "$words[1]" in
118         (attach)
119             _arguments \
120                 '--no-stdin[Do not attach stdin]' \
121                 ':servers:__scw_runningservers'
122             ;;
123         (commit)
124             _arguments \
125                 {-v,--volume=0}'[Volume slot]:volume: ' \
126                 ':server:__scw_servers' \
127                 ':repository:__scw_repositories_with_tags'
128             ;;
129         (cp)
130             _arguments \
131                 ':server:->server' \
132                 ':hostpath:_files'
133             case $state in
134                 (server)
135                     if compset -P '*:'; then
136                         _files
137                     else
138                         __scw_servers -qS ":"
139                     fi
140                     ;;
141             esac
142             ;;
143         (exec)
144             local state ret
145             _arguments \
146                 {-T,--timeout=0}'[Set timeout values to seconds]' \
147                 {-w,--wait}'[Wait for SSH to be ready]' \
148                 ':servers:__scw_runningservers' \
149                 '*::command:->anycommand' && ret=0
150
151             case $state in
152                 (anycommand)
153                     shift 1 words
154                     (( CURRENT-- ))
155                     _normal
156                     ;;
157             esac
158
159             return ret
160             ;;
161         (history)
162             _arguments \
163                 '--no-trunc[Do not truncate output]' \
164                 {-q,--quiet}'[Only show numeric IDs]' \
165                 '*:images:__scw_images'
166             ;;
167         (images)
168             _arguments \
169                 {-a,--all}'[Show all images]' \
170                 '--no-trunc[Do not truncate output]' \
171                 {-q,--quiet}'[Only show numeric IDs]' \
172                 ':repository:__scw_repositories'
173             ;;
174         (info)
175             ;;
176         (inspect)
177             _arguments \
178                 {-f,--format=-}'[Format the output using the given go template]:template: ' \
179                 '*:servers:__scw_servers'
180             ;;
181         (kill)
182             _arguments \
183                 '*:servers:__scw_runningservers'
184             ;;
185         (login)
186             _arguments \
187                 {-o,--organization=-}'[Organization]:organization: ' \
188                 {-t,--token=-}'[Token]:token: ' \
189                 ':server: '
190             ;;
191         (logout)
192             _arguments \
193                 ':server: '
194             ;;
195         (logs)
196             _arguments \
197                 '*:servers:__scw_servers'
198             ;;
199         (port)
200             _arguments \
201                 '1:servers:__scw_runningservers' \
202                 '2:port:_ports'
203             ;;
204         (start)
205             _arguments \
206                 {-T,--timeout=0}'[Set timeout values to seconds]' \
207                 {-w,--wait}'[Wait for SSH to be ready]' \
208                 '*:servers:__scw_stoppedservers'
209             ;;
210         (rm)
211             _arguments \
212                 '*:servers:__scw_stoppedservers'
213             ;;
214         (rmi)
215             _arguments \
216                 '*:images:__scw_images'
217             ;;
218         (restart)
219             _arguments \
220                 '*:servers:__scw_runningservers'
221             ;;
222         (stop)
223             _arguments \
224                 {-t,--terminate}'[Stop and trash a server with its volumes]' \
225                 {-w,--wait}'[Synchronous stop. Wait for server to be stopped]' \
226                 '*:servers:__scw_runningservers'
227             ;;
228         (top)
229             _arguments \
230                 '1:servers:__scw_runningservers' \
231                 '(-)*:: :->ps-arguments'
232             case $state in
233                 (ps-arguments)
234                     _ps
235                     ;;
236             esac
237             ;;
238         (ps)
239             _arguments \
240                 {-a,--all}'[Show all servers. Only running servers are shown by default]' \
241                 {-l,--latest}'[Show only the latest created server]' \
242                 '-n[Show n last created servers, include non-running one]:n:(1 5 10 25 50)' \
243                 '--no-trunc[Do not truncate output]' \
244                 {-q,--quiet}'[Only show numeric IDs]'
245             ;;
246         (tag)
247             _arguments \
248                 {-f,--force}'[force]'\
249                 ':image:__scw_images'\
250                 ':repository:__scw_repositories_with_tags'
251             ;;
252         (create|run)
253             _arguments \
254                 {-a,--attach}'[Attach to stdin, stdout or stderr]' \
255                 '*'{-e,--environment=-}'[Set environment variables]:environment variable: ' \
256                 '--name=-[Server name]:name: ' \
257                 '--bootscript=-[Assign a bootscript]:bootscript:__scw_bootscripts ' \
258                 '*-v[Bind mount a volume]:volume: '\
259                 '(-):images:__scw_images_and_snapshots' \
260                 '(-):command: _command_names -e' \
261                 '*::arguments: _normal'
262
263             case $state in
264                 (link)
265                     if compset -P '*:'; then
266                         _wanted alias expl 'Alias' compadd -E ""
267                     else
268                         __scw_runningservers -qS ":"
269                     fi
270                     ;;
271             esac
272             ;;
273         (rename)
274             _arguments \
275                 ':old name:__scw_servers' \
276                 ':new name: '
277             ;;
278         (search)
279             _arguments \
280                 '--no-trunc[Do not truncate output]' \
281                 ':term: '
282             ;;
283         (wait)
284             _arguments '*:servers:__scw_runningservers'
285             ;;
286         (help)
287             _arguments ':subcommand:__scw_commands'
288             ;;
289         (*)
290             _message 'Unknown sub command'
291     esac
292
293 }
294
295 _scw () {
296     # Support for subservices, which allows for `compdef _scw scw-shell=_scw_servers`.
297     # Based on /usr/share/zsh/functions/Completion/Unix/_git without support for `ret`.
298     if [[ $service != scw ]]; then
299         _call_function - _$service
300         return
301     fi
302
303     local curcontext="$curcontext" state line
304     typeset -A opt_args
305
306     _arguments -C \
307       '-H[tcp://host:port to bind/connect to]:socket: ' \
308          '(-): :->command' \
309          '(-)*:: :->option-or-argument'
310
311     if (( CURRENT == 1 )); then
312
313     fi
314     case $state in
315         (command)
316             __scw_commands
317             ;;
318         (option-or-argument)
319             curcontext=${curcontext%:*:*}:scw-$words[1]:
320             __scw_subcommand
321             ;;
322     esac
323 }
324
325 _scw "$@"
326
327 # Local Variables:
328 # mode: Shell-Script
329 # sh-indentation: 4
330 # indent-tabs-mode: nil
331 # sh-basic-offset: 4
332 # End:
333 # vim: ft=zsh sw=4 ts=4 et