docker 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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_inspect()
  280. {
  281. case "$prev" in
  282. -f|--format)
  283. return
  284. ;;
  285. *)
  286. ;;
  287. esac
  288. case "$cur" in
  289. -*)
  290. COMPREPLY=( $( compgen -W "-f --format" -- "$cur" ) )
  291. ;;
  292. *)
  293. __docker_containers_and_images
  294. ;;
  295. esac
  296. }
  297. _docker_kill()
  298. {
  299. __docker_containers_running
  300. }
  301. _docker_load()
  302. {
  303. return
  304. }
  305. _docker_login()
  306. {
  307. case "$prev" in
  308. -u|--username|-p|--password|-e|--email)
  309. return
  310. ;;
  311. *)
  312. ;;
  313. esac
  314. case "$cur" in
  315. -*)
  316. COMPREPLY=( $( compgen -W "-u --username -p --password -e --email" -- "$cur" ) )
  317. ;;
  318. *)
  319. ;;
  320. esac
  321. }
  322. _docker_logs()
  323. {
  324. case "$cur" in
  325. -*)
  326. COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) )
  327. ;;
  328. *)
  329. local counter=$(__docker_pos_first_nonflag)
  330. if [ $cword -eq $counter ]; then
  331. __docker_containers_all
  332. fi
  333. ;;
  334. esac
  335. }
  336. _docker_port()
  337. {
  338. local counter=$(__docker_pos_first_nonflag)
  339. if [ $cword -eq $counter ]; then
  340. __docker_containers_all
  341. fi
  342. }
  343. _docker_ps()
  344. {
  345. case "$prev" in
  346. --since|--before)
  347. __docker_containers_all
  348. ;;
  349. -n)
  350. return
  351. ;;
  352. *)
  353. ;;
  354. esac
  355. case "$cur" in
  356. -*)
  357. COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since --before -n" -- "$cur" ) )
  358. ;;
  359. *)
  360. ;;
  361. esac
  362. }
  363. _docker_pull()
  364. {
  365. case "$prev" in
  366. -t|--tag)
  367. return
  368. ;;
  369. *)
  370. ;;
  371. esac
  372. case "$cur" in
  373. -*)
  374. COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) )
  375. ;;
  376. *)
  377. local counter=$(__docker_pos_first_nonflag '-t|--tag')
  378. if [ $cword -eq $counter ]; then
  379. __docker_image_repos_and_tags
  380. fi
  381. ;;
  382. esac
  383. }
  384. _docker_push()
  385. {
  386. local counter=$(__docker_pos_first_nonflag)
  387. if [ $cword -eq $counter ]; then
  388. __docker_image_repos_and_tags
  389. fi
  390. }
  391. _docker_restart()
  392. {
  393. case "$prev" in
  394. -t|--time)
  395. return
  396. ;;
  397. *)
  398. ;;
  399. esac
  400. case "$cur" in
  401. -*)
  402. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  403. ;;
  404. *)
  405. __docker_containers_all
  406. ;;
  407. esac
  408. }
  409. _docker_rm()
  410. {
  411. case "$cur" in
  412. -*)
  413. COMPREPLY=( $( compgen -W "-v --volumes -l --link" -- "$cur" ) )
  414. ;;
  415. *)
  416. __docker_containers_stopped
  417. ;;
  418. esac
  419. }
  420. _docker_rmi()
  421. {
  422. __docker_image_repos_and_tags_and_ids
  423. }
  424. _docker_run()
  425. {
  426. case "$prev" in
  427. --cidfile)
  428. _filedir
  429. ;;
  430. --volumes-from)
  431. __docker_containers_all
  432. ;;
  433. -v|--volume)
  434. # TODO something magical with colons and _filedir ?
  435. return
  436. ;;
  437. -e|--env)
  438. COMPREPLY=( $( compgen -e -- "$cur" ) )
  439. return
  440. ;;
  441. --entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|-c|--cpu-shares|-n|--name|-a|--attach|--link|-p|--publish|--expose|--dns|--lxc-conf)
  442. return
  443. ;;
  444. *)
  445. ;;
  446. esac
  447. case "$cur" in
  448. -*)
  449. 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" ) )
  450. ;;
  451. *)
  452. 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')
  453. if [ $cword -eq $counter ]; then
  454. __docker_image_repos_and_tags_and_ids
  455. fi
  456. ;;
  457. esac
  458. }
  459. _docker_save()
  460. {
  461. local counter=$(__docker_pos_first_nonflag)
  462. if [ $cword -eq $counter ]; then
  463. __docker_image_repos_and_tags_and_ids
  464. fi
  465. }
  466. _docker_search()
  467. {
  468. case "$prev" in
  469. -s|--stars)
  470. return
  471. ;;
  472. *)
  473. ;;
  474. esac
  475. case "$cur" in
  476. -*)
  477. COMPREPLY=( $( compgen -W "--no-trunc --automated -s --stars" -- "$cur" ) )
  478. ;;
  479. *)
  480. ;;
  481. esac
  482. }
  483. _docker_start()
  484. {
  485. case "$cur" in
  486. -*)
  487. COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) )
  488. ;;
  489. *)
  490. __docker_containers_stopped
  491. ;;
  492. esac
  493. }
  494. _docker_stop()
  495. {
  496. case "$prev" in
  497. -t|--time)
  498. return
  499. ;;
  500. *)
  501. ;;
  502. esac
  503. case "$cur" in
  504. -*)
  505. COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) )
  506. ;;
  507. *)
  508. __docker_containers_running
  509. ;;
  510. esac
  511. }
  512. _docker_tag()
  513. {
  514. case "$cur" in
  515. -*)
  516. COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) )
  517. ;;
  518. *)
  519. local counter=$(__docker_pos_first_nonflag)
  520. if [ $cword -eq $counter ]; then
  521. __docker_image_repos_and_tags
  522. return
  523. fi
  524. (( counter++ ))
  525. if [ $cword -eq $counter ]; then
  526. __docker_image_repos_and_tags
  527. return
  528. fi
  529. ;;
  530. esac
  531. }
  532. _docker_top()
  533. {
  534. local counter=$(__docker_pos_first_nonflag)
  535. if [ $cword -eq $counter ]; then
  536. __docker_containers_running
  537. fi
  538. }
  539. _docker_version()
  540. {
  541. return
  542. }
  543. _docker_wait()
  544. {
  545. __docker_containers_all
  546. }
  547. _docker()
  548. {
  549. local commands="
  550. attach
  551. build
  552. commit
  553. cp
  554. diff
  555. events
  556. export
  557. history
  558. images
  559. import
  560. info
  561. insert
  562. inspect
  563. kill
  564. load
  565. login
  566. logs
  567. port
  568. ps
  569. pull
  570. push
  571. restart
  572. rm
  573. rmi
  574. run
  575. save
  576. search
  577. start
  578. stop
  579. tag
  580. top
  581. version
  582. wait
  583. "
  584. COMPREPLY=()
  585. local cur prev words cword
  586. _get_comp_words_by_ref -n : cur prev words cword
  587. local command='docker'
  588. local counter=1
  589. while [ $counter -lt $cword ]; do
  590. case "${words[$counter]}" in
  591. -H)
  592. (( counter++ ))
  593. ;;
  594. -*)
  595. ;;
  596. *)
  597. command="${words[$counter]}"
  598. cpos=$counter
  599. (( cpos++ ))
  600. break
  601. ;;
  602. esac
  603. (( counter++ ))
  604. done
  605. local completions_func=_docker_${command}
  606. declare -F $completions_func >/dev/null && $completions_func
  607. return 0
  608. }
  609. complete -F _docker docker