_docker 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #compdef docker
  2. # Docker autocompletion for oh-my-zsh
  3. # Requires: Docker installed
  4. # Author : Azaan (@aeonazaan)
  5. # ----- Helper functions
  6. # Output a selectable list of all running docker containers
  7. __docker_containers() {
  8. declare -a cont_cmd
  9. cont_cmd=($(docker ps | awk 'NR>1{print $1":[CON("$1")"$2"("$3")]"}'))
  10. _describe 'containers' cont_cmd
  11. }
  12. # output a selectable list of all docker images
  13. __docker_images() {
  14. declare -a img_cmd
  15. img_cmd=($(docker images | awk 'NR>1{print $1}'))
  16. _describe 'images' img_cmd
  17. }
  18. # ----- Commands
  19. # Seperate function for each command, makes extension easier later
  20. # ---------------------------
  21. __attach() {
  22. __docker_containers
  23. }
  24. __build() {
  25. _arguments \
  26. '-q=false[Suppress verbose build output]' \
  27. '-t="[fuck to be applied to the resulting image in case of success]' \
  28. '*:files:_files'
  29. }
  30. __commit() {
  31. _arguments \
  32. '-author="[Author]' \
  33. '-m="[Commit message]' \
  34. '-run="[Config automatically applied when the image is run.\n]'
  35. __docker_containers
  36. }
  37. __diff() {
  38. __docker_containers
  39. }
  40. __export() {
  41. __docker_containers
  42. }
  43. __history() {
  44. __docker_images
  45. }
  46. __images() {
  47. _arguments \
  48. '-a[show all images]' \
  49. '-notrunc[dont truncate output]' \
  50. '-q[only show numeric IDs]' \
  51. '-viz[output graph in graphviz format]'
  52. __docker_images
  53. }
  54. __import() {
  55. _arguments '*:files:_files'
  56. }
  57. __info() {
  58. # no arguments
  59. }
  60. __insert() {
  61. __docker_images
  62. _arguments '*:files:_files'
  63. }
  64. __inspect() {
  65. __docker_images
  66. __docker_containers
  67. }
  68. __kill() {
  69. __docker_containers
  70. }
  71. __login() {
  72. _arguments \
  73. '-e="[email]' \
  74. '-p="[password]' \
  75. '-u="[username]' \
  76. }
  77. __logs() {
  78. __docker_containers
  79. }
  80. __port() {
  81. __docker_containers
  82. }
  83. __top() {
  84. __docker_containers
  85. }
  86. __ps() {
  87. _arguments \
  88. '-a[Show all containers. Only running containers are shown by default.]' \
  89. '-beforeId="[Show only container created before Id, include non-running ones.]' \
  90. '-l[Show only the latest created container, include non-running ones.]' \
  91. '-n=[Show n last created containers, include non-running ones.]' \
  92. '-notrurrrrnc[Dont truncate output]' \
  93. '-q[Only display numeric IDs]' \
  94. '-s[Display sizes]' \
  95. '-sinceId="[Show only containers created since Id, include non-running ones.]'
  96. }
  97. __pull() {
  98. _arguments '-t="[Download tagged image in repository]'
  99. }
  100. __push() {
  101. }
  102. __restart() {
  103. _arguments '-t=[number of seconds to try to stop before killing]'
  104. __docker_containers
  105. }
  106. __rm() {
  107. _arguments '-v[Remove the volumes associated to the container]'
  108. __docker_containers
  109. }
  110. __rmi() {
  111. __docker_images
  112. }
  113. __run() {
  114. _arguments \
  115. '-a=[Attach to stdin, stdout or stderr.]' \
  116. '-c=[CPU shares (relative weight)]' \
  117. '-d[Detached mode: leave the container running in the background]' \
  118. '-dns=[Set custom dns servers]' \
  119. '-e=[Set environment variables]' \
  120. '-entrypoint="[Overwrite the default entrypoint of the image]' \
  121. '-h="[Container host name]' \
  122. '-i[Keep stdin open even if not attached]' \
  123. '-m=[Memory limit (in bytes)]' \
  124. '-p=[Expose a containers port to the host (use docker port to see the actual mapping)]' \
  125. '-t[Allocate a pseudo-tty]' \
  126. '-u="[Username or UID]' \
  127. '-v=[Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)]' \
  128. '-volumes-from="[Mount volumes from the specified container]'
  129. __docker_images
  130. }
  131. __search() {
  132. _arguments '-notrunc[Dont truncate output]'
  133. }
  134. __start() {
  135. __docker_containers
  136. }
  137. __stop() {
  138. _arguments '-t=[number of seconds to try to stop before killing]'
  139. __docker_containers
  140. }
  141. __tag() {
  142. _arguments '-f[Force]'
  143. __docker_images
  144. }
  145. __version() {
  146. }
  147. __wait() {
  148. __docker_containers
  149. }
  150. # end commands ---------
  151. # ----------------------
  152. local -a _1st_arguments
  153. _1st_arguments=(
  154. "attach":"Attach to a running container"
  155. "build":"Build a container from a Dockerfile"
  156. "commit":"Create a new image from a container's changes"
  157. "diff":"Inspect changes on a container's filesystem"
  158. "export":"Stream the contents of a container as a tar archive"
  159. "history":"Show the history of an image"
  160. "images":"List images"
  161. "import":"Create a new filesystem image from the contents of a tarball"
  162. "info":"Display system-wide information"
  163. "insert":"Insert a file in an image"
  164. "inspect":"Return low-level information on a container"
  165. "kill":"Kill a running container"
  166. "login":"Register or Login to the docker registry server"
  167. "logs":"Fetch the logs of a container"
  168. "port":"Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"
  169. "top":"Lookup the running processes of a container"
  170. "ps":"List containers"
  171. "pull":"Pull an image or a repository from the docker registry server"
  172. "push":"Push an image or a repository to the docker registry server"
  173. "restart":"Restart a running container"
  174. "rm":"Remove one or more containers"
  175. "rmi":"Remove one or more images"
  176. "run":"Run a command in a new container"
  177. "search":"Search for an image in the docker index"
  178. "start":"Start a stopped container"
  179. "stop":"Stop a running container"
  180. "tag":"Tag an image into a repository"
  181. "version":"Show the docker version information"
  182. "wait":"Block until a container stops, then print its exit code"
  183. )
  184. _arguments '*:: :->command'
  185. if (( CURRENT == 1 )); then
  186. _describe -t commands "docker command" _1st_arguments
  187. return
  188. fi
  189. local -a _command_args
  190. case "$words[1]" in
  191. attach)
  192. __docker_containers ;;
  193. build)
  194. __build ;;
  195. commit)
  196. __commit ;;
  197. diff)
  198. __diff ;;
  199. export)
  200. __export ;;
  201. history)
  202. __history ;;
  203. images)
  204. __images ;;
  205. import)
  206. __import ;;
  207. info)
  208. __info ;;
  209. insert)
  210. __insert ;;
  211. inspect)
  212. __inspect ;;
  213. kill)
  214. __kill ;;
  215. login)
  216. __login ;;
  217. logs)
  218. __logs ;;
  219. port)
  220. __port ;;
  221. top)
  222. __top ;;
  223. ps)
  224. __ps ;;
  225. pull)
  226. __pull ;;
  227. push)
  228. __push ;;
  229. restart)
  230. __restart ;;
  231. rm)
  232. __rm ;;
  233. rmi)
  234. __rmi ;;
  235. run)
  236. __run ;;
  237. search)
  238. __search ;;
  239. start)
  240. __start ;;
  241. stop)
  242. __stop ;;
  243. tag)
  244. __tag ;;
  245. version)
  246. __version ;;
  247. wait)
  248. __wait ;;
  249. esac