3 # zsh completion for scw (https://www.scaleway.com)
5 # Inspired by https://github.com/felixr/docker-zsh-completion
10 servers=(${(f)"$(_call_program commands scw _completion servers-names)"})
11 _describe -t servers "servers" servers
14 __scw_stoppedservers() {
18 __scw_runningservers() {
29 images=(${(f)"$(_call_program commands scw _completion images-names)"})
30 _describe -t images "images" images
33 __scw_images_and_snapshots () {
41 snapshots=(${(f)"$(_call_program commands scw _completion --prefix snapshots-names)"})
42 _describe -t snapshots "snapshots" snapshots
45 __scw_bootscripts () {
47 declare -a bootscripts
48 bootscripts=(${(f)"$(_call_program commands scw _completion bootscripts-names)"})
49 _describe -t bootscripts "bootscripts" bootscripts
56 __scw_repositories_with_tags() {
61 # declare -a scwsearch
63 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
64 if [[ -z "$cache_policy" ]]; then
65 zstyle ":completion:${curcontext}:" cache-policy __scw_caching_policy
68 local searchterm cachename
69 searchterm="${words[$CURRENT]%/}"
70 cachename=_scw-search-$searchterm
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
80 _wanted scwsearch expl 'available images' compadd -a result
83 __scw_caching_policy()
85 oldp=( "$1"(Nmh+1) ) # 1 hour
90 __scw_repositories () {
95 # local -a _scw_subcommands
98 zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
99 if [[ -z "$cache_policy" ]]; then
100 zstyle ":completion:${curcontext}:" cache-policy __scw_caching_policy
103 if ( [[ ${+_scw_subcommands} -eq 0 ]] || _cache_invalid scw_subcommands) \
104 && ! _retrieve_cache scw_subcommands;
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
112 _describe -t scw-commands "scw command" _scw_subcommands
115 __scw_subcommand () {
116 local -a _command_args
120 '--no-stdin[Do not attach stdin]' \
121 ':servers:__scw_runningservers'
125 {-v,--volume=0}'[Volume slot]:volume: ' \
126 ':server:__scw_servers' \
127 ':repository:__scw_repositories_with_tags'
135 if compset -P '*:'; then
138 __scw_servers -qS ":"
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
163 '--no-trunc[Do not truncate output]' \
164 {-q,--quiet}'[Only show numeric IDs]' \
165 '*:images:__scw_images'
169 {-a,--all}'[Show all images]' \
170 '--no-trunc[Do not truncate output]' \
171 {-q,--quiet}'[Only show numeric IDs]' \
172 ':repository:__scw_repositories'
178 {-f,--format=-}'[Format the output using the given go template]:template: ' \
179 '*:servers:__scw_servers'
183 '*:servers:__scw_runningservers'
187 {-o,--organization=-}'[Organization]:organization: ' \
188 {-t,--token=-}'[Token]:token: ' \
197 '*:servers:__scw_servers'
201 '1:servers:__scw_runningservers' \
206 {-T,--timeout=0}'[Set timeout values to seconds]' \
207 {-w,--wait}'[Wait for SSH to be ready]' \
208 '*:servers:__scw_stoppedservers'
212 '*:servers:__scw_stoppedservers'
216 '*:images:__scw_images'
220 '*:servers:__scw_runningservers'
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'
230 '1:servers:__scw_runningservers' \
231 '(-)*:: :->ps-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]'
248 {-f,--force}'[force]'\
249 ':image:__scw_images'\
250 ':repository:__scw_repositories_with_tags'
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'
265 if compset -P '*:'; then
266 _wanted alias expl 'Alias' compadd -E ""
268 __scw_runningservers -qS ":"
275 ':old name:__scw_servers' \
280 '--no-trunc[Do not truncate output]' \
284 _arguments '*:servers:__scw_runningservers'
287 _arguments ':subcommand:__scw_commands'
290 _message 'Unknown sub command'
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
303 local curcontext="$curcontext" state line
307 '-H[tcp://host:port to bind/connect to]:socket: ' \
309 '(-)*:: :->option-or-argument'
311 if (( CURRENT == 1 )); then
319 curcontext=${curcontext%:*:*}:scw-$words[1]:
330 # indent-tabs-mode: nil
333 # vim: ft=zsh sw=4 ts=4 et