docker 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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 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. --version -v
  195. "
  196. case "$prev" in
  197. --graph|-g)
  198. _filedir -d
  199. return
  200. ;;
  201. --log-driver)
  202. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur" ) )
  203. return
  204. ;;
  205. --log-level|-l)
  206. COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
  207. return
  208. ;;
  209. --pidfile|-p|--tlscacert|--tlscert|--tlskey)
  210. _filedir
  211. return
  212. ;;
  213. --storage-driver|-s)
  214. COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
  215. return
  216. ;;
  217. $main_options_with_args_glob )
  218. return
  219. ;;
  220. esac
  221. case "$cur" in
  222. -*)
  223. COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) )
  224. ;;
  225. *)
  226. COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
  227. ;;
  228. esac
  229. }
  230. _docker_attach() {
  231. case "$cur" in
  232. -*)
  233. COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) )
  234. ;;
  235. *)
  236. local counter="$(__docker_pos_first_nonflag)"
  237. if [ $cword -eq $counter ]; then
  238. __docker_containers_running
  239. fi
  240. ;;
  241. esac
  242. }
  243. _docker_build() {
  244. case "$prev" in
  245. --tag|-t)
  246. __docker_image_repos_and_tags
  247. return
  248. ;;
  249. --file|-f)
  250. _filedir
  251. return
  252. ;;
  253. esac
  254. case "$cur" in
  255. -*)
  256. COMPREPLY=( $( compgen -W "--cpu-shares -c --cpuset-cpus --file -f --force-rm --help --memory -m --memory-swap --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
  257. ;;
  258. *)
  259. local counter="$(__docker_pos_first_nonflag '--tag|-t')"
  260. if [ $cword -eq $counter ]; then
  261. _filedir -d
  262. fi
  263. ;;
  264. esac
  265. }
  266. _docker_commit() {
  267. case "$prev" in
  268. --author|-a|--change|-c|--message|-m)
  269. return
  270. ;;
  271. esac
  272. case "$cur" in
  273. -*)
  274. COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
  275. ;;
  276. *)
  277. local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
  278. if [ $cword -eq $counter ]; then
  279. __docker_containers_all
  280. return
  281. fi
  282. (( counter++ ))
  283. if [ $cword -eq $counter ]; then
  284. __docker_image_repos_and_tags
  285. return
  286. fi
  287. ;;
  288. esac
  289. }
  290. _docker_cp() {
  291. case "$cur" in
  292. -*)
  293. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  294. ;;
  295. *)
  296. local counter=$(__docker_pos_first_nonflag)
  297. if [ $cword -eq $counter ]; then
  298. case "$cur" in
  299. *:)
  300. return
  301. ;;
  302. *)
  303. __docker_containers_all
  304. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  305. compopt -o nospace
  306. return
  307. ;;
  308. esac
  309. fi
  310. (( counter++ ))
  311. if [ $cword -eq $counter ]; then
  312. _filedir -d
  313. return
  314. fi
  315. ;;
  316. esac
  317. }
  318. _docker_create() {
  319. _docker_run
  320. }
  321. _docker_diff() {
  322. case "$cur" in
  323. -*)
  324. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  325. ;;
  326. *)
  327. local counter=$(__docker_pos_first_nonflag)
  328. if [ $cword -eq $counter ]; then
  329. __docker_containers_all
  330. fi
  331. ;;
  332. esac
  333. }
  334. _docker_events() {
  335. case "$prev" in
  336. --filter|-f)
  337. COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) )
  338. compopt -o nospace
  339. return
  340. ;;
  341. --since|--until)
  342. return
  343. ;;
  344. esac
  345. # "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
  346. # it is the last character or not. So we search for "xxx=" in the the last two words.
  347. case "${words[$cword-2]}$prev=" in
  348. *container=*)
  349. cur="${cur#=}"
  350. __docker_containers_all
  351. return
  352. ;;
  353. *event=*)
  354. COMPREPLY=( $( compgen -W "create destroy die export kill pause restart start stop unpause" -- "${cur#=}" ) )
  355. return
  356. ;;
  357. *image=*)
  358. cur="${cur#=}"
  359. __docker_image_repos_and_tags_and_ids
  360. return
  361. ;;
  362. esac
  363. case "$cur" in
  364. -*)
  365. COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
  366. ;;
  367. esac
  368. }
  369. _docker_exec() {
  370. case "$cur" in
  371. -*)
  372. COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty" -- "$cur" ) )
  373. ;;
  374. *)
  375. __docker_containers_running
  376. ;;
  377. esac
  378. }
  379. _docker_export() {
  380. case "$cur" in
  381. -*)
  382. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  383. ;;
  384. *)
  385. local counter=$(__docker_pos_first_nonflag)
  386. if [ $cword -eq $counter ]; then
  387. __docker_containers_all
  388. fi
  389. ;;
  390. esac
  391. }
  392. _docker_help() {
  393. local counter=$(__docker_pos_first_nonflag)
  394. if [ $cword -eq $counter ]; then
  395. COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  396. fi
  397. }
  398. _docker_history() {
  399. case "$cur" in
  400. -*)
  401. COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  402. ;;
  403. *)
  404. local counter=$(__docker_pos_first_nonflag)
  405. if [ $cword -eq $counter ]; then
  406. __docker_image_repos_and_tags_and_ids
  407. fi
  408. ;;
  409. esac
  410. }
  411. _docker_images() {
  412. case "$prev" in
  413. --filter|-f)
  414. COMPREPLY=( $( compgen -W "dangling=true label=" -- "$cur" ) )
  415. if [ "$COMPREPLY" = "label=" ]; then
  416. compopt -o nospace
  417. fi
  418. return
  419. ;;
  420. esac
  421. case "${words[$cword-2]}$prev=" in
  422. *dangling=*)
  423. COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  424. return
  425. ;;
  426. *label=*)
  427. return
  428. ;;
  429. esac
  430. case "$cur" in
  431. -*)
  432. COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  433. ;;
  434. =)
  435. return
  436. ;;
  437. *)
  438. __docker_image_repos
  439. ;;
  440. esac
  441. }
  442. _docker_import() {
  443. case "$cur" in
  444. -*)
  445. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  446. ;;
  447. *)
  448. local counter=$(__docker_pos_first_nonflag)
  449. if [ $cword -eq $counter ]; then
  450. return
  451. fi
  452. (( counter++ ))
  453. if [ $cword -eq $counter ]; then
  454. __docker_image_repos_and_tags
  455. return
  456. fi
  457. ;;
  458. esac
  459. }
  460. _docker_info() {
  461. case "$cur" in
  462. -*)
  463. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  464. ;;
  465. esac
  466. }
  467. _docker_inspect() {
  468. case "$prev" in
  469. --format|-f)
  470. return
  471. ;;
  472. esac
  473. case "$cur" in
  474. -*)
  475. COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
  476. ;;
  477. *)
  478. __docker_containers_and_images
  479. ;;
  480. esac
  481. }
  482. _docker_kill() {
  483. case "$prev" in
  484. --signal|-s)
  485. __docker_signals
  486. return
  487. ;;
  488. esac
  489. case "$cur" in
  490. -*)
  491. COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  492. ;;
  493. *)
  494. __docker_containers_running
  495. ;;
  496. esac
  497. }
  498. _docker_load() {
  499. case "$prev" in
  500. --input|-i)
  501. _filedir
  502. return
  503. ;;
  504. esac
  505. case "$cur" in
  506. -*)
  507. COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  508. ;;
  509. esac
  510. }
  511. _docker_login() {
  512. case "$prev" in
  513. --email|-e|--password|-p|--username|-u)
  514. return
  515. ;;
  516. esac
  517. case "$cur" in
  518. -*)
  519. COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  520. ;;
  521. esac
  522. }
  523. _docker_logout() {
  524. case "$cur" in
  525. -*)
  526. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  527. ;;
  528. esac
  529. }
  530. _docker_logs() {
  531. case "$prev" in
  532. --tail)
  533. return
  534. ;;
  535. esac
  536. case "$cur" in
  537. -*)
  538. COMPREPLY=( $( compgen -W "--follow -f --help --tail --timestamps -t" -- "$cur" ) )
  539. ;;
  540. *)
  541. local counter=$(__docker_pos_first_nonflag '--tail')
  542. if [ $cword -eq $counter ]; then
  543. __docker_containers_all
  544. fi
  545. ;;
  546. esac
  547. }
  548. _docker_pause() {
  549. case "$cur" in
  550. -*)
  551. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  552. ;;
  553. *)
  554. local counter=$(__docker_pos_first_nonflag)
  555. if [ $cword -eq $counter ]; then
  556. __docker_containers_pauseable
  557. fi
  558. ;;
  559. esac
  560. }
  561. _docker_port() {
  562. case "$cur" in
  563. -*)
  564. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  565. ;;
  566. *)
  567. local counter=$(__docker_pos_first_nonflag)
  568. if [ $cword -eq $counter ]; then
  569. __docker_containers_all
  570. fi
  571. ;;
  572. esac
  573. }
  574. _docker_ps() {
  575. case "$prev" in
  576. --before|--since)
  577. __docker_containers_all
  578. ;;
  579. --filter|-f)
  580. COMPREPLY=( $( compgen -S = -W "exited id label name status" -- "$cur" ) )
  581. compopt -o nospace
  582. return
  583. ;;
  584. -n)
  585. return
  586. ;;
  587. esac
  588. case "${words[$cword-2]}$prev=" in
  589. *id=*)
  590. cur="${cur#=}"
  591. __docker_container_ids
  592. return
  593. ;;
  594. *name=*)
  595. cur="${cur#=}"
  596. __docker_container_names
  597. return
  598. ;;
  599. *status=*)
  600. COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
  601. return
  602. ;;
  603. esac
  604. case "$cur" in
  605. -*)
  606. COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  607. ;;
  608. esac
  609. }
  610. _docker_pull() {
  611. case "$cur" in
  612. -*)
  613. COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  614. ;;
  615. *)
  616. local counter=$(__docker_pos_first_nonflag)
  617. if [ $cword -eq $counter ]; then
  618. __docker_image_repos_and_tags
  619. fi
  620. ;;
  621. esac
  622. }
  623. _docker_push() {
  624. case "$cur" in
  625. -*)
  626. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  627. ;;
  628. *)
  629. local counter=$(__docker_pos_first_nonflag)
  630. if [ $cword -eq $counter ]; then
  631. __docker_image_repos_and_tags
  632. fi
  633. ;;
  634. esac
  635. }
  636. _docker_rename() {
  637. case "$cur" in
  638. -*)
  639. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  640. ;;
  641. *)
  642. local counter=$(__docker_pos_first_nonflag)
  643. if [ $cword -eq $counter ]; then
  644. __docker_containers_all
  645. fi
  646. ;;
  647. esac
  648. }
  649. _docker_restart() {
  650. case "$prev" in
  651. --time|-t)
  652. return
  653. ;;
  654. esac
  655. case "$cur" in
  656. -*)
  657. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  658. ;;
  659. *)
  660. __docker_containers_all
  661. ;;
  662. esac
  663. }
  664. _docker_rm() {
  665. case "$cur" in
  666. -*)
  667. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  668. ;;
  669. *)
  670. for arg in "${COMP_WORDS[@]}"; do
  671. case "$arg" in
  672. --force|-f)
  673. __docker_containers_all
  674. return
  675. ;;
  676. esac
  677. done
  678. __docker_containers_stopped
  679. ;;
  680. esac
  681. }
  682. _docker_rmi() {
  683. case "$cur" in
  684. -*)
  685. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  686. ;;
  687. *)
  688. __docker_image_repos_and_tags_and_ids
  689. ;;
  690. esac
  691. }
  692. _docker_run() {
  693. local options_with_args="
  694. --add-host
  695. --attach -a
  696. --cap-add
  697. --cap-drop
  698. --cgroup-parent
  699. --cidfile
  700. --cpuset
  701. --cpu-shares -c
  702. --device
  703. --dns
  704. --dns-search
  705. --entrypoint
  706. --env -e
  707. --env-file
  708. --expose
  709. --hostname -h
  710. --ipc
  711. --label -l
  712. --label-file
  713. --link
  714. --log-driver
  715. --lxc-conf
  716. --mac-address
  717. --memory -m
  718. --memory-swap
  719. --name
  720. --net
  721. --pid
  722. --publish -p
  723. --restart
  724. --security-opt
  725. --user -u
  726. --ulimit
  727. --volumes-from
  728. --volume -v
  729. --workdir -w
  730. "
  731. local all_options="$options_with_args
  732. --help
  733. --interactive -i
  734. --privileged
  735. --publish-all -P
  736. --read-only
  737. --tty -t
  738. "
  739. [ "$command" = "run" ] && all_options="$all_options
  740. --detach -d
  741. --rm
  742. --sig-proxy
  743. "
  744. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  745. case "$prev" in
  746. --add-host)
  747. case "$cur" in
  748. *:)
  749. __docker_resolve_hostname
  750. return
  751. ;;
  752. esac
  753. ;;
  754. --attach|-a)
  755. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  756. return
  757. ;;
  758. --cap-add|--cap-drop)
  759. __docker_capabilities
  760. return
  761. ;;
  762. --cidfile|--env-file|--label-file)
  763. _filedir
  764. return
  765. ;;
  766. --device|--volume|-v)
  767. case "$cur" in
  768. *:*)
  769. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  770. ;;
  771. '')
  772. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  773. compopt -o nospace
  774. ;;
  775. /*)
  776. _filedir
  777. compopt -o nospace
  778. ;;
  779. esac
  780. return
  781. ;;
  782. --env|-e)
  783. COMPREPLY=( $( compgen -e -- "$cur" ) )
  784. compopt -o nospace
  785. return
  786. ;;
  787. --ipc)
  788. case "$cur" in
  789. *:*)
  790. cur="${cur#*:}"
  791. __docker_containers_running
  792. ;;
  793. *)
  794. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  795. if [ "$COMPREPLY" = "container:" ]; then
  796. compopt -o nospace
  797. fi
  798. ;;
  799. esac
  800. return
  801. ;;
  802. --link)
  803. case "$cur" in
  804. *:*)
  805. ;;
  806. *)
  807. __docker_containers_running
  808. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  809. compopt -o nospace
  810. ;;
  811. esac
  812. return
  813. ;;
  814. --log-driver)
  815. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur") )
  816. return
  817. ;;
  818. --net)
  819. case "$cur" in
  820. container:*)
  821. local cur=${cur#*:}
  822. __docker_containers_all
  823. ;;
  824. *)
  825. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  826. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  827. compopt -o nospace
  828. fi
  829. ;;
  830. esac
  831. return
  832. ;;
  833. --restart)
  834. case "$cur" in
  835. on-failure:*)
  836. ;;
  837. *)
  838. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  839. ;;
  840. esac
  841. return
  842. ;;
  843. --security-opt)
  844. case "$cur" in
  845. label:*:*)
  846. ;;
  847. label:*)
  848. local cur=${cur##*:}
  849. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  850. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  851. compopt -o nospace
  852. fi
  853. ;;
  854. *)
  855. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  856. compopt -o nospace
  857. ;;
  858. esac
  859. return
  860. ;;
  861. --volumes-from)
  862. __docker_containers_all
  863. return
  864. ;;
  865. $options_with_args_glob )
  866. return
  867. ;;
  868. esac
  869. case "$cur" in
  870. -*)
  871. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  872. ;;
  873. *)
  874. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  875. if [ $cword -eq $counter ]; then
  876. __docker_image_repos_and_tags_and_ids
  877. fi
  878. ;;
  879. esac
  880. }
  881. _docker_save() {
  882. case "$prev" in
  883. --output|-o)
  884. _filedir
  885. return
  886. ;;
  887. esac
  888. case "$cur" in
  889. -*)
  890. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  891. ;;
  892. *)
  893. __docker_image_repos_and_tags_and_ids
  894. ;;
  895. esac
  896. }
  897. _docker_search() {
  898. case "$prev" in
  899. --stars|-s)
  900. return
  901. ;;
  902. esac
  903. case "$cur" in
  904. -*)
  905. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  906. ;;
  907. esac
  908. }
  909. _docker_start() {
  910. case "$cur" in
  911. -*)
  912. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  913. ;;
  914. *)
  915. __docker_containers_stopped
  916. ;;
  917. esac
  918. }
  919. _docker_stats() {
  920. case "$cur" in
  921. -*)
  922. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  923. ;;
  924. *)
  925. __docker_containers_running
  926. ;;
  927. esac
  928. }
  929. _docker_stop() {
  930. case "$prev" in
  931. --time|-t)
  932. return
  933. ;;
  934. esac
  935. case "$cur" in
  936. -*)
  937. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  938. ;;
  939. *)
  940. __docker_containers_running
  941. ;;
  942. esac
  943. }
  944. _docker_tag() {
  945. case "$cur" in
  946. -*)
  947. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  948. ;;
  949. *)
  950. local counter=$(__docker_pos_first_nonflag)
  951. if [ $cword -eq $counter ]; then
  952. __docker_image_repos_and_tags
  953. return
  954. fi
  955. (( counter++ ))
  956. if [ $cword -eq $counter ]; then
  957. __docker_image_repos_and_tags
  958. return
  959. fi
  960. ;;
  961. esac
  962. }
  963. _docker_unpause() {
  964. case "$cur" in
  965. -*)
  966. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  967. ;;
  968. *)
  969. local counter=$(__docker_pos_first_nonflag)
  970. if [ $cword -eq $counter ]; then
  971. __docker_containers_unpauseable
  972. fi
  973. ;;
  974. esac
  975. }
  976. _docker_top() {
  977. case "$cur" in
  978. -*)
  979. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  980. ;;
  981. *)
  982. local counter=$(__docker_pos_first_nonflag)
  983. if [ $cword -eq $counter ]; then
  984. __docker_containers_running
  985. fi
  986. ;;
  987. esac
  988. }
  989. _docker_version() {
  990. case "$cur" in
  991. -*)
  992. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  993. ;;
  994. esac
  995. }
  996. _docker_wait() {
  997. case "$cur" in
  998. -*)
  999. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1000. ;;
  1001. *)
  1002. __docker_containers_all
  1003. ;;
  1004. esac
  1005. }
  1006. _docker() {
  1007. local previous_extglob_setting=$(shopt -p extglob)
  1008. shopt -s extglob
  1009. local commands=(
  1010. attach
  1011. build
  1012. commit
  1013. cp
  1014. create
  1015. diff
  1016. events
  1017. exec
  1018. export
  1019. history
  1020. images
  1021. import
  1022. info
  1023. inspect
  1024. kill
  1025. load
  1026. login
  1027. logout
  1028. logs
  1029. pause
  1030. port
  1031. ps
  1032. pull
  1033. push
  1034. rename
  1035. restart
  1036. rm
  1037. rmi
  1038. run
  1039. save
  1040. search
  1041. start
  1042. stats
  1043. stop
  1044. tag
  1045. top
  1046. unpause
  1047. version
  1048. wait
  1049. )
  1050. local main_options_with_args="
  1051. --api-cors-header
  1052. --bip
  1053. --bridge -b
  1054. --default-ulimit
  1055. --dns
  1056. --dns-search
  1057. --exec-driver -e
  1058. --fixed-cidr
  1059. --fixed-cidr-v6
  1060. --graph -g
  1061. --group -G
  1062. --host -H
  1063. --insecure-registry
  1064. --ip
  1065. --label
  1066. --log-driver
  1067. --log-level -l
  1068. --mtu
  1069. --pidfile -p
  1070. --registry-mirror
  1071. --storage-driver -s
  1072. --storage-opt
  1073. --tlscacert
  1074. --tlscert
  1075. --tlskey
  1076. "
  1077. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1078. COMPREPLY=()
  1079. local cur prev words cword
  1080. _get_comp_words_by_ref -n : cur prev words cword
  1081. local command='docker' cpos=0
  1082. local counter=1
  1083. while [ $counter -lt $cword ]; do
  1084. case "${words[$counter]}" in
  1085. $main_options_with_args_glob )
  1086. (( counter++ ))
  1087. ;;
  1088. -*)
  1089. ;;
  1090. *)
  1091. command="${words[$counter]}"
  1092. cpos=$counter
  1093. (( cpos++ ))
  1094. break
  1095. ;;
  1096. esac
  1097. (( counter++ ))
  1098. done
  1099. local completions_func=_docker_${command}
  1100. declare -F $completions_func >/dev/null && $completions_func
  1101. eval "$previous_extglob_setting"
  1102. return 0
  1103. }
  1104. complete -F _docker docker