docker 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. #!/bin/bash
  2. #
  3. # bash completion file for core docker commands
  4. #
  5. # This script provides 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 to e.g. ~/.docker-completion.sh and add the line
  15. # below to your .bashrc after bash completion features are loaded
  16. # . ~/.docker-completion.sh
  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. #
  24. # Note for developers:
  25. # Please arrange options sorted alphabetically by long name with the short
  26. # options immediately following their corresponding long form.
  27. # This order should be applied to lists, alternatives and code blocks.
  28. __docker_q() {
  29. docker ${host:+-H "$host"} 2>/dev/null "$@"
  30. }
  31. __docker_containers_all() {
  32. local IFS=$'\n'
  33. local containers=( $(__docker_q ps -aq --no-trunc) )
  34. if [ "$1" ]; then
  35. containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
  36. fi
  37. local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
  38. names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
  39. unset IFS
  40. COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
  41. }
  42. __docker_containers_running() {
  43. __docker_containers_all '.State.Running'
  44. }
  45. __docker_containers_stopped() {
  46. __docker_containers_all 'not .State.Running'
  47. }
  48. __docker_containers_pauseable() {
  49. __docker_containers_all 'and .State.Running (not .State.Paused)'
  50. }
  51. __docker_containers_unpauseable() {
  52. __docker_containers_all '.State.Paused'
  53. }
  54. __docker_container_names() {
  55. local containers=( $(__docker_q ps -aq --no-trunc) )
  56. local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
  57. names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
  58. COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
  59. }
  60. __docker_container_ids() {
  61. local containers=( $(__docker_q ps -aq) )
  62. COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
  63. }
  64. __docker_image_repos() {
  65. local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
  66. COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
  67. }
  68. __docker_image_repos_and_tags() {
  69. local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
  70. COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
  71. __ltrim_colon_completions "$cur"
  72. }
  73. __docker_image_repos_and_tags_and_ids() {
  74. local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
  75. COMPREPLY=( $(compgen -W "$images" -- "$cur") )
  76. __ltrim_colon_completions "$cur"
  77. }
  78. __docker_containers_and_images() {
  79. __docker_containers_all
  80. local containers=( "${COMPREPLY[@]}" )
  81. __docker_image_repos_and_tags_and_ids
  82. COMPREPLY+=( "${containers[@]}" )
  83. }
  84. __docker_pos_first_nonflag() {
  85. local argument_flags=$1
  86. local counter=$cpos
  87. while [ $counter -le $cword ]; do
  88. if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
  89. (( counter++ ))
  90. else
  91. case "${words[$counter]}" in
  92. -*)
  93. ;;
  94. *)
  95. break
  96. ;;
  97. esac
  98. fi
  99. (( counter++ ))
  100. done
  101. echo $counter
  102. }
  103. # Transforms a multiline list of strings into a single line string
  104. # with the words separated by "|".
  105. # This is used to prepare arguments to __docker_pos_first_nonflag().
  106. __docker_to_alternatives() {
  107. local parts=( $1 )
  108. local IFS='|'
  109. echo "${parts[*]}"
  110. }
  111. # Transforms a multiline list of options into an extglob pattern
  112. # suitable for use in case statements.
  113. __docker_to_extglob() {
  114. local extglob=$( __docker_to_alternatives "$1" )
  115. echo "@($extglob)"
  116. }
  117. __docker_resolve_hostname() {
  118. command -v host >/dev/null 2>&1 || return
  119. COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
  120. }
  121. __docker_capabilities() {
  122. # The list of capabilities is defined in types.go, ALL was added manually.
  123. COMPREPLY=( $( compgen -W "
  124. ALL
  125. AUDIT_CONTROL
  126. AUDIT_WRITE
  127. AUDIT_READ
  128. BLOCK_SUSPEND
  129. CHOWN
  130. DAC_OVERRIDE
  131. DAC_READ_SEARCH
  132. FOWNER
  133. FSETID
  134. IPC_LOCK
  135. IPC_OWNER
  136. KILL
  137. LEASE
  138. LINUX_IMMUTABLE
  139. MAC_ADMIN
  140. MAC_OVERRIDE
  141. MKNOD
  142. NET_ADMIN
  143. NET_BIND_SERVICE
  144. NET_BROADCAST
  145. NET_RAW
  146. SETFCAP
  147. SETGID
  148. SETPCAP
  149. SETUID
  150. SYS_ADMIN
  151. SYS_BOOT
  152. SYS_CHROOT
  153. SYSLOG
  154. SYS_MODULE
  155. SYS_NICE
  156. SYS_PACCT
  157. SYS_PTRACE
  158. SYS_RAWIO
  159. SYS_RESOURCE
  160. SYS_TIME
  161. SYS_TTY_CONFIG
  162. WAKE_ALARM
  163. " -- "$cur" ) )
  164. }
  165. # a selection of the available signals that is most likely of interest in the
  166. # context of docker containers.
  167. __docker_signals() {
  168. local signals=(
  169. SIGCONT
  170. SIGHUP
  171. SIGINT
  172. SIGKILL
  173. SIGQUIT
  174. SIGSTOP
  175. SIGTERM
  176. SIGUSR1
  177. SIGUSR2
  178. )
  179. COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) )
  180. }
  181. _docker_docker() {
  182. local boolean_options="
  183. --daemon -d
  184. --debug -D
  185. --help -h
  186. --icc
  187. --ip-forward
  188. --ip-masq
  189. --iptables
  190. --ipv6
  191. --selinux-enabled
  192. --tls
  193. --tlsverify
  194. --userland-proxy=false
  195. --version -v
  196. "
  197. case "$prev" in
  198. --exec-root|--graph|-g)
  199. _filedir -d
  200. return
  201. ;;
  202. --log-driver)
  203. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur" ) )
  204. return
  205. ;;
  206. --log-level|-l)
  207. COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
  208. return
  209. ;;
  210. --pidfile|-p|--tlscacert|--tlscert|--tlskey)
  211. _filedir
  212. return
  213. ;;
  214. --storage-driver|-s)
  215. COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
  216. return
  217. ;;
  218. $main_options_with_args_glob )
  219. return
  220. ;;
  221. esac
  222. case "$cur" in
  223. -*)
  224. COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) )
  225. ;;
  226. *)
  227. COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
  228. ;;
  229. esac
  230. }
  231. _docker_attach() {
  232. case "$cur" in
  233. -*)
  234. COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) )
  235. ;;
  236. *)
  237. local counter="$(__docker_pos_first_nonflag)"
  238. if [ $cword -eq $counter ]; then
  239. __docker_containers_running
  240. fi
  241. ;;
  242. esac
  243. }
  244. _docker_build() {
  245. case "$prev" in
  246. --cgroup-parent|--cpuset-cpus|--cpuset-mems|--cpu-shares|-c|--cpu-period|--cpu-quota|--memory|-m|--memory-swap)
  247. return
  248. ;;
  249. --file|-f)
  250. _filedir
  251. return
  252. ;;
  253. --tag|-t)
  254. __docker_image_repos_and_tags
  255. return
  256. ;;
  257. esac
  258. case "$cur" in
  259. -*)
  260. COMPREPLY=( $( compgen -W "--cgroup-parent --cpuset-cpus --cpuset-mems --cpu-shares -c --cpu-period --cpu-quota --file -f --force-rm --help --memory -m --memory-swap --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
  261. ;;
  262. *)
  263. local counter="$(__docker_pos_first_nonflag '--cgroup-parent|--cpuset-cpus|--cpuset-mems|--cpu-shares|-c|--cpu-period|--cpu-quota|--file|-f|--memory|-m|--memory-swap|--tag|-t')"
  264. if [ $cword -eq $counter ]; then
  265. _filedir -d
  266. fi
  267. ;;
  268. esac
  269. }
  270. _docker_commit() {
  271. case "$prev" in
  272. --author|-a|--change|-c|--message|-m)
  273. return
  274. ;;
  275. esac
  276. case "$cur" in
  277. -*)
  278. COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
  279. ;;
  280. *)
  281. local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
  282. if [ $cword -eq $counter ]; then
  283. __docker_containers_all
  284. return
  285. fi
  286. (( counter++ ))
  287. if [ $cword -eq $counter ]; then
  288. __docker_image_repos_and_tags
  289. return
  290. fi
  291. ;;
  292. esac
  293. }
  294. _docker_cp() {
  295. case "$cur" in
  296. -*)
  297. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  298. ;;
  299. *)
  300. local counter=$(__docker_pos_first_nonflag)
  301. if [ $cword -eq $counter ]; then
  302. case "$cur" in
  303. *:)
  304. return
  305. ;;
  306. *)
  307. __docker_containers_all
  308. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  309. compopt -o nospace
  310. return
  311. ;;
  312. esac
  313. fi
  314. (( counter++ ))
  315. if [ $cword -eq $counter ]; then
  316. _filedir -d
  317. return
  318. fi
  319. ;;
  320. esac
  321. }
  322. _docker_create() {
  323. _docker_run
  324. }
  325. _docker_diff() {
  326. case "$cur" in
  327. -*)
  328. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  329. ;;
  330. *)
  331. local counter=$(__docker_pos_first_nonflag)
  332. if [ $cword -eq $counter ]; then
  333. __docker_containers_all
  334. fi
  335. ;;
  336. esac
  337. }
  338. _docker_events() {
  339. case "$prev" in
  340. --filter|-f)
  341. COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) )
  342. compopt -o nospace
  343. return
  344. ;;
  345. --since|--until)
  346. return
  347. ;;
  348. esac
  349. # "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
  350. # it is the last character or not. So we search for "xxx=" in the the last two words.
  351. case "${words[$cword-2]}$prev=" in
  352. *container=*)
  353. cur="${cur#=}"
  354. __docker_containers_all
  355. return
  356. ;;
  357. *event=*)
  358. COMPREPLY=( $( compgen -W "create destroy die export kill pause restart start stop unpause" -- "${cur#=}" ) )
  359. return
  360. ;;
  361. *image=*)
  362. cur="${cur#=}"
  363. __docker_image_repos_and_tags_and_ids
  364. return
  365. ;;
  366. esac
  367. case "$cur" in
  368. -*)
  369. COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
  370. ;;
  371. esac
  372. }
  373. _docker_exec() {
  374. case "$prev" in
  375. --user|-u)
  376. return
  377. ;;
  378. esac
  379. case "$cur" in
  380. -*)
  381. COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty -u --user" -- "$cur" ) )
  382. ;;
  383. *)
  384. __docker_containers_running
  385. ;;
  386. esac
  387. }
  388. _docker_export() {
  389. case "$cur" in
  390. -*)
  391. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  392. ;;
  393. *)
  394. local counter=$(__docker_pos_first_nonflag)
  395. if [ $cword -eq $counter ]; then
  396. __docker_containers_all
  397. fi
  398. ;;
  399. esac
  400. }
  401. _docker_help() {
  402. local counter=$(__docker_pos_first_nonflag)
  403. if [ $cword -eq $counter ]; then
  404. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  405. fi
  406. }
  407. _docker_history() {
  408. case "$cur" in
  409. -*)
  410. COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  411. ;;
  412. *)
  413. local counter=$(__docker_pos_first_nonflag)
  414. if [ $cword -eq $counter ]; then
  415. __docker_image_repos_and_tags_and_ids
  416. fi
  417. ;;
  418. esac
  419. }
  420. _docker_images() {
  421. case "$prev" in
  422. --filter|-f)
  423. COMPREPLY=( $( compgen -W "dangling=true label=" -- "$cur" ) )
  424. if [ "$COMPREPLY" = "label=" ]; then
  425. compopt -o nospace
  426. fi
  427. return
  428. ;;
  429. esac
  430. case "${words[$cword-2]}$prev=" in
  431. *dangling=*)
  432. COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  433. return
  434. ;;
  435. *label=*)
  436. return
  437. ;;
  438. esac
  439. case "$cur" in
  440. -*)
  441. COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  442. ;;
  443. =)
  444. return
  445. ;;
  446. *)
  447. __docker_image_repos
  448. ;;
  449. esac
  450. }
  451. _docker_import() {
  452. case "$cur" in
  453. -*)
  454. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  455. ;;
  456. *)
  457. local counter=$(__docker_pos_first_nonflag)
  458. if [ $cword -eq $counter ]; then
  459. return
  460. fi
  461. (( counter++ ))
  462. if [ $cword -eq $counter ]; then
  463. __docker_image_repos_and_tags
  464. return
  465. fi
  466. ;;
  467. esac
  468. }
  469. _docker_info() {
  470. case "$cur" in
  471. -*)
  472. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  473. ;;
  474. esac
  475. }
  476. _docker_inspect() {
  477. case "$prev" in
  478. --format|-f)
  479. return
  480. ;;
  481. esac
  482. case "$cur" in
  483. -*)
  484. COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
  485. ;;
  486. *)
  487. __docker_containers_and_images
  488. ;;
  489. esac
  490. }
  491. _docker_kill() {
  492. case "$prev" in
  493. --signal|-s)
  494. __docker_signals
  495. return
  496. ;;
  497. esac
  498. case "$cur" in
  499. -*)
  500. COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  501. ;;
  502. *)
  503. __docker_containers_running
  504. ;;
  505. esac
  506. }
  507. _docker_load() {
  508. case "$prev" in
  509. --input|-i)
  510. _filedir
  511. return
  512. ;;
  513. esac
  514. case "$cur" in
  515. -*)
  516. COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  517. ;;
  518. esac
  519. }
  520. _docker_login() {
  521. case "$prev" in
  522. --email|-e|--password|-p|--username|-u)
  523. return
  524. ;;
  525. esac
  526. case "$cur" in
  527. -*)
  528. COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  529. ;;
  530. esac
  531. }
  532. _docker_logout() {
  533. case "$cur" in
  534. -*)
  535. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  536. ;;
  537. esac
  538. }
  539. _docker_logs() {
  540. case "$prev" in
  541. --since|--tail)
  542. return
  543. ;;
  544. esac
  545. case "$cur" in
  546. -*)
  547. COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) )
  548. ;;
  549. *)
  550. local counter=$(__docker_pos_first_nonflag '--tail')
  551. if [ $cword -eq $counter ]; then
  552. __docker_containers_all
  553. fi
  554. ;;
  555. esac
  556. }
  557. _docker_pause() {
  558. case "$cur" in
  559. -*)
  560. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  561. ;;
  562. *)
  563. local counter=$(__docker_pos_first_nonflag)
  564. if [ $cword -eq $counter ]; then
  565. __docker_containers_pauseable
  566. fi
  567. ;;
  568. esac
  569. }
  570. _docker_port() {
  571. case "$cur" in
  572. -*)
  573. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  574. ;;
  575. *)
  576. local counter=$(__docker_pos_first_nonflag)
  577. if [ $cword -eq $counter ]; then
  578. __docker_containers_all
  579. fi
  580. ;;
  581. esac
  582. }
  583. _docker_ps() {
  584. case "$prev" in
  585. --before|--since)
  586. __docker_containers_all
  587. ;;
  588. --filter|-f)
  589. COMPREPLY=( $( compgen -S = -W "exited id label name status" -- "$cur" ) )
  590. compopt -o nospace
  591. return
  592. ;;
  593. -n)
  594. return
  595. ;;
  596. esac
  597. case "${words[$cword-2]}$prev=" in
  598. *id=*)
  599. cur="${cur#=}"
  600. __docker_container_ids
  601. return
  602. ;;
  603. *name=*)
  604. cur="${cur#=}"
  605. __docker_container_names
  606. return
  607. ;;
  608. *status=*)
  609. COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
  610. return
  611. ;;
  612. esac
  613. case "$cur" in
  614. -*)
  615. COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  616. ;;
  617. esac
  618. }
  619. _docker_pull() {
  620. case "$cur" in
  621. -*)
  622. COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  623. ;;
  624. *)
  625. local counter=$(__docker_pos_first_nonflag)
  626. if [ $cword -eq $counter ]; then
  627. for arg in "${COMP_WORDS[@]}"; do
  628. case "$arg" in
  629. --all-tags|-a)
  630. __docker_image_repos
  631. return
  632. ;;
  633. esac
  634. done
  635. __docker_image_repos_and_tags
  636. fi
  637. ;;
  638. esac
  639. }
  640. _docker_push() {
  641. case "$cur" in
  642. -*)
  643. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  644. ;;
  645. *)
  646. local counter=$(__docker_pos_first_nonflag)
  647. if [ $cword -eq $counter ]; then
  648. __docker_image_repos_and_tags
  649. fi
  650. ;;
  651. esac
  652. }
  653. _docker_rename() {
  654. case "$cur" in
  655. -*)
  656. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  657. ;;
  658. *)
  659. local counter=$(__docker_pos_first_nonflag)
  660. if [ $cword -eq $counter ]; then
  661. __docker_containers_all
  662. fi
  663. ;;
  664. esac
  665. }
  666. _docker_restart() {
  667. case "$prev" in
  668. --time|-t)
  669. return
  670. ;;
  671. esac
  672. case "$cur" in
  673. -*)
  674. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  675. ;;
  676. *)
  677. __docker_containers_all
  678. ;;
  679. esac
  680. }
  681. _docker_rm() {
  682. case "$cur" in
  683. -*)
  684. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  685. ;;
  686. *)
  687. for arg in "${COMP_WORDS[@]}"; do
  688. case "$arg" in
  689. --force|-f)
  690. __docker_containers_all
  691. return
  692. ;;
  693. esac
  694. done
  695. __docker_containers_stopped
  696. ;;
  697. esac
  698. }
  699. _docker_rmi() {
  700. case "$cur" in
  701. -*)
  702. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  703. ;;
  704. *)
  705. __docker_image_repos_and_tags_and_ids
  706. ;;
  707. esac
  708. }
  709. _docker_run() {
  710. local options_with_args="
  711. --add-host
  712. --blkio-weight
  713. --attach -a
  714. --cap-add
  715. --cap-drop
  716. --cgroup-parent
  717. --cidfile
  718. --cpuset
  719. --cpu-period
  720. --cpu-quota
  721. --cpu-shares -c
  722. --device
  723. --dns
  724. --dns-search
  725. --entrypoint
  726. --env -e
  727. --env-file
  728. --expose
  729. --hostname -h
  730. --ipc
  731. --label -l
  732. --label-file
  733. --link
  734. --log-driver
  735. --lxc-conf
  736. --mac-address
  737. --memory -m
  738. --memory-swap
  739. --name
  740. --net
  741. --pid
  742. --publish -p
  743. --restart
  744. --security-opt
  745. --user -u
  746. --ulimit
  747. --uts
  748. --volumes-from
  749. --volume -v
  750. --workdir -w
  751. "
  752. local all_options="$options_with_args
  753. --help
  754. --interactive -i
  755. --privileged
  756. --publish-all -P
  757. --read-only
  758. --tty -t
  759. "
  760. [ "$command" = "run" ] && all_options="$all_options
  761. --detach -d
  762. --rm
  763. --sig-proxy
  764. "
  765. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  766. case "$prev" in
  767. --add-host)
  768. case "$cur" in
  769. *:)
  770. __docker_resolve_hostname
  771. return
  772. ;;
  773. esac
  774. ;;
  775. --attach|-a)
  776. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  777. return
  778. ;;
  779. --cap-add|--cap-drop)
  780. __docker_capabilities
  781. return
  782. ;;
  783. --cidfile|--env-file|--label-file)
  784. _filedir
  785. return
  786. ;;
  787. --device|--volume|-v)
  788. case "$cur" in
  789. *:*)
  790. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  791. ;;
  792. '')
  793. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  794. compopt -o nospace
  795. ;;
  796. /*)
  797. _filedir
  798. compopt -o nospace
  799. ;;
  800. esac
  801. return
  802. ;;
  803. --env|-e)
  804. COMPREPLY=( $( compgen -e -- "$cur" ) )
  805. compopt -o nospace
  806. return
  807. ;;
  808. --ipc)
  809. case "$cur" in
  810. *:*)
  811. cur="${cur#*:}"
  812. __docker_containers_running
  813. ;;
  814. *)
  815. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  816. if [ "$COMPREPLY" = "container:" ]; then
  817. compopt -o nospace
  818. fi
  819. ;;
  820. esac
  821. return
  822. ;;
  823. --link)
  824. case "$cur" in
  825. *:*)
  826. ;;
  827. *)
  828. __docker_containers_running
  829. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  830. compopt -o nospace
  831. ;;
  832. esac
  833. return
  834. ;;
  835. --log-driver)
  836. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur") )
  837. return
  838. ;;
  839. --net)
  840. case "$cur" in
  841. container:*)
  842. local cur=${cur#*:}
  843. __docker_containers_all
  844. ;;
  845. *)
  846. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  847. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  848. compopt -o nospace
  849. fi
  850. ;;
  851. esac
  852. return
  853. ;;
  854. --restart)
  855. case "$cur" in
  856. on-failure:*)
  857. ;;
  858. *)
  859. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  860. ;;
  861. esac
  862. return
  863. ;;
  864. --security-opt)
  865. case "$cur" in
  866. label:*:*)
  867. ;;
  868. label:*)
  869. local cur=${cur##*:}
  870. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  871. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  872. compopt -o nospace
  873. fi
  874. ;;
  875. *)
  876. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  877. compopt -o nospace
  878. ;;
  879. esac
  880. return
  881. ;;
  882. --volumes-from)
  883. __docker_containers_all
  884. return
  885. ;;
  886. $options_with_args_glob )
  887. return
  888. ;;
  889. esac
  890. case "$cur" in
  891. -*)
  892. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  893. ;;
  894. *)
  895. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  896. if [ $cword -eq $counter ]; then
  897. __docker_image_repos_and_tags_and_ids
  898. fi
  899. ;;
  900. esac
  901. }
  902. _docker_save() {
  903. case "$prev" in
  904. --output|-o)
  905. _filedir
  906. return
  907. ;;
  908. esac
  909. case "$cur" in
  910. -*)
  911. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  912. ;;
  913. *)
  914. __docker_image_repos_and_tags_and_ids
  915. ;;
  916. esac
  917. }
  918. _docker_search() {
  919. case "$prev" in
  920. --stars|-s)
  921. return
  922. ;;
  923. esac
  924. case "$cur" in
  925. -*)
  926. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  927. ;;
  928. esac
  929. }
  930. _docker_start() {
  931. case "$cur" in
  932. -*)
  933. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  934. ;;
  935. *)
  936. __docker_containers_stopped
  937. ;;
  938. esac
  939. }
  940. _docker_stats() {
  941. case "$cur" in
  942. -*)
  943. COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) )
  944. ;;
  945. *)
  946. __docker_containers_running
  947. ;;
  948. esac
  949. }
  950. _docker_stop() {
  951. case "$prev" in
  952. --time|-t)
  953. return
  954. ;;
  955. esac
  956. case "$cur" in
  957. -*)
  958. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  959. ;;
  960. *)
  961. __docker_containers_running
  962. ;;
  963. esac
  964. }
  965. _docker_tag() {
  966. case "$cur" in
  967. -*)
  968. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  969. ;;
  970. *)
  971. local counter=$(__docker_pos_first_nonflag)
  972. if [ $cword -eq $counter ]; then
  973. __docker_image_repos_and_tags
  974. return
  975. fi
  976. (( counter++ ))
  977. if [ $cword -eq $counter ]; then
  978. __docker_image_repos_and_tags
  979. return
  980. fi
  981. ;;
  982. esac
  983. }
  984. _docker_unpause() {
  985. case "$cur" in
  986. -*)
  987. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  988. ;;
  989. *)
  990. local counter=$(__docker_pos_first_nonflag)
  991. if [ $cword -eq $counter ]; then
  992. __docker_containers_unpauseable
  993. fi
  994. ;;
  995. esac
  996. }
  997. _docker_top() {
  998. case "$cur" in
  999. -*)
  1000. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1001. ;;
  1002. *)
  1003. local counter=$(__docker_pos_first_nonflag)
  1004. if [ $cword -eq $counter ]; then
  1005. __docker_containers_running
  1006. fi
  1007. ;;
  1008. esac
  1009. }
  1010. _docker_version() {
  1011. case "$cur" in
  1012. -*)
  1013. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1014. ;;
  1015. esac
  1016. }
  1017. _docker_wait() {
  1018. case "$cur" in
  1019. -*)
  1020. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1021. ;;
  1022. *)
  1023. __docker_containers_all
  1024. ;;
  1025. esac
  1026. }
  1027. _docker() {
  1028. local previous_extglob_setting=$(shopt -p extglob)
  1029. shopt -s extglob
  1030. local commands=(
  1031. attach
  1032. build
  1033. commit
  1034. cp
  1035. create
  1036. diff
  1037. events
  1038. exec
  1039. export
  1040. history
  1041. images
  1042. import
  1043. info
  1044. inspect
  1045. kill
  1046. load
  1047. login
  1048. logout
  1049. logs
  1050. pause
  1051. port
  1052. ps
  1053. pull
  1054. push
  1055. rename
  1056. restart
  1057. rm
  1058. rmi
  1059. run
  1060. save
  1061. search
  1062. start
  1063. stats
  1064. stop
  1065. tag
  1066. top
  1067. unpause
  1068. version
  1069. wait
  1070. )
  1071. local main_options_with_args="
  1072. --api-cors-header
  1073. --bip
  1074. --bridge -b
  1075. --default-gateway
  1076. --default-gateway-v6
  1077. --default-ulimit
  1078. --dns
  1079. --dns-search
  1080. --exec-driver -e
  1081. --exec-opt
  1082. --exec-root
  1083. --fixed-cidr
  1084. --fixed-cidr-v6
  1085. --graph -g
  1086. --group -G
  1087. --host -H
  1088. --insecure-registry
  1089. --ip
  1090. --label
  1091. --log-driver
  1092. --log-level -l
  1093. --mtu
  1094. --pidfile -p
  1095. --registry-mirror
  1096. --storage-driver -s
  1097. --storage-opt
  1098. --tlscacert
  1099. --tlscert
  1100. --tlskey
  1101. "
  1102. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1103. local host
  1104. COMPREPLY=()
  1105. local cur prev words cword
  1106. _get_comp_words_by_ref -n : cur prev words cword
  1107. local command='docker' cpos=0
  1108. local counter=1
  1109. while [ $counter -lt $cword ]; do
  1110. case "${words[$counter]}" in
  1111. # save host so that completion can use custom daemon
  1112. --host|-H)
  1113. (( counter++ ))
  1114. host="${words[$counter]}"
  1115. ;;
  1116. $main_options_with_args_glob )
  1117. (( counter++ ))
  1118. ;;
  1119. -*)
  1120. ;;
  1121. =)
  1122. (( counter++ ))
  1123. ;;
  1124. *)
  1125. command="${words[$counter]}"
  1126. cpos=$counter
  1127. (( cpos++ ))
  1128. break
  1129. ;;
  1130. esac
  1131. (( counter++ ))
  1132. done
  1133. local completions_func=_docker_${command}
  1134. declare -F $completions_func >/dev/null && $completions_func
  1135. eval "$previous_extglob_setting"
  1136. return 0
  1137. }
  1138. complete -F _docker docker