Blame view

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