_docker 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #compdef docker
  2. #
  3. # zsh completion for docker (http://docker.io)
  4. #
  5. # version: 0.2.2
  6. # author: Felix Riedel
  7. # license: BSD License
  8. # github: https://github.com/felixr/docker-zsh-completion
  9. #
  10. __parse_docker_list() {
  11. sed -e '/^ID/d' -e 's/[ ]\{2,\}/|/g' -e 's/ \([hdwm]\)\(inutes\|ays\|ours\|eeks\)/\1/' | awk ' BEGIN {FS="|"} { printf("%s:%7s, %s\n", $1, $4, $2)}'
  12. }
  13. __docker_stoppedcontainers() {
  14. local expl
  15. declare -a stoppedcontainers
  16. stoppedcontainers=(${(f)"$(docker ps -a | grep --color=never 'Exit' | __parse_docker_list )"})
  17. _describe -t containers-stopped "Stopped Containers" stoppedcontainers
  18. }
  19. __docker_runningcontainers() {
  20. local expl
  21. declare -a containers
  22. containers=(${(f)"$(docker ps | __parse_docker_list)"})
  23. _describe -t containers-active "Running Containers" containers
  24. }
  25. __docker_containers () {
  26. __docker_stoppedcontainers
  27. __docker_runningcontainers
  28. }
  29. __docker_images () {
  30. local expl
  31. declare -a images
  32. images=(${(f)"$(docker images | awk '(NR > 1){printf("%s\\:%s\n", $1,$2)}')"})
  33. images=($images ${(f)"$(docker images | awk '(NR > 1){printf("%s:%-15s in %s\n", $3,$2,$1)}')"})
  34. _describe -t docker-images "Images" images
  35. }
  36. __docker_tags() {
  37. local expl
  38. declare -a tags
  39. tags=(${(f)"$(docker images | awk '(NR>1){print $2}'| sort | uniq)"})
  40. _describe -t docker-tags "tags" tags
  41. }
  42. __docker_search() {
  43. # declare -a dockersearch
  44. local cache_policy
  45. zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
  46. if [[ -z "$cache_policy" ]]; then
  47. zstyle ":completion:${curcontext}:" cache-policy __docker_caching_policy
  48. fi
  49. local searchterm cachename
  50. searchterm="${words[$CURRENT]%/}"
  51. cachename=_docker-search-$searchterm
  52. local expl
  53. local -a result
  54. if ( [[ ${(P)+cachename} -eq 0 ]] || _cache_invalid ${cachename#_} ) \
  55. && ! _retrieve_cache ${cachename#_}; then
  56. _message "Searching for ${searchterm}..."
  57. result=(${(f)"$(docker search ${searchterm} | awk '(NR>2){print $1}')"})
  58. _store_cache ${cachename#_} result
  59. fi
  60. _wanted dockersearch expl 'Available images' compadd -a result
  61. }
  62. __docker_caching_policy()
  63. {
  64. # oldp=( "$1"(Nmh+24) ) # 24 hour
  65. oldp=( "$1"(Nmh+1) ) # 24 hour
  66. (( $#oldp ))
  67. }
  68. __docker_repositories () {
  69. local expl
  70. declare -a repos
  71. repos=(${(f)"$(docker images | sed -e '1d' -e 's/[ ].*//' | sort | uniq)"})
  72. _describe -t docker-repos "Repositories" repos
  73. }
  74. __docker_commands () {
  75. # local -a _docker_subcommands
  76. local cache_policy
  77. zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
  78. if [[ -z "$cache_policy" ]]; then
  79. zstyle ":completion:${curcontext}:" cache-policy __docker_caching_policy
  80. fi
  81. if ( [[ ${+_docker_subcommands} -eq 0 ]] || _cache_invalid docker_subcommands) \
  82. && ! _retrieve_cache docker_subcommands;
  83. then
  84. _docker_subcommands=(${${(f)"$(_call_program commands
  85. docker 2>&1 | sed -e '1,6d' -e '/^[ ]*$/d' -e 's/[ ]*\([^ ]\+\)\s*\([^ ].*\)/\1:\2/' )"}})
  86. _docker_subcommands=($_docker_subcommands 'help:Show help for a command')
  87. _store_cache docker_subcommands _docker_subcommands
  88. fi
  89. _describe -t docker-commands "docker command" _docker_subcommands
  90. }
  91. __docker_subcommand () {
  92. local -a _command_args
  93. case "$words[1]" in
  94. (attach|wait)
  95. _arguments ':containers:__docker_runningcontainers'
  96. ;;
  97. (build)
  98. _arguments \
  99. '-t=-:repository:__docker_repositories' \
  100. ':path or URL:_directories'
  101. ;;
  102. (commit)
  103. _arguments \
  104. ':container:__docker_containers' \
  105. ':repository:__docker_repositories' \
  106. ':tag: '
  107. ;;
  108. (diff|export|logs)
  109. _arguments '*:containers:__docker_containers'
  110. ;;
  111. (history)
  112. _arguments '*:images:__docker_images'
  113. ;;
  114. (images)
  115. _arguments \
  116. '-a[Show all images]' \
  117. ':repository:__docker_repositories'
  118. ;;
  119. (inspect)
  120. _arguments '*:containers:__docker_containers'
  121. ;;
  122. (history)
  123. _arguments ':images:__docker_images'
  124. ;;
  125. (insert)
  126. _arguments '1:containers:__docker_containers' \
  127. '2:URL:(http:// file://)' \
  128. '3:file:_files'
  129. ;;
  130. (kill)
  131. _arguments '*:containers:__docker_runningcontainers'
  132. ;;
  133. (port)
  134. _arguments '1:containers:__docker_runningcontainers'
  135. ;;
  136. (start)
  137. _arguments '*:containers:__docker_stoppedcontainers'
  138. ;;
  139. (rm)
  140. _arguments '-v[Remove the volumes associated to the container]' \
  141. '*:containers:__docker_stoppedcontainers'
  142. ;;
  143. (rmi)
  144. _arguments '-v[Remove the volumes associated to the container]' \
  145. '*:images:__docker_images'
  146. ;;
  147. (top)
  148. _arguments '1:containers:__docker_runningcontainers'
  149. ;;
  150. (restart|stop)
  151. _arguments '-t=-[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)' \
  152. '*:containers:__docker_runningcontainers'
  153. ;;
  154. (top)
  155. _arguments ':containers:__docker_runningcontainers'
  156. ;;
  157. (ps)
  158. _arguments '-a[Show all containers. Only running containers are shown by default]' \
  159. '-h[Show help]' \
  160. '--before-id=-[Show only container created before Id, include non-running one]:containers:__docker_containers' \
  161. '-n=-[Show n last created containers, include non-running one]:n:(1 5 10 25 50)'
  162. ;;
  163. (tag)
  164. _arguments \
  165. '-f[force]'\
  166. ':image:__docker_images'\
  167. ':repository:__docker_repositories' \
  168. ':tag:__docker_tags'
  169. ;;
  170. (run)
  171. _arguments \
  172. '-a=-[Attach to stdin, stdout or stderr]:toggle:(true false)' \
  173. '-c=-[CPU shares (relative weight)]:CPU shares: ' \
  174. '-d[Detached mode: leave the container running in the background]' \
  175. '*--dns=[Set custom dns servers]:dns server: ' \
  176. '*-e=[Set environment variables]:environment variable: ' \
  177. '--entrypoint=-[Overwrite the default entrypoint of the image]:entry point: ' \
  178. '-h=-[Container host name]:hostname:_hosts' \
  179. '-i[Keep stdin open even if not attached]' \
  180. '-m=-[Memory limit (in bytes)]:limit: ' \
  181. '*-p=-[Expose a container''s port to the host]:port:_ports' \
  182. '-t=-[Allocate a pseudo-tty]:toggle:(true false)' \
  183. '-u=-[Username or UID]:user:_users' \
  184. '*-v=-[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]:volume: '\
  185. '--volumes-from=-[Mount volumes from the specified container]:volume: ' \
  186. '(-):images:__docker_images' \
  187. '(-):command: _command_names -e' \
  188. '*::arguments: _normal'
  189. ;;
  190. (pull|search)
  191. _arguments ':name:__docker_search'
  192. ;;
  193. (help)
  194. _arguments ':subcommand:__docker_commands'
  195. ;;
  196. (*)
  197. _message 'Unknown sub command'
  198. esac
  199. }
  200. _docker () {
  201. local curcontext="$curcontext" state line
  202. typeset -A opt_args
  203. _arguments -C \
  204. '-H=-[tcp://host:port to bind/connect to]:socket: ' \
  205. '(-): :->command' \
  206. '(-)*:: :->option-or-argument'
  207. if (( CURRENT == 1 )); then
  208. fi
  209. case $state in
  210. (command)
  211. __docker_commands
  212. ;;
  213. (option-or-argument)
  214. curcontext=${curcontext%:*:*}:docker-$words[1]:
  215. __docker_subcommand
  216. ;;
  217. esac
  218. }
  219. _docker "$@"