my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
9.3 KiB

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