docker 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 --force-rm" -- "$cur" ) )
  139. ;;
  140. *)
  141. local counter="$(__docker_pos_first_nonflag '-t|--tag')"
  142. if [ $cword -eq $counter ]; then
  143. _filedir -d
  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_exec()
  222. {
  223. case "$cur" in
  224. -*)
  225. COMPREPLY=( $( compgen -W "-d --detach -i --interactive -t --tty" -- "$cur" ) )
  226. ;;
  227. *)
  228. __docker_containers_running
  229. ;;
  230. esac
  231. }
  232. _docker_export()
  233. {
  234. local counter=$(__docker_pos_first_nonflag)
  235. if [ $cword -eq $counter ]; then
  236. __docker_containers_all
  237. fi
  238. }
  239. _docker_help()
  240. {
  241. local counter=$(__docker_pos_first_nonflag)
  242. if [ $cword -eq $counter ]; then
  243. COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
  244. fi
  245. }
  246. _docker_history()
  247. {
  248. case "$cur" in
  249. -*)
  250. COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) )
  251. ;;
  252. *)
  253. local counter=$(__docker_pos_first_nonflag)
  254. if [ $cword -eq $counter ]; then
  255. __docker_image_repos_and_tags_and_ids
  256. fi
  257. ;;
  258. esac
  259. }
  260. _docker_images()
  261. {
  262. case "$cur" in
  263. -*)
  264. COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) )
  265. ;;
  266. *)
  267. local counter=$(__docker_pos_first_nonflag)
  268. if [ $cword -eq $counter ]; then
  269. __docker_image_repos
  270. fi
  271. ;;
  272. esac
  273. }
  274. _docker_import()
  275. {
  276. local counter=$(__docker_pos_first_nonflag)
  277. if [ $cword -eq $counter ]; then
  278. return
  279. fi
  280. (( counter++ ))
  281. if [ $cword -eq $counter ]; then
  282. __docker_image_repos_and_tags
  283. return
  284. fi
  285. }
  286. _docker_info()
  287. {
  288. return
  289. }
  290. _docker_inspect()
  291. {
  292. case "$prev" in
  293. -f|--format)
  294. return
  295. ;;
  296. *)
  297. ;;
  298. esac
  299. case "$cur" in
  300. -*)
  301. COMPREPLY=( $( compgen -W "-f --format" -- "$cur" ) )
  302. ;;
  303. *)
  304. __docker_containers_and_images
  305. ;;
  306. esac
  307. }
  308. _docker_kill()
  309. {
  310. __docker_containers_running
  311. }
  312. _docker_load()
  313. {
  314. return
  315. }
  316. _docker_login()
  317. {
  318. case "$prev" in
  319. -u|--username|-p|--password|-e|--email)
  320. return
  321. ;;
  322. *)
  323. ;;
  324. esac
  325. case "$cur" in
  326. -*)
  327. COMPREPLY=( $( compgen -W "-u --username -p --password -e --email" -- "$cur" ) )
  328. ;;
  329. *)
  330. ;;
  331. esac
  332. }
  333. _docker_logs()
  334. {
  335. case "$cur" in
  336. -*)
  337. COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) )
  338. ;;
  339. *)
  340. local counter=$(__docker_pos_first_nonflag)
  341. if [ $cword -eq $counter ]; then
  342. __docker_containers_all
  343. fi
  344. ;;
  345. esac
  346. }
  347. _docker_port()
  348. {
  349. local counter=$(__docker_pos_first_nonflag)
  350. if [ $cword -eq $counter ]; then
  351. __docker_containers_all
  352. fi
  353. }
  354. _docker_ps()
  355. {
  356. case "$prev" in
  357. --since|--before)
  358. __docker_containers_all
  359. ;;
  360. -n)
  361. return
  362. ;;
  363. *)
  364. ;;
  365. esac
  366. case "$cur" in
  367. -*)
  368. COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since --before -n" -- "$cur" ) )
  369. ;;
  370. *)
  371. ;;
  372. esac
  373. }
  374. _docker_pull()
  375. {
  376. case "$prev" in
  377. -t|--tag)
  378. return
  379. ;;
  380. *)
  381. ;;
  382. esac
  383. case "$cur" in
  384. -*)
  385. COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) )
  386. ;;
  387. *)
  388. local counter=$(__docker_pos_first_nonflag '-t|--tag')
  389. if [ $cword -eq $counter ]; then
  390. __docker_image_repos_and_tags
  391. fi
  392. ;;
  393. esac
  394. }
  395. _docker_push()
  396. {
  397. local counter=$(__docker_pos_first_nonflag)
  398. if [ $cword -eq $counter ]; then
  399. __docker_image_repos_and_tags
  400. fi
  401. }
  402. _docker_restart()
  403. {
  404. case "$prev" in
  405. -t|--time)
  406. return
  407. ;;
  408. *)
  409. ;;
  410. esac
  411. case "$cur" in
  412. -*)
  413. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  414. ;;
  415. *)
  416. __docker_containers_all
  417. ;;
  418. esac
  419. }
  420. _docker_rm()
  421. {
  422. case "$cur" in
  423. -*)
  424. COMPREPLY=( $( compgen -W "-f --force -l --link -v --volumes" -- "$cur" ) )
  425. return
  426. ;;
  427. *)
  428. local force=
  429. for arg in "${COMP_WORDS[@]}"; do
  430. case "$arg" in
  431. -f|--force)
  432. __docker_containers_all
  433. return
  434. ;;
  435. esac
  436. done
  437. __docker_containers_stopped
  438. return
  439. ;;
  440. esac
  441. }
  442. _docker_rmi()
  443. {
  444. __docker_image_repos_and_tags_and_ids
  445. }
  446. _docker_run()
  447. {
  448. case "$prev" in
  449. -a|--attach)
  450. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  451. return
  452. ;;
  453. --cidfile|--env-file)
  454. _filedir
  455. return
  456. ;;
  457. --volumes-from)
  458. __docker_containers_all
  459. return
  460. ;;
  461. -v|--volume)
  462. case "$cur" in
  463. *:*)
  464. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  465. ;;
  466. '')
  467. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  468. compopt -o nospace
  469. ;;
  470. /*)
  471. _filedir
  472. compopt -o nospace
  473. ;;
  474. esac
  475. return
  476. ;;
  477. -e|--env)
  478. COMPREPLY=( $( compgen -e -- "$cur" ) )
  479. compopt -o nospace
  480. return
  481. ;;
  482. --link)
  483. case "$cur" in
  484. *:*)
  485. ;;
  486. *)
  487. __docker_containers_running
  488. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  489. compopt -o nospace
  490. ;;
  491. esac
  492. return
  493. ;;
  494. --entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|--cpuset|-c|--cpu-shares|-n|--name|-p|--publish|--expose|--dns|--lxc-conf)
  495. return
  496. ;;
  497. *)
  498. ;;
  499. esac
  500. case "$cur" in
  501. -*)
  502. 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 --cpuset -c --cpu-shares --sig-proxy --name -a --attach -v --volume --link -e --env -p --publish --expose --dns --volumes-from --lxc-conf" -- "$cur" ) )
  503. ;;
  504. *)
  505. local counter=$(__docker_pos_first_nonflag '--cidfile|--volumes-from|-v|--volume|-e|--env|--entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|--cpuset|-c|--cpu-shares|-n|--name|-a|--attach|--link|-p|--publish|--expose|--dns|--lxc-conf')
  506. if [ $cword -eq $counter ]; then
  507. __docker_image_repos_and_tags_and_ids
  508. fi
  509. ;;
  510. esac
  511. }
  512. _docker_save()
  513. {
  514. local counter=$(__docker_pos_first_nonflag)
  515. if [ $cword -eq $counter ]; then
  516. __docker_image_repos_and_tags_and_ids
  517. fi
  518. }
  519. _docker_search()
  520. {
  521. case "$prev" in
  522. -s|--stars)
  523. return
  524. ;;
  525. *)
  526. ;;
  527. esac
  528. case "$cur" in
  529. -*)
  530. COMPREPLY=( $( compgen -W "--no-trunc --automated -s --stars" -- "$cur" ) )
  531. ;;
  532. *)
  533. ;;
  534. esac
  535. }
  536. _docker_start()
  537. {
  538. case "$cur" in
  539. -*)
  540. COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
  541. ;;
  542. *)
  543. __docker_containers_stopped
  544. ;;
  545. esac
  546. }
  547. _docker_stop()
  548. {
  549. case "$prev" in
  550. -t|--time)
  551. return
  552. ;;
  553. *)
  554. ;;
  555. esac
  556. case "$cur" in
  557. -*)
  558. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  559. ;;
  560. *)
  561. __docker_containers_running
  562. ;;
  563. esac
  564. }
  565. _docker_tag()
  566. {
  567. case "$cur" in
  568. -*)
  569. COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
  570. ;;
  571. *)
  572. local counter=$(__docker_pos_first_nonflag)
  573. if [ $cword -eq $counter ]; then
  574. __docker_image_repos_and_tags
  575. return
  576. fi
  577. (( counter++ ))
  578. if [ $cword -eq $counter ]; then
  579. __docker_image_repos_and_tags
  580. return
  581. fi
  582. ;;
  583. esac
  584. }
  585. _docker_top()
  586. {
  587. local counter=$(__docker_pos_first_nonflag)
  588. if [ $cword -eq $counter ]; then
  589. __docker_containers_running
  590. fi
  591. }
  592. _docker_version()
  593. {
  594. return
  595. }
  596. _docker_wait()
  597. {
  598. __docker_containers_all
  599. }
  600. _docker()
  601. {
  602. local commands="
  603. attach
  604. build
  605. commit
  606. cp
  607. diff
  608. events
  609. exec
  610. export
  611. history
  612. images
  613. import
  614. info
  615. insert
  616. inspect
  617. kill
  618. load
  619. login
  620. logs
  621. port
  622. ps
  623. pull
  624. push
  625. restart
  626. rm
  627. rmi
  628. run
  629. save
  630. search
  631. start
  632. stop
  633. tag
  634. top
  635. version
  636. wait
  637. "
  638. COMPREPLY=()
  639. local cur prev words cword
  640. _get_comp_words_by_ref -n : cur prev words cword
  641. local command='docker'
  642. local counter=1
  643. while [ $counter -lt $cword ]; do
  644. case "${words[$counter]}" in
  645. -H)
  646. (( counter++ ))
  647. ;;
  648. -*)
  649. ;;
  650. *)
  651. command="${words[$counter]}"
  652. cpos=$counter
  653. (( cpos++ ))
  654. break
  655. ;;
  656. esac
  657. (( counter++ ))
  658. done
  659. local completions_func=_docker_${command}
  660. declare -F $completions_func >/dev/null && $completions_func
  661. return 0
  662. }
  663. complete -F _docker docker