docker 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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|--before)
  354. __docker_containers_all
  355. ;;
  356. -n)
  357. return
  358. ;;
  359. *)
  360. ;;
  361. esac
  362. case "$cur" in
  363. -*)
  364. COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since --before -n" -- "$cur" ) )
  365. ;;
  366. *)
  367. ;;
  368. esac
  369. }
  370. _docker_pull()
  371. {
  372. case "$prev" in
  373. -t|--tag)
  374. return
  375. ;;
  376. *)
  377. ;;
  378. esac
  379. case "$cur" in
  380. -*)
  381. COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) )
  382. ;;
  383. *)
  384. local counter=$(__docker_pos_first_nonflag '-t|--tag')
  385. if [ $cword -eq $counter ]; then
  386. __docker_image_repos_and_tags
  387. fi
  388. ;;
  389. esac
  390. }
  391. _docker_push()
  392. {
  393. local counter=$(__docker_pos_first_nonflag)
  394. if [ $cword -eq $counter ]; then
  395. __docker_image_repos
  396. # TODO replace this with __docker_image_repos_and_tags
  397. # see https://github.com/dotcloud/docker/issues/3411
  398. fi
  399. }
  400. _docker_restart()
  401. {
  402. case "$prev" in
  403. -t|--time)
  404. return
  405. ;;
  406. *)
  407. ;;
  408. esac
  409. case "$cur" in
  410. -*)
  411. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  412. ;;
  413. *)
  414. __docker_containers_all
  415. ;;
  416. esac
  417. }
  418. _docker_rm()
  419. {
  420. case "$cur" in
  421. -*)
  422. COMPREPLY=( $( compgen -W "-v --volumes -l --link" -- "$cur" ) )
  423. ;;
  424. *)
  425. __docker_containers_stopped
  426. ;;
  427. esac
  428. }
  429. _docker_rmi()
  430. {
  431. __docker_image_repos_and_tags_and_ids
  432. }
  433. _docker_run()
  434. {
  435. case "$prev" in
  436. --cidfile)
  437. _filedir
  438. ;;
  439. --volumes-from)
  440. __docker_containers_all
  441. ;;
  442. -v|--volume)
  443. # TODO something magical with colons and _filedir ?
  444. return
  445. ;;
  446. -e|--env)
  447. COMPREPLY=( $( compgen -e -- "$cur" ) )
  448. return
  449. ;;
  450. --entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|-c|--cpu-shares|-n|--name|-a|--attach|--link|-p|--publish|--expose|--dns|--lxc-conf)
  451. return
  452. ;;
  453. *)
  454. ;;
  455. esac
  456. case "$cur" in
  457. -*)
  458. 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" ) )
  459. ;;
  460. *)
  461. 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')
  462. if [ $cword -eq $counter ]; then
  463. __docker_image_repos_and_tags_and_ids
  464. fi
  465. ;;
  466. esac
  467. }
  468. _docker_save()
  469. {
  470. local counter=$(__docker_pos_first_nonflag)
  471. if [ $cword -eq $counter ]; then
  472. __docker_image_repos_and_tags_and_ids
  473. fi
  474. }
  475. _docker_search()
  476. {
  477. case "$prev" in
  478. -s|--stars)
  479. return
  480. ;;
  481. *)
  482. ;;
  483. esac
  484. case "$cur" in
  485. -*)
  486. COMPREPLY=( $( compgen -W "--no-trunc -t --trusted -s --stars" -- "$cur" ) )
  487. ;;
  488. *)
  489. ;;
  490. esac
  491. }
  492. _docker_start()
  493. {
  494. case "$cur" in
  495. -*)
  496. COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
  497. ;;
  498. *)
  499. __docker_containers_stopped
  500. ;;
  501. esac
  502. }
  503. _docker_stop()
  504. {
  505. case "$prev" in
  506. -t|--time)
  507. return
  508. ;;
  509. *)
  510. ;;
  511. esac
  512. case "$cur" in
  513. -*)
  514. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  515. ;;
  516. *)
  517. __docker_containers_running
  518. ;;
  519. esac
  520. }
  521. _docker_tag()
  522. {
  523. case "$cur" in
  524. -*)
  525. COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
  526. ;;
  527. *)
  528. local counter=$(__docker_pos_first_nonflag)
  529. if [ $cword -eq $counter ]; then
  530. __docker_image_repos_and_tags
  531. return
  532. fi
  533. (( counter++ ))
  534. if [ $cword -eq $counter ]; then
  535. __docker_image_repos_and_tags
  536. return
  537. fi
  538. ;;
  539. esac
  540. }
  541. _docker_top()
  542. {
  543. local counter=$(__docker_pos_first_nonflag)
  544. if [ $cword -eq $counter ]; then
  545. __docker_containers_running
  546. fi
  547. }
  548. _docker_version()
  549. {
  550. return
  551. }
  552. _docker_wait()
  553. {
  554. __docker_containers_all
  555. }
  556. _docker()
  557. {
  558. local commands="
  559. attach
  560. build
  561. commit
  562. cp
  563. diff
  564. events
  565. export
  566. history
  567. images
  568. import
  569. info
  570. insert
  571. inspect
  572. kill
  573. load
  574. login
  575. logs
  576. port
  577. ps
  578. pull
  579. push
  580. restart
  581. rm
  582. rmi
  583. run
  584. save
  585. search
  586. start
  587. stop
  588. tag
  589. top
  590. version
  591. wait
  592. "
  593. COMPREPLY=()
  594. local cur prev words cword
  595. _get_comp_words_by_ref -n : cur prev words cword
  596. local command='docker'
  597. local counter=1
  598. while [ $counter -lt $cword ]; do
  599. case "${words[$counter]}" in
  600. -H)
  601. (( counter++ ))
  602. ;;
  603. -*)
  604. ;;
  605. *)
  606. command="${words[$counter]}"
  607. cpos=$counter
  608. (( cpos++ ))
  609. break
  610. ;;
  611. esac
  612. (( counter++ ))
  613. done
  614. local completions_func=_docker_${command}
  615. declare -F $completions_func >/dev/null && $completions_func
  616. return 0
  617. }
  618. complete -F _docker docker