docker 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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_q() {
  24. docker 2>/dev/null "$@"
  25. }
  26. __docker_containers_all()
  27. {
  28. local containers="$( __docker_q ps -a -q )"
  29. local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
  30. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  31. }
  32. __docker_containers_running()
  33. {
  34. local containers="$( __docker_q ps -q )"
  35. local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
  36. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  37. }
  38. __docker_containers_stopped()
  39. {
  40. local containers="$( { __docker_q ps -a -q; __docker_q ps -q; } | sort | uniq -u )"
  41. local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
  42. COMPREPLY=( $( compgen -W "$names $containers" -- "$cur" ) )
  43. }
  44. __docker_image_repos()
  45. {
  46. local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )"
  47. COMPREPLY=( $( compgen -W "$repos" -- "$cur" ) )
  48. }
  49. __docker_image_repos_and_tags()
  50. {
  51. local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )"
  52. local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
  53. COMPREPLY=( $( compgen -W "$repos $images" -- "$cur" ) )
  54. __ltrim_colon_completions "$cur"
  55. }
  56. __docker_image_repos_and_tags_and_ids()
  57. {
  58. local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )"
  59. local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
  60. local ids="$( __docker_q images -a -q )"
  61. COMPREPLY=( $( compgen -W "$repos $images $ids" -- "$cur" ) )
  62. __ltrim_colon_completions "$cur"
  63. }
  64. __docker_containers_and_images()
  65. {
  66. local containers="$( __docker_q ps -a -q )"
  67. local names="$( __docker_q inspect --format '{{.Name}}' $containers | sed 's,^/,,' )"
  68. local repos="$( __docker_q images | awk 'NR>1{print $1}' | grep -v '^<none>$' )"
  69. local images="$( __docker_q images | awk 'NR>1{print $1":"$2}' | grep -v '^<none>:' )"
  70. local ids="$( __docker_q images -a -q )"
  71. COMPREPLY=( $( compgen -W "$containers $names $repos $images $ids" -- "$cur" ) )
  72. __ltrim_colon_completions "$cur"
  73. }
  74. __docker_pos_first_nonflag()
  75. {
  76. local argument_flags=$1
  77. local counter=$cpos
  78. while [ $counter -le $cword ]; do
  79. if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
  80. (( counter++ ))
  81. else
  82. case "${words[$counter]}" in
  83. -*)
  84. ;;
  85. *)
  86. break
  87. ;;
  88. esac
  89. fi
  90. (( counter++ ))
  91. done
  92. echo $counter
  93. }
  94. _docker_docker()
  95. {
  96. case "$prev" in
  97. -H)
  98. return
  99. ;;
  100. *)
  101. ;;
  102. esac
  103. case "$cur" in
  104. -*)
  105. COMPREPLY=( $( compgen -W "-H" -- "$cur" ) )
  106. ;;
  107. *)
  108. COMPREPLY=( $( compgen -W "$commands help" -- "$cur" ) )
  109. ;;
  110. esac
  111. }
  112. _docker_attach()
  113. {
  114. case "$cur" in
  115. -*)
  116. COMPREPLY=( $( compgen -W "--no-stdin --sig-proxy" -- "$cur" ) )
  117. ;;
  118. *)
  119. local counter="$(__docker_pos_first_nonflag)"
  120. if [ $cword -eq $counter ]; then
  121. __docker_containers_running
  122. fi
  123. ;;
  124. esac
  125. }
  126. _docker_build()
  127. {
  128. case "$prev" in
  129. -t|--tag)
  130. __docker_image_repos_and_tags
  131. return
  132. ;;
  133. *)
  134. ;;
  135. esac
  136. case "$cur" in
  137. -*)
  138. COMPREPLY=( $( compgen -W "-t --tag -q --quiet --no-cache --rm" -- "$cur" ) )
  139. ;;
  140. *)
  141. local counter="$(__docker_pos_first_nonflag '-t|--tag')"
  142. if [ $cword -eq $counter ]; then
  143. _filedir
  144. fi
  145. ;;
  146. esac
  147. }
  148. _docker_commit()
  149. {
  150. case "$prev" in
  151. -m|--message|-a|--author|--run)
  152. return
  153. ;;
  154. *)
  155. ;;
  156. esac
  157. case "$cur" in
  158. -*)
  159. COMPREPLY=( $( compgen -W "-m --message -a --author --run" -- "$cur" ) )
  160. ;;
  161. *)
  162. local counter=$(__docker_pos_first_nonflag '-m|--message|-a|--author|--run')
  163. if [ $cword -eq $counter ]; then
  164. __docker_containers_all
  165. return
  166. fi
  167. (( counter++ ))
  168. if [ $cword -eq $counter ]; then
  169. __docker_image_repos_and_tags
  170. return
  171. fi
  172. ;;
  173. esac
  174. }
  175. _docker_cp()
  176. {
  177. local counter=$(__docker_pos_first_nonflag)
  178. if [ $cword -eq $counter ]; then
  179. case "$cur" in
  180. *:)
  181. return
  182. ;;
  183. *)
  184. __docker_containers_all
  185. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  186. compopt -o nospace
  187. return
  188. ;;
  189. esac
  190. fi
  191. (( counter++ ))
  192. if [ $cword -eq $counter ]; then
  193. _filedir
  194. return
  195. fi
  196. }
  197. _docker_diff()
  198. {
  199. local counter=$(__docker_pos_first_nonflag)
  200. if [ $cword -eq $counter ]; then
  201. __docker_containers_all
  202. fi
  203. }
  204. _docker_events()
  205. {
  206. case "$prev" in
  207. --since)
  208. return
  209. ;;
  210. *)
  211. ;;
  212. esac
  213. case "$cur" in
  214. -*)
  215. COMPREPLY=( $( compgen -W "--since" -- "$cur" ) )
  216. ;;
  217. *)
  218. ;;
  219. esac
  220. }
  221. _docker_export()
  222. {
  223. local counter=$(__docker_pos_first_nonflag)
  224. if [ $cword -eq $counter ]; then
  225. __docker_containers_all
  226. fi
  227. }
  228. _docker_help()
  229. {
  230. local counter=$(__docker_pos_first_nonflag)
  231. if [ $cword -eq $counter ]; then
  232. COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
  233. fi
  234. }
  235. _docker_history()
  236. {
  237. case "$cur" in
  238. -*)
  239. COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) )
  240. ;;
  241. *)
  242. local counter=$(__docker_pos_first_nonflag)
  243. if [ $cword -eq $counter ]; then
  244. __docker_image_repos_and_tags_and_ids
  245. fi
  246. ;;
  247. esac
  248. }
  249. _docker_images()
  250. {
  251. case "$cur" in
  252. -*)
  253. COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) )
  254. ;;
  255. *)
  256. local counter=$(__docker_pos_first_nonflag)
  257. if [ $cword -eq $counter ]; then
  258. __docker_image_repos
  259. fi
  260. ;;
  261. esac
  262. }
  263. _docker_import()
  264. {
  265. local counter=$(__docker_pos_first_nonflag)
  266. if [ $cword -eq $counter ]; then
  267. return
  268. fi
  269. (( counter++ ))
  270. if [ $cword -eq $counter ]; then
  271. __docker_image_repos_and_tags
  272. return
  273. fi
  274. }
  275. _docker_info()
  276. {
  277. return
  278. }
  279. _docker_insert()
  280. {
  281. local counter=$(__docker_pos_first_nonflag)
  282. if [ $cword -eq $counter ]; then
  283. __docker_image_repos_and_tags_and_ids
  284. fi
  285. }
  286. _docker_inspect()
  287. {
  288. case "$prev" in
  289. -f|--format)
  290. return
  291. ;;
  292. *)
  293. ;;
  294. esac
  295. case "$cur" in
  296. -*)
  297. COMPREPLY=( $( compgen -W "-f --format" -- "$cur" ) )
  298. ;;
  299. *)
  300. __docker_containers_and_images
  301. ;;
  302. esac
  303. }
  304. _docker_kill()
  305. {
  306. __docker_containers_running
  307. }
  308. _docker_load()
  309. {
  310. return
  311. }
  312. _docker_login()
  313. {
  314. case "$prev" in
  315. -u|--username|-p|--password|-e|--email)
  316. return
  317. ;;
  318. *)
  319. ;;
  320. esac
  321. case "$cur" in
  322. -*)
  323. COMPREPLY=( $( compgen -W "-u --username -p --password -e --email" -- "$cur" ) )
  324. ;;
  325. *)
  326. ;;
  327. esac
  328. }
  329. _docker_logs()
  330. {
  331. case "$cur" in
  332. -*)
  333. COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) )
  334. ;;
  335. *)
  336. local counter=$(__docker_pos_first_nonflag)
  337. if [ $cword -eq $counter ]; then
  338. __docker_containers_all
  339. fi
  340. ;;
  341. esac
  342. }
  343. _docker_port()
  344. {
  345. local counter=$(__docker_pos_first_nonflag)
  346. if [ $cword -eq $counter ]; then
  347. __docker_containers_all
  348. fi
  349. }
  350. _docker_ps()
  351. {
  352. case "$prev" in
  353. --since-id|--before-id)
  354. COMPREPLY=( $( compgen -W "$( __docker_q ps -a -q )" -- "$cur" ) )
  355. # TODO replace this with __docker_containers_all
  356. # see https://github.com/dotcloud/docker/issues/3565
  357. return
  358. ;;
  359. -n)
  360. return
  361. ;;
  362. *)
  363. ;;
  364. esac
  365. case "$cur" in
  366. -*)
  367. COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since-id --before-id -n" -- "$cur" ) )
  368. ;;
  369. *)
  370. ;;
  371. esac
  372. }
  373. _docker_pull()
  374. {
  375. case "$prev" in
  376. -t|--tag)
  377. return
  378. ;;
  379. *)
  380. ;;
  381. esac
  382. case "$cur" in
  383. -*)
  384. COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) )
  385. ;;
  386. *)
  387. local counter=$(__docker_pos_first_nonflag '-t|--tag')
  388. if [ $cword -eq $counter ]; then
  389. __docker_image_repos_and_tags
  390. fi
  391. ;;
  392. esac
  393. }
  394. _docker_push()
  395. {
  396. local counter=$(__docker_pos_first_nonflag)
  397. if [ $cword -eq $counter ]; then
  398. __docker_image_repos
  399. # TODO replace this with __docker_image_repos_and_tags
  400. # see https://github.com/dotcloud/docker/issues/3411
  401. fi
  402. }
  403. _docker_restart()
  404. {
  405. case "$prev" in
  406. -t|--time)
  407. return
  408. ;;
  409. *)
  410. ;;
  411. esac
  412. case "$cur" in
  413. -*)
  414. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  415. ;;
  416. *)
  417. __docker_containers_all
  418. ;;
  419. esac
  420. }
  421. _docker_rm()
  422. {
  423. case "$cur" in
  424. -*)
  425. COMPREPLY=( $( compgen -W "-v --volumes -l --link" -- "$cur" ) )
  426. ;;
  427. *)
  428. __docker_containers_stopped
  429. ;;
  430. esac
  431. }
  432. _docker_rmi()
  433. {
  434. __docker_image_repos_and_tags_and_ids
  435. }
  436. _docker_run()
  437. {
  438. case "$prev" in
  439. --cidfile)
  440. _filedir
  441. ;;
  442. --volumes-from)
  443. __docker_containers_all
  444. ;;
  445. -v|--volume)
  446. # TODO something magical with colons and _filedir ?
  447. return
  448. ;;
  449. -e|--env)
  450. COMPREPLY=( $( compgen -e -- "$cur" ) )
  451. return
  452. ;;
  453. --entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|-c|--cpu-shares|-n|--name|-a|--attach|--link|-p|--publish|--expose|--dns|--lxc-conf)
  454. return
  455. ;;
  456. *)
  457. ;;
  458. esac
  459. case "$cur" in
  460. -*)
  461. COMPREPLY=( $( compgen -W "--rm -d --detach -n --networking --privileged -P --publish-all -i --interactive -t --tty --cidfile --entrypoint -h --hostname -m --memory -u --user -w --workdir -c --cpu-shares --sig-proxy --name -a --attach -v --volume --link -e --env -p --publish --expose --dns --volumes-from --lxc-conf" -- "$cur" ) )
  462. ;;
  463. *)
  464. local counter=$(__docker_pos_first_nonflag '--cidfile|--volumes-from|-v|--volume|-e|--env|--entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|-c|--cpu-shares|-n|--name|-a|--attach|--link|-p|--publish|--expose|--dns|--lxc-conf')
  465. if [ $cword -eq $counter ]; then
  466. __docker_image_repos_and_tags_and_ids
  467. fi
  468. ;;
  469. esac
  470. }
  471. _docker_save()
  472. {
  473. local counter=$(__docker_pos_first_nonflag)
  474. if [ $cword -eq $counter ]; then
  475. __docker_image_repos_and_tags_and_ids
  476. fi
  477. }
  478. _docker_search()
  479. {
  480. case "$prev" in
  481. -s|--stars)
  482. return
  483. ;;
  484. *)
  485. ;;
  486. esac
  487. case "$cur" in
  488. -*)
  489. COMPREPLY=( $( compgen -W "--no-trunc -t --trusted -s --stars" -- "$cur" ) )
  490. ;;
  491. *)
  492. ;;
  493. esac
  494. }
  495. _docker_start()
  496. {
  497. case "$cur" in
  498. -*)
  499. COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
  500. ;;
  501. *)
  502. __docker_containers_stopped
  503. ;;
  504. esac
  505. }
  506. _docker_stop()
  507. {
  508. case "$prev" in
  509. -t|--time)
  510. return
  511. ;;
  512. *)
  513. ;;
  514. esac
  515. case "$cur" in
  516. -*)
  517. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  518. ;;
  519. *)
  520. __docker_containers_running
  521. ;;
  522. esac
  523. }
  524. _docker_tag()
  525. {
  526. case "$cur" in
  527. -*)
  528. COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
  529. ;;
  530. *)
  531. local counter=$(__docker_pos_first_nonflag)
  532. if [ $cword -eq $counter ]; then
  533. __docker_image_repos_and_tags
  534. return
  535. fi
  536. (( counter++ ))
  537. if [ $cword -eq $counter ]; then
  538. __docker_image_repos_and_tags
  539. return
  540. fi
  541. ;;
  542. esac
  543. }
  544. _docker_top()
  545. {
  546. local counter=$(__docker_pos_first_nonflag)
  547. if [ $cword -eq $counter ]; then
  548. __docker_containers_running
  549. fi
  550. }
  551. _docker_version()
  552. {
  553. return
  554. }
  555. _docker_wait()
  556. {
  557. __docker_containers_all
  558. }
  559. _docker()
  560. {
  561. local commands="
  562. attach
  563. build
  564. commit
  565. cp
  566. diff
  567. events
  568. export
  569. history
  570. images
  571. import
  572. info
  573. insert
  574. inspect
  575. kill
  576. load
  577. login
  578. logs
  579. port
  580. ps
  581. pull
  582. push
  583. restart
  584. rm
  585. rmi
  586. run
  587. save
  588. search
  589. start
  590. stop
  591. tag
  592. top
  593. version
  594. wait
  595. "
  596. COMPREPLY=()
  597. local cur prev words cword
  598. _get_comp_words_by_ref -n : cur prev words cword
  599. local command='docker'
  600. local counter=1
  601. while [ $counter -lt $cword ]; do
  602. case "${words[$counter]}" in
  603. -H)
  604. (( counter++ ))
  605. ;;
  606. -*)
  607. ;;
  608. *)
  609. command="${words[$counter]}"
  610. cpos=$counter
  611. (( cpos++ ))
  612. break
  613. ;;
  614. esac
  615. (( counter++ ))
  616. done
  617. local completions_func=_docker_${command}
  618. declare -F $completions_func >/dev/null && $completions_func
  619. return 0
  620. }
  621. complete -F _docker docker