docker 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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 "
  359. attach
  360. commit
  361. copy
  362. create
  363. delete
  364. destroy
  365. die
  366. exec_create
  367. exec_start
  368. export
  369. import
  370. kill
  371. oom
  372. pause
  373. pull
  374. push
  375. rename
  376. resize
  377. restart
  378. start
  379. stop
  380. tag
  381. top
  382. unpause
  383. untag
  384. " -- "${cur#=}" ) )
  385. return
  386. ;;
  387. *image=*)
  388. cur="${cur#=}"
  389. __docker_image_repos_and_tags_and_ids
  390. return
  391. ;;
  392. esac
  393. case "$cur" in
  394. -*)
  395. COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
  396. ;;
  397. esac
  398. }
  399. _docker_exec() {
  400. case "$prev" in
  401. --user|-u)
  402. return
  403. ;;
  404. esac
  405. case "$cur" in
  406. -*)
  407. COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty -u --user" -- "$cur" ) )
  408. ;;
  409. *)
  410. __docker_containers_running
  411. ;;
  412. esac
  413. }
  414. _docker_export() {
  415. case "$cur" in
  416. -*)
  417. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  418. ;;
  419. *)
  420. local counter=$(__docker_pos_first_nonflag)
  421. if [ $cword -eq $counter ]; then
  422. __docker_containers_all
  423. fi
  424. ;;
  425. esac
  426. }
  427. _docker_help() {
  428. local counter=$(__docker_pos_first_nonflag)
  429. if [ $cword -eq $counter ]; then
  430. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  431. fi
  432. }
  433. _docker_history() {
  434. case "$cur" in
  435. -*)
  436. COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  437. ;;
  438. *)
  439. local counter=$(__docker_pos_first_nonflag)
  440. if [ $cword -eq $counter ]; then
  441. __docker_image_repos_and_tags_and_ids
  442. fi
  443. ;;
  444. esac
  445. }
  446. _docker_images() {
  447. case "$prev" in
  448. --filter|-f)
  449. COMPREPLY=( $( compgen -W "dangling=true label=" -- "$cur" ) )
  450. if [ "$COMPREPLY" = "label=" ]; then
  451. compopt -o nospace
  452. fi
  453. return
  454. ;;
  455. esac
  456. case "${words[$cword-2]}$prev=" in
  457. *dangling=*)
  458. COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  459. return
  460. ;;
  461. *label=*)
  462. return
  463. ;;
  464. esac
  465. case "$cur" in
  466. -*)
  467. COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  468. ;;
  469. =)
  470. return
  471. ;;
  472. *)
  473. __docker_image_repos
  474. ;;
  475. esac
  476. }
  477. _docker_import() {
  478. case "$cur" in
  479. -*)
  480. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  481. ;;
  482. *)
  483. local counter=$(__docker_pos_first_nonflag)
  484. if [ $cword -eq $counter ]; then
  485. return
  486. fi
  487. (( counter++ ))
  488. if [ $cword -eq $counter ]; then
  489. __docker_image_repos_and_tags
  490. return
  491. fi
  492. ;;
  493. esac
  494. }
  495. _docker_info() {
  496. case "$cur" in
  497. -*)
  498. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  499. ;;
  500. esac
  501. }
  502. _docker_inspect() {
  503. case "$prev" in
  504. --format|-f)
  505. return
  506. ;;
  507. --type)
  508. COMPREPLY=( $( compgen -W "image container" -- "$cur" ) )
  509. return
  510. ;;
  511. esac
  512. case "$cur" in
  513. -*)
  514. COMPREPLY=( $( compgen -W "--format -f --type --help" -- "$cur" ) )
  515. ;;
  516. *)
  517. __docker_containers_and_images
  518. ;;
  519. esac
  520. }
  521. _docker_kill() {
  522. case "$prev" in
  523. --signal|-s)
  524. __docker_signals
  525. return
  526. ;;
  527. esac
  528. case "$cur" in
  529. -*)
  530. COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  531. ;;
  532. *)
  533. __docker_containers_running
  534. ;;
  535. esac
  536. }
  537. _docker_load() {
  538. case "$prev" in
  539. --input|-i)
  540. _filedir
  541. return
  542. ;;
  543. esac
  544. case "$cur" in
  545. -*)
  546. COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  547. ;;
  548. esac
  549. }
  550. _docker_login() {
  551. case "$prev" in
  552. --email|-e|--password|-p|--username|-u)
  553. return
  554. ;;
  555. esac
  556. case "$cur" in
  557. -*)
  558. COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  559. ;;
  560. esac
  561. }
  562. _docker_logout() {
  563. case "$cur" in
  564. -*)
  565. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  566. ;;
  567. esac
  568. }
  569. _docker_logs() {
  570. case "$prev" in
  571. --since|--tail)
  572. return
  573. ;;
  574. esac
  575. case "$cur" in
  576. -*)
  577. COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) )
  578. ;;
  579. *)
  580. local counter=$(__docker_pos_first_nonflag '--tail')
  581. if [ $cword -eq $counter ]; then
  582. __docker_containers_all
  583. fi
  584. ;;
  585. esac
  586. }
  587. _docker_pause() {
  588. case "$cur" in
  589. -*)
  590. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  591. ;;
  592. *)
  593. local counter=$(__docker_pos_first_nonflag)
  594. if [ $cword -eq $counter ]; then
  595. __docker_containers_pauseable
  596. fi
  597. ;;
  598. esac
  599. }
  600. _docker_port() {
  601. case "$cur" in
  602. -*)
  603. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  604. ;;
  605. *)
  606. local counter=$(__docker_pos_first_nonflag)
  607. if [ $cword -eq $counter ]; then
  608. __docker_containers_all
  609. fi
  610. ;;
  611. esac
  612. }
  613. _docker_ps() {
  614. case "$prev" in
  615. --before|--since)
  616. __docker_containers_all
  617. ;;
  618. --filter|-f)
  619. COMPREPLY=( $( compgen -S = -W "exited id label name status" -- "$cur" ) )
  620. compopt -o nospace
  621. return
  622. ;;
  623. -n)
  624. return
  625. ;;
  626. esac
  627. case "${words[$cword-2]}$prev=" in
  628. *id=*)
  629. cur="${cur#=}"
  630. __docker_container_ids
  631. return
  632. ;;
  633. *name=*)
  634. cur="${cur#=}"
  635. __docker_container_names
  636. return
  637. ;;
  638. *status=*)
  639. COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
  640. return
  641. ;;
  642. esac
  643. case "$cur" in
  644. -*)
  645. COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  646. ;;
  647. esac
  648. }
  649. _docker_pull() {
  650. case "$cur" in
  651. -*)
  652. COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  653. ;;
  654. *)
  655. local counter=$(__docker_pos_first_nonflag)
  656. if [ $cword -eq $counter ]; then
  657. for arg in "${COMP_WORDS[@]}"; do
  658. case "$arg" in
  659. --all-tags|-a)
  660. __docker_image_repos
  661. return
  662. ;;
  663. esac
  664. done
  665. __docker_image_repos_and_tags
  666. fi
  667. ;;
  668. esac
  669. }
  670. _docker_push() {
  671. case "$cur" in
  672. -*)
  673. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  674. ;;
  675. *)
  676. local counter=$(__docker_pos_first_nonflag)
  677. if [ $cword -eq $counter ]; then
  678. __docker_image_repos_and_tags
  679. fi
  680. ;;
  681. esac
  682. }
  683. _docker_rename() {
  684. case "$cur" in
  685. -*)
  686. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  687. ;;
  688. *)
  689. local counter=$(__docker_pos_first_nonflag)
  690. if [ $cword -eq $counter ]; then
  691. __docker_containers_all
  692. fi
  693. ;;
  694. esac
  695. }
  696. _docker_restart() {
  697. case "$prev" in
  698. --time|-t)
  699. return
  700. ;;
  701. esac
  702. case "$cur" in
  703. -*)
  704. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  705. ;;
  706. *)
  707. __docker_containers_all
  708. ;;
  709. esac
  710. }
  711. _docker_rm() {
  712. case "$cur" in
  713. -*)
  714. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  715. ;;
  716. *)
  717. for arg in "${COMP_WORDS[@]}"; do
  718. case "$arg" in
  719. --force|-f)
  720. __docker_containers_all
  721. return
  722. ;;
  723. esac
  724. done
  725. __docker_containers_stopped
  726. ;;
  727. esac
  728. }
  729. _docker_rmi() {
  730. case "$cur" in
  731. -*)
  732. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  733. ;;
  734. *)
  735. __docker_image_repos_and_tags_and_ids
  736. ;;
  737. esac
  738. }
  739. _docker_run() {
  740. local options_with_args="
  741. --add-host
  742. --blkio-weight
  743. --attach -a
  744. --cap-add
  745. --cap-drop
  746. --cgroup-parent
  747. --cidfile
  748. --cpuset
  749. --cpu-period
  750. --cpu-quota
  751. --cpu-shares -c
  752. --device
  753. --dns
  754. --dns-search
  755. --entrypoint
  756. --env -e
  757. --env-file
  758. --expose
  759. --hostname -h
  760. --ipc
  761. --label -l
  762. --label-file
  763. --link
  764. --log-driver
  765. --lxc-conf
  766. --mac-address
  767. --memory -m
  768. --memory-swap
  769. --name
  770. --net
  771. --pid
  772. --publish -p
  773. --restart
  774. --security-opt
  775. --user -u
  776. --ulimit
  777. --uts
  778. --volumes-from
  779. --volume -v
  780. --workdir -w
  781. "
  782. local all_options="$options_with_args
  783. --help
  784. --interactive -i
  785. --privileged
  786. --publish-all -P
  787. --read-only
  788. --tty -t
  789. "
  790. [ "$command" = "run" ] && all_options="$all_options
  791. --detach -d
  792. --rm
  793. --sig-proxy
  794. "
  795. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  796. case "$prev" in
  797. --add-host)
  798. case "$cur" in
  799. *:)
  800. __docker_resolve_hostname
  801. return
  802. ;;
  803. esac
  804. ;;
  805. --attach|-a)
  806. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  807. return
  808. ;;
  809. --cap-add|--cap-drop)
  810. __docker_capabilities
  811. return
  812. ;;
  813. --cidfile|--env-file|--label-file)
  814. _filedir
  815. return
  816. ;;
  817. --device|--volume|-v)
  818. case "$cur" in
  819. *:*)
  820. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  821. ;;
  822. '')
  823. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  824. compopt -o nospace
  825. ;;
  826. /*)
  827. _filedir
  828. compopt -o nospace
  829. ;;
  830. esac
  831. return
  832. ;;
  833. --env|-e)
  834. COMPREPLY=( $( compgen -e -- "$cur" ) )
  835. compopt -o nospace
  836. return
  837. ;;
  838. --ipc)
  839. case "$cur" in
  840. *:*)
  841. cur="${cur#*:}"
  842. __docker_containers_running
  843. ;;
  844. *)
  845. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  846. if [ "$COMPREPLY" = "container:" ]; then
  847. compopt -o nospace
  848. fi
  849. ;;
  850. esac
  851. return
  852. ;;
  853. --link)
  854. case "$cur" in
  855. *:*)
  856. ;;
  857. *)
  858. __docker_containers_running
  859. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  860. compopt -o nospace
  861. ;;
  862. esac
  863. return
  864. ;;
  865. --log-driver)
  866. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur") )
  867. return
  868. ;;
  869. --net)
  870. case "$cur" in
  871. container:*)
  872. local cur=${cur#*:}
  873. __docker_containers_all
  874. ;;
  875. *)
  876. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  877. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  878. compopt -o nospace
  879. fi
  880. ;;
  881. esac
  882. return
  883. ;;
  884. --restart)
  885. case "$cur" in
  886. on-failure:*)
  887. ;;
  888. *)
  889. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  890. ;;
  891. esac
  892. return
  893. ;;
  894. --security-opt)
  895. case "$cur" in
  896. label:*:*)
  897. ;;
  898. label:*)
  899. local cur=${cur##*:}
  900. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  901. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  902. compopt -o nospace
  903. fi
  904. ;;
  905. *)
  906. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  907. compopt -o nospace
  908. ;;
  909. esac
  910. return
  911. ;;
  912. --volumes-from)
  913. __docker_containers_all
  914. return
  915. ;;
  916. $options_with_args_glob )
  917. return
  918. ;;
  919. esac
  920. case "$cur" in
  921. -*)
  922. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  923. ;;
  924. *)
  925. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  926. if [ $cword -eq $counter ]; then
  927. __docker_image_repos_and_tags_and_ids
  928. fi
  929. ;;
  930. esac
  931. }
  932. _docker_save() {
  933. case "$prev" in
  934. --output|-o)
  935. _filedir
  936. return
  937. ;;
  938. esac
  939. case "$cur" in
  940. -*)
  941. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  942. ;;
  943. *)
  944. __docker_image_repos_and_tags_and_ids
  945. ;;
  946. esac
  947. }
  948. _docker_search() {
  949. case "$prev" in
  950. --stars|-s)
  951. return
  952. ;;
  953. esac
  954. case "$cur" in
  955. -*)
  956. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  957. ;;
  958. esac
  959. }
  960. _docker_start() {
  961. case "$cur" in
  962. -*)
  963. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  964. ;;
  965. *)
  966. __docker_containers_stopped
  967. ;;
  968. esac
  969. }
  970. _docker_stats() {
  971. case "$cur" in
  972. -*)
  973. COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) )
  974. ;;
  975. *)
  976. __docker_containers_running
  977. ;;
  978. esac
  979. }
  980. _docker_stop() {
  981. case "$prev" in
  982. --time|-t)
  983. return
  984. ;;
  985. esac
  986. case "$cur" in
  987. -*)
  988. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  989. ;;
  990. *)
  991. __docker_containers_running
  992. ;;
  993. esac
  994. }
  995. _docker_tag() {
  996. case "$cur" in
  997. -*)
  998. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  999. ;;
  1000. *)
  1001. local counter=$(__docker_pos_first_nonflag)
  1002. if [ $cword -eq $counter ]; then
  1003. __docker_image_repos_and_tags
  1004. return
  1005. fi
  1006. (( counter++ ))
  1007. if [ $cword -eq $counter ]; then
  1008. __docker_image_repos_and_tags
  1009. return
  1010. fi
  1011. ;;
  1012. esac
  1013. }
  1014. _docker_unpause() {
  1015. case "$cur" in
  1016. -*)
  1017. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1018. ;;
  1019. *)
  1020. local counter=$(__docker_pos_first_nonflag)
  1021. if [ $cword -eq $counter ]; then
  1022. __docker_containers_unpauseable
  1023. fi
  1024. ;;
  1025. esac
  1026. }
  1027. _docker_top() {
  1028. case "$cur" in
  1029. -*)
  1030. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1031. ;;
  1032. *)
  1033. local counter=$(__docker_pos_first_nonflag)
  1034. if [ $cword -eq $counter ]; then
  1035. __docker_containers_running
  1036. fi
  1037. ;;
  1038. esac
  1039. }
  1040. _docker_version() {
  1041. case "$cur" in
  1042. -*)
  1043. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1044. ;;
  1045. esac
  1046. }
  1047. _docker_wait() {
  1048. case "$cur" in
  1049. -*)
  1050. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1051. ;;
  1052. *)
  1053. __docker_containers_all
  1054. ;;
  1055. esac
  1056. }
  1057. _docker() {
  1058. local previous_extglob_setting=$(shopt -p extglob)
  1059. shopt -s extglob
  1060. local commands=(
  1061. attach
  1062. build
  1063. commit
  1064. cp
  1065. create
  1066. diff
  1067. events
  1068. exec
  1069. export
  1070. history
  1071. images
  1072. import
  1073. info
  1074. inspect
  1075. kill
  1076. load
  1077. login
  1078. logout
  1079. logs
  1080. pause
  1081. port
  1082. ps
  1083. pull
  1084. push
  1085. rename
  1086. restart
  1087. rm
  1088. rmi
  1089. run
  1090. save
  1091. search
  1092. start
  1093. stats
  1094. stop
  1095. tag
  1096. top
  1097. unpause
  1098. version
  1099. wait
  1100. )
  1101. local main_options_with_args="
  1102. --api-cors-header
  1103. --bip
  1104. --bridge -b
  1105. --default-gateway
  1106. --default-gateway-v6
  1107. --default-ulimit
  1108. --dns
  1109. --dns-search
  1110. --exec-driver -e
  1111. --exec-opt
  1112. --exec-root
  1113. --fixed-cidr
  1114. --fixed-cidr-v6
  1115. --graph -g
  1116. --group -G
  1117. --host -H
  1118. --insecure-registry
  1119. --ip
  1120. --label
  1121. --log-driver
  1122. --log-level -l
  1123. --mtu
  1124. --pidfile -p
  1125. --registry-mirror
  1126. --storage-driver -s
  1127. --storage-opt
  1128. --tlscacert
  1129. --tlscert
  1130. --tlskey
  1131. "
  1132. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1133. local host
  1134. COMPREPLY=()
  1135. local cur prev words cword
  1136. _get_comp_words_by_ref -n : cur prev words cword
  1137. local command='docker' cpos=0
  1138. local counter=1
  1139. while [ $counter -lt $cword ]; do
  1140. case "${words[$counter]}" in
  1141. # save host so that completion can use custom daemon
  1142. --host|-H)
  1143. (( counter++ ))
  1144. host="${words[$counter]}"
  1145. ;;
  1146. $main_options_with_args_glob )
  1147. (( counter++ ))
  1148. ;;
  1149. -*)
  1150. ;;
  1151. =)
  1152. (( counter++ ))
  1153. ;;
  1154. *)
  1155. command="${words[$counter]}"
  1156. cpos=$counter
  1157. (( cpos++ ))
  1158. break
  1159. ;;
  1160. esac
  1161. (( counter++ ))
  1162. done
  1163. local completions_func=_docker_${command}
  1164. declare -F $completions_func >/dev/null && $completions_func
  1165. eval "$previous_extglob_setting"
  1166. return 0
  1167. }
  1168. complete -F _docker docker