docker.bash 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #!bash
  2. #
  3. # bash completion file for core docker commands
  4. #
  5. # This script provides supports completion of:
  6. # - commands and their options
  7. # - container ids
  8. # - image repos and tags
  9. # - filepaths
  10. #
  11. # To enable the completions either:
  12. # - place this file in /etc/bash_completion.d
  13. # or
  14. # - copy this file and add the line below to your .bashrc after
  15. # bash completion features are loaded
  16. # . docker.bash
  17. #
  18. # Note:
  19. # Currently, the completions will not work if the docker daemon is not
  20. # bound to the default communication port/socket
  21. # If the docker daemon is using a unix socket for communication your user
  22. # must have access to the socket for the completions to function correctly
  23. have docker && {
  24. __docker_containers_all()
  25. {
  26. local containers
  27. containers="$( docker ps -a -q )"
  28. COMPREPLY=( $( compgen -W "$containers" -- "$cur" ) )
  29. }
  30. __docker_containers_running()
  31. {
  32. local containers
  33. containers="$( docker ps -q )"
  34. COMPREPLY=( $( compgen -W "$containers" -- "$cur" ) )
  35. }
  36. __docker_containers_stopped()
  37. {
  38. local containers
  39. containers="$( comm -13 <(docker ps -q | sort -u) <(docker ps -a -q | sort -u) )"
  40. COMPREPLY=( $( compgen -W "$containers" -- "$cur" ) )
  41. }
  42. __docker_image_repos()
  43. {
  44. local repos
  45. repos="$( docker images | awk 'NR>1{print $1}' )"
  46. COMPREPLY=( $( compgen -W "$repos" -- "$cur" ) )
  47. }
  48. __docker_images()
  49. {
  50. local images
  51. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  52. COMPREPLY=( $( compgen -W "$images" -- "$cur" ) )
  53. __ltrim_colon_completions "$cur"
  54. }
  55. __docker_image_repos_and_tags()
  56. {
  57. local repos images
  58. repos="$( docker images | awk 'NR>1{print $1}' )"
  59. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  60. COMPREPLY=( $( compgen -W "$repos $images" -- "$cur" ) )
  61. __ltrim_colon_completions "$cur"
  62. }
  63. __docker_containers_and_images()
  64. {
  65. local containers images
  66. containers="$( docker ps -a -q )"
  67. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  68. COMPREPLY=( $( compgen -W "$images $containers" -- "$cur" ) )
  69. __ltrim_colon_completions "$cur"
  70. }
  71. _docker_docker()
  72. {
  73. case "$prev" in
  74. -H)
  75. return
  76. ;;
  77. *)
  78. ;;
  79. esac
  80. case "$cur" in
  81. -*)
  82. COMPREPLY=( $( compgen -W "-H" -- "$cur" ) )
  83. ;;
  84. *)
  85. COMPREPLY=( $( compgen -W "$commands help" -- "$cur" ) )
  86. ;;
  87. esac
  88. }
  89. _docker_attach()
  90. {
  91. if [ $cpos -eq $cword ]; then
  92. __docker_containers_running
  93. fi
  94. }
  95. _docker_build()
  96. {
  97. case "$prev" in
  98. -t)
  99. return
  100. ;;
  101. *)
  102. ;;
  103. esac
  104. case "$cur" in
  105. -*)
  106. COMPREPLY=( $( compgen -W "-t -q" -- "$cur" ) )
  107. ;;
  108. *)
  109. _filedir
  110. ;;
  111. esac
  112. }
  113. _docker_commit()
  114. {
  115. case "$prev" in
  116. -author|-m|-run)
  117. return
  118. ;;
  119. *)
  120. ;;
  121. esac
  122. case "$cur" in
  123. -*)
  124. COMPREPLY=( $( compgen -W "-author -m -run" -- "$cur" ) )
  125. ;;
  126. *)
  127. __docker_containers_all
  128. ;;
  129. esac
  130. }
  131. _docker_diff()
  132. {
  133. if [ $cpos -eq $cword ]; then
  134. __docker_containers_all
  135. fi
  136. }
  137. _docker_events()
  138. {
  139. COMPREPLY=( $( compgen -W "-since" -- "$cur" ) )
  140. }
  141. _docker_export()
  142. {
  143. if [ $cpos -eq $cword ]; then
  144. __docker_containers_all
  145. fi
  146. }
  147. _docker_help()
  148. {
  149. if [ $cpos -eq $cword ]; then
  150. COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
  151. fi
  152. }
  153. _docker_history()
  154. {
  155. if [ $cpos -eq $cword ]; then
  156. __docker_image_repos_and_tags
  157. fi
  158. }
  159. _docker_images()
  160. {
  161. case "$cur" in
  162. -*)
  163. COMPREPLY=( $( compgen -W "-a -notrunc -q -viz" -- "$cur" ) )
  164. ;;
  165. *)
  166. local counter=$cpos
  167. while [ $counter -le $cword ]; do
  168. case "${words[$counter]}" in
  169. -*)
  170. ;;
  171. *)
  172. break
  173. ;;
  174. esac
  175. (( counter++ ))
  176. done
  177. if [ $counter -eq $cword ]; then
  178. __docker_image_repos
  179. fi
  180. ;;
  181. esac
  182. }
  183. _docker_import()
  184. {
  185. return
  186. }
  187. _docker_info()
  188. {
  189. return
  190. }
  191. _docker_insert()
  192. {
  193. if [ $cpos -eq $cword ]; then
  194. __docker_image_repos_and_tags
  195. fi
  196. }
  197. _docker_inspect()
  198. {
  199. __docker_containers_and_images
  200. }
  201. _docker_kill()
  202. {
  203. __docker_containers_running
  204. }
  205. _docker_login()
  206. {
  207. COMPREPLY=( $( compgen -W "-e -p -u" -- "$cur" ) )
  208. }
  209. _docker_logs()
  210. {
  211. if [ $cpos -eq $cword ]; then
  212. __docker_containers_all
  213. fi
  214. }
  215. _docker_port()
  216. {
  217. if [ $cpos -eq $cword ]; then
  218. __docker_containers_all
  219. fi
  220. }
  221. _docker_ps()
  222. {
  223. COMPREPLY=( $( compgen -W "-a -beforeId -l -n -notrunc -q -s -sinceId" -- "$cur" ) )
  224. }
  225. _docker_pull()
  226. {
  227. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  228. }
  229. _docker_push()
  230. {
  231. return
  232. }
  233. _docker_restart()
  234. {
  235. case "$prev" in
  236. -t)
  237. return
  238. ;;
  239. *)
  240. ;;
  241. esac
  242. case "$cur" in
  243. -*)
  244. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  245. ;;
  246. *)
  247. __docker_containers_all
  248. ;;
  249. esac
  250. }
  251. _docker_rm()
  252. {
  253. case "$cur" in
  254. -*)
  255. COMPREPLY=( $( compgen -W "-v" -- "$cur" ) )
  256. ;;
  257. *)
  258. __docker_containers_stopped
  259. ;;
  260. esac
  261. }
  262. _docker_rmi()
  263. {
  264. __docker_image_repos_and_tags
  265. }
  266. _docker_run()
  267. {
  268. case "$prev" in
  269. -cidfile)
  270. _filedir
  271. ;;
  272. -volumes-from)
  273. __docker_containers_all
  274. ;;
  275. -a|-c|-dns|-e|-entrypoint|-h|-m|-p|-u|-v)
  276. return
  277. ;;
  278. *)
  279. ;;
  280. esac
  281. case "$cur" in
  282. -*)
  283. COMPREPLY=( $( compgen -W "-a -c -cidfile -d -dns -e -entrypoint -h -i -m -n -p -t -u -v -volumes-from" -- "$cur" ) )
  284. ;;
  285. *)
  286. case "$cur" in
  287. -*)
  288. COMPREPLY=( $( compgen -W "-a -notrunc -q -viz" -- "$cur" ) )
  289. ;;
  290. *)
  291. local counter=$cpos
  292. while [ $counter -le $cword ]; do
  293. case "${words[$counter]}" in
  294. -a|-c|-cidfile|-dns|-e|-entrypoint|-h|-m|-p|-u|-v|-volumes-from)
  295. (( counter++ ))
  296. ;;
  297. -*)
  298. ;;
  299. *)
  300. break
  301. ;;
  302. esac
  303. (( counter++ ))
  304. done
  305. if [ $counter -eq $cword ]; then
  306. __docker_image_repos_and_tags
  307. fi
  308. ;;
  309. esac
  310. ;;
  311. esac
  312. }
  313. _docker_search()
  314. {
  315. COMPREPLY=( $( compgen -W "-notrunc" -- "$cur" ) )
  316. }
  317. _docker_start()
  318. {
  319. __docker_containers_stopped
  320. }
  321. _docker_stop()
  322. {
  323. case "$prev" in
  324. -t)
  325. return
  326. ;;
  327. *)
  328. ;;
  329. esac
  330. case "$cur" in
  331. -*)
  332. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  333. ;;
  334. *)
  335. __docker_containers_running
  336. ;;
  337. esac
  338. }
  339. _docker_tag()
  340. {
  341. COMPREPLY=( $( compgen -W "-f" -- "$cur" ) )
  342. }
  343. _docker_top()
  344. {
  345. if [ $cpos -eq $cword ]; then
  346. __docker_containers_running
  347. fi
  348. }
  349. _docker_version()
  350. {
  351. return
  352. }
  353. _docker_wait()
  354. {
  355. __docker_containers_all
  356. }
  357. _docker()
  358. {
  359. local cur prev words cword command="docker" counter=1 word cpos
  360. local commands="
  361. attach
  362. build
  363. commit
  364. diff
  365. events
  366. export
  367. history
  368. images
  369. import
  370. info
  371. insert
  372. inspect
  373. kill
  374. login
  375. logs
  376. port
  377. ps
  378. pull
  379. push
  380. restart
  381. rm
  382. rmi
  383. run
  384. search
  385. start
  386. stop
  387. tag
  388. top
  389. version
  390. wait
  391. "
  392. COMPREPLY=()
  393. _get_comp_words_by_ref -n : cur prev words cword
  394. while [ $counter -lt $cword ]; do
  395. word="${words[$counter]}"
  396. case "$word" in
  397. -H)
  398. (( counter++ ))
  399. ;;
  400. -*)
  401. ;;
  402. *)
  403. command="$word"
  404. cpos=$counter
  405. (( cpos++ ))
  406. break
  407. ;;
  408. esac
  409. (( counter++ ))
  410. done
  411. local completions_func=_docker_${command}
  412. declare -F $completions_func >/dev/null && $completions_func
  413. return 0
  414. }
  415. complete -F _docker docker
  416. }