docker 7.4 KB

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