docker 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 and names
  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. __docker_containers_all()
  24. {
  25. local containers
  26. containers="$( docker ps -a -q )"
  27. names="$( docker inspect -format '{{.Name}}' $containers | sed 's,^/,,' )"
  28. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  29. }
  30. __docker_containers_running()
  31. {
  32. local containers
  33. containers="$( docker ps -q )"
  34. names="$( docker inspect -format '{{.Name}}' $containers | sed 's,^/,,' )"
  35. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  36. }
  37. __docker_containers_stopped()
  38. {
  39. local containers
  40. containers="$( comm -13 <(docker ps -q | sort -u) <(docker ps -a -q | sort -u) )"
  41. names="$( docker inspect -format '{{.Name}}' $containers | sed 's,^/,,' )"
  42. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  43. }
  44. __docker_image_repos()
  45. {
  46. local repos
  47. repos="$( docker images | awk 'NR>1{print $1}' )"
  48. COMPREPLY=( $( compgen -W "$repos" -- "$cur" ) )
  49. }
  50. __docker_images()
  51. {
  52. local images
  53. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  54. COMPREPLY=( $( compgen -W "$images" -- "$cur" ) )
  55. __ltrim_colon_completions "$cur"
  56. }
  57. __docker_image_repos_and_tags()
  58. {
  59. local repos images
  60. repos="$( docker images | awk 'NR>1{print $1}' )"
  61. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  62. COMPREPLY=( $( compgen -W "$repos $images" -- "$cur" ) )
  63. __ltrim_colon_completions "$cur"
  64. }
  65. __docker_containers_and_images()
  66. {
  67. local containers images
  68. containers="$( docker ps -a -q )"
  69. names="$( docker inspect -format '{{.Name}}' $containers | sed 's,^/,,' )"
  70. images="$( docker images | awk 'NR>1{print $1":"$2}' )"
  71. COMPREPLY=( $( compgen -W "$images $names $containers" -- "$cur" ) )
  72. __ltrim_colon_completions "$cur"
  73. }
  74. _docker_docker()
  75. {
  76. case "$prev" in
  77. -H)
  78. return
  79. ;;
  80. *)
  81. ;;
  82. esac
  83. case "$cur" in
  84. -*)
  85. COMPREPLY=( $( compgen -W "-H" -- "$cur" ) )
  86. ;;
  87. *)
  88. COMPREPLY=( $( compgen -W "$commands help" -- "$cur" ) )
  89. ;;
  90. esac
  91. }
  92. _docker_attach()
  93. {
  94. if [ $cpos -eq $cword ]; then
  95. __docker_containers_running
  96. fi
  97. }
  98. _docker_build()
  99. {
  100. case "$prev" in
  101. -t)
  102. return
  103. ;;
  104. *)
  105. ;;
  106. esac
  107. case "$cur" in
  108. -*)
  109. COMPREPLY=( $( compgen -W "-no-cache -t -q -rm" -- "$cur" ) )
  110. ;;
  111. *)
  112. _filedir
  113. ;;
  114. esac
  115. }
  116. _docker_commit()
  117. {
  118. case "$prev" in
  119. -author|-m|-run)
  120. return
  121. ;;
  122. *)
  123. ;;
  124. esac
  125. case "$cur" in
  126. -*)
  127. COMPREPLY=( $( compgen -W "-author -m -run" -- "$cur" ) )
  128. ;;
  129. *)
  130. local counter=$cpos
  131. while [ $counter -le $cword ]; do
  132. case "${words[$counter]}" in
  133. -author|-m|-run)
  134. (( counter++ ))
  135. ;;
  136. -*)
  137. ;;
  138. *)
  139. break
  140. ;;
  141. esac
  142. (( counter++ ))
  143. done
  144. if [ $counter -eq $cword ]; then
  145. __docker_containers_all
  146. fi
  147. ;;
  148. esac
  149. }
  150. _docker_cp()
  151. {
  152. if [ $cpos -eq $cword ]; then
  153. __docker_containers_all
  154. else
  155. _filedir
  156. fi
  157. }
  158. _docker_diff()
  159. {
  160. if [ $cpos -eq $cword ]; then
  161. __docker_containers_all
  162. fi
  163. }
  164. _docker_events()
  165. {
  166. case "$prev" in
  167. -since)
  168. return
  169. ;;
  170. *)
  171. ;;
  172. esac
  173. case "$cur" in
  174. -*)
  175. COMPREPLY=( $( compgen -W "-since" -- "$cur" ) )
  176. ;;
  177. *)
  178. ;;
  179. esac
  180. }
  181. _docker_export()
  182. {
  183. if [ $cpos -eq $cword ]; then
  184. __docker_containers_all
  185. fi
  186. }
  187. _docker_help()
  188. {
  189. if [ $cpos -eq $cword ]; then
  190. COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
  191. fi
  192. }
  193. _docker_history()
  194. {
  195. if [ $cpos -eq $cword ]; then
  196. __docker_image_repos_and_tags
  197. fi
  198. }
  199. _docker_images()
  200. {
  201. case "$cur" in
  202. -*)
  203. COMPREPLY=( $( compgen -W "-a -notrunc -q -viz" -- "$cur" ) )
  204. ;;
  205. *)
  206. local counter=$cpos
  207. while [ $counter -le $cword ]; do
  208. case "${words[$counter]}" in
  209. -*)
  210. ;;
  211. *)
  212. break
  213. ;;
  214. esac
  215. (( counter++ ))
  216. done
  217. if [ $counter -eq $cword ]; then
  218. __docker_image_repos
  219. fi
  220. ;;
  221. esac
  222. }
  223. _docker_import()
  224. {
  225. return
  226. }
  227. _docker_info()
  228. {
  229. return
  230. }
  231. _docker_insert()
  232. {
  233. if [ $cpos -eq $cword ]; then
  234. __docker_image_repos_and_tags
  235. fi
  236. }
  237. _docker_inspect()
  238. {
  239. __docker_containers_and_images
  240. }
  241. _docker_kill()
  242. {
  243. __docker_containers_running
  244. }
  245. _docker_login()
  246. {
  247. case "$prev" in
  248. -e|-p|-u)
  249. return
  250. ;;
  251. *)
  252. ;;
  253. esac
  254. case "$cur" in
  255. -*)
  256. COMPREPLY=( $( compgen -W "-e -p -u" -- "$cur" ) )
  257. ;;
  258. *)
  259. ;;
  260. esac
  261. }
  262. _docker_logs()
  263. {
  264. if [ $cpos -eq $cword ]; then
  265. __docker_containers_all
  266. fi
  267. }
  268. _docker_port()
  269. {
  270. if [ $cpos -eq $cword ]; then
  271. __docker_containers_all
  272. fi
  273. }
  274. _docker_ps()
  275. {
  276. case "$prev" in
  277. -beforeId|-n|-sinceId)
  278. return
  279. ;;
  280. *)
  281. ;;
  282. esac
  283. case "$cur" in
  284. -*)
  285. COMPREPLY=( $( compgen -W "-a -beforeId -l -n -notrunc -q -s -sinceId" -- "$cur" ) )
  286. ;;
  287. *)
  288. ;;
  289. esac
  290. }
  291. _docker_pull()
  292. {
  293. case "$prev" in
  294. -t)
  295. return
  296. ;;
  297. *)
  298. ;;
  299. esac
  300. case "$cur" in
  301. -*)
  302. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  303. ;;
  304. *)
  305. ;;
  306. esac
  307. }
  308. _docker_push()
  309. {
  310. __docker_image_repos
  311. }
  312. _docker_restart()
  313. {
  314. case "$prev" in
  315. -t)
  316. return
  317. ;;
  318. *)
  319. ;;
  320. esac
  321. case "$cur" in
  322. -*)
  323. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  324. ;;
  325. *)
  326. __docker_containers_all
  327. ;;
  328. esac
  329. }
  330. _docker_rm()
  331. {
  332. case "$cur" in
  333. -*)
  334. COMPREPLY=( $( compgen -W "-v" -- "$cur" ) )
  335. ;;
  336. *)
  337. __docker_containers_stopped
  338. ;;
  339. esac
  340. }
  341. _docker_rmi()
  342. {
  343. __docker_image_repos_and_tags
  344. }
  345. _docker_run()
  346. {
  347. case "$prev" in
  348. -cidfile)
  349. _filedir
  350. ;;
  351. -volumes-from)
  352. __docker_containers_all
  353. ;;
  354. -a|-c|-dns|-e|-entrypoint|-h|-lxc-conf|-m|-p|-u|-v|-w)
  355. return
  356. ;;
  357. *)
  358. ;;
  359. esac
  360. case "$cur" in
  361. -*)
  362. COMPREPLY=( $( compgen -W "-a -c -cidfile -d -dns -e -entrypoint -h -i -lxc-conf -m -n -p -privileged -t -u -v -volumes-from -w" -- "$cur" ) )
  363. ;;
  364. *)
  365. local counter=$cpos
  366. while [ $counter -le $cword ]; do
  367. case "${words[$counter]}" in
  368. -a|-c|-cidfile|-dns|-e|-entrypoint|-h|-lxc-conf|-m|-p|-u|-v|-volumes-from|-w)
  369. (( counter++ ))
  370. ;;
  371. -*)
  372. ;;
  373. *)
  374. break
  375. ;;
  376. esac
  377. (( counter++ ))
  378. done
  379. if [ $counter -eq $cword ]; then
  380. __docker_image_repos_and_tags
  381. fi
  382. ;;
  383. esac
  384. }
  385. _docker_search()
  386. {
  387. COMPREPLY=( $( compgen -W "-notrunc" "-stars" "-trusted" -- "$cur" ) )
  388. }
  389. _docker_start()
  390. {
  391. __docker_containers_stopped
  392. }
  393. _docker_stop()
  394. {
  395. case "$prev" in
  396. -t)
  397. return
  398. ;;
  399. *)
  400. ;;
  401. esac
  402. case "$cur" in
  403. -*)
  404. COMPREPLY=( $( compgen -W "-t" -- "$cur" ) )
  405. ;;
  406. *)
  407. __docker_containers_running
  408. ;;
  409. esac
  410. }
  411. _docker_tag()
  412. {
  413. COMPREPLY=( $( compgen -W "-f" -- "$cur" ) )
  414. }
  415. _docker_top()
  416. {
  417. if [ $cpos -eq $cword ]; then
  418. __docker_containers_running
  419. fi
  420. }
  421. _docker_version()
  422. {
  423. return
  424. }
  425. _docker_wait()
  426. {
  427. __docker_containers_all
  428. }
  429. _docker()
  430. {
  431. local cur prev words cword command="docker" counter=1 word cpos
  432. local commands="
  433. attach
  434. build
  435. commit
  436. cp
  437. diff
  438. events
  439. export
  440. history
  441. images
  442. import
  443. info
  444. insert
  445. inspect
  446. kill
  447. login
  448. logs
  449. port
  450. ps
  451. pull
  452. push
  453. restart
  454. rm
  455. rmi
  456. run
  457. search
  458. start
  459. stop
  460. tag
  461. top
  462. version
  463. wait
  464. "
  465. COMPREPLY=()
  466. _get_comp_words_by_ref -n : cur prev words cword
  467. while [ $counter -lt $cword ]; do
  468. word="${words[$counter]}"
  469. case "$word" in
  470. -H)
  471. (( counter++ ))
  472. ;;
  473. -*)
  474. ;;
  475. *)
  476. command="$word"
  477. cpos=$counter
  478. (( cpos++ ))
  479. break
  480. ;;
  481. esac
  482. (( counter++ ))
  483. done
  484. local completions_func=_docker_${command}
  485. declare -F $completions_func >/dev/null && $completions_func
  486. return 0
  487. }
  488. complete -F _docker docker