docker 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 --cpu-quota --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 --privileged -t --tty -u --user" -- "$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. for arg in "${COMP_WORDS[@]}"; do
  619. case "$arg" in
  620. --all-tags|-a)
  621. __docker_image_repos
  622. return
  623. ;;
  624. esac
  625. done
  626. __docker_image_repos_and_tags
  627. fi
  628. ;;
  629. esac
  630. }
  631. _docker_push() {
  632. case "$cur" in
  633. -*)
  634. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  635. ;;
  636. *)
  637. local counter=$(__docker_pos_first_nonflag)
  638. if [ $cword -eq $counter ]; then
  639. __docker_image_repos_and_tags
  640. fi
  641. ;;
  642. esac
  643. }
  644. _docker_rename() {
  645. case "$cur" in
  646. -*)
  647. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  648. ;;
  649. *)
  650. local counter=$(__docker_pos_first_nonflag)
  651. if [ $cword -eq $counter ]; then
  652. __docker_containers_all
  653. fi
  654. ;;
  655. esac
  656. }
  657. _docker_restart() {
  658. case "$prev" in
  659. --time|-t)
  660. return
  661. ;;
  662. esac
  663. case "$cur" in
  664. -*)
  665. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  666. ;;
  667. *)
  668. __docker_containers_all
  669. ;;
  670. esac
  671. }
  672. _docker_rm() {
  673. case "$cur" in
  674. -*)
  675. COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  676. ;;
  677. *)
  678. for arg in "${COMP_WORDS[@]}"; do
  679. case "$arg" in
  680. --force|-f)
  681. __docker_containers_all
  682. return
  683. ;;
  684. esac
  685. done
  686. __docker_containers_stopped
  687. ;;
  688. esac
  689. }
  690. _docker_rmi() {
  691. case "$cur" in
  692. -*)
  693. COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  694. ;;
  695. *)
  696. __docker_image_repos_and_tags_and_ids
  697. ;;
  698. esac
  699. }
  700. _docker_run() {
  701. local options_with_args="
  702. --add-host
  703. --attach -a
  704. --cap-add
  705. --cap-drop
  706. --cgroup-parent
  707. --cidfile
  708. --cpuset
  709. --cpu-shares -c
  710. --cpu-quota
  711. --device
  712. --dns
  713. --dns-search
  714. --entrypoint
  715. --env -e
  716. --env-file
  717. --expose
  718. --hostname -h
  719. --ipc
  720. --label -l
  721. --label-file
  722. --link
  723. --log-driver
  724. --lxc-conf
  725. --mac-address
  726. --memory -m
  727. --memory-swap
  728. --name
  729. --net
  730. --pid
  731. --publish -p
  732. --restart
  733. --security-opt
  734. --user -u
  735. --ulimit
  736. --volumes-from
  737. --volume -v
  738. --workdir -w
  739. "
  740. local all_options="$options_with_args
  741. --help
  742. --interactive -i
  743. --privileged
  744. --publish-all -P
  745. --read-only
  746. --tty -t
  747. "
  748. [ "$command" = "run" ] && all_options="$all_options
  749. --detach -d
  750. --rm
  751. --sig-proxy
  752. "
  753. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  754. case "$prev" in
  755. --add-host)
  756. case "$cur" in
  757. *:)
  758. __docker_resolve_hostname
  759. return
  760. ;;
  761. esac
  762. ;;
  763. --attach|-a)
  764. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  765. return
  766. ;;
  767. --cap-add|--cap-drop)
  768. __docker_capabilities
  769. return
  770. ;;
  771. --cidfile|--env-file|--label-file)
  772. _filedir
  773. return
  774. ;;
  775. --device|--volume|-v)
  776. case "$cur" in
  777. *:*)
  778. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  779. ;;
  780. '')
  781. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  782. compopt -o nospace
  783. ;;
  784. /*)
  785. _filedir
  786. compopt -o nospace
  787. ;;
  788. esac
  789. return
  790. ;;
  791. --env|-e)
  792. COMPREPLY=( $( compgen -e -- "$cur" ) )
  793. compopt -o nospace
  794. return
  795. ;;
  796. --ipc)
  797. case "$cur" in
  798. *:*)
  799. cur="${cur#*:}"
  800. __docker_containers_running
  801. ;;
  802. *)
  803. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  804. if [ "$COMPREPLY" = "container:" ]; then
  805. compopt -o nospace
  806. fi
  807. ;;
  808. esac
  809. return
  810. ;;
  811. --link)
  812. case "$cur" in
  813. *:*)
  814. ;;
  815. *)
  816. __docker_containers_running
  817. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  818. compopt -o nospace
  819. ;;
  820. esac
  821. return
  822. ;;
  823. --log-driver)
  824. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur") )
  825. return
  826. ;;
  827. --net)
  828. case "$cur" in
  829. container:*)
  830. local cur=${cur#*:}
  831. __docker_containers_all
  832. ;;
  833. *)
  834. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  835. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  836. compopt -o nospace
  837. fi
  838. ;;
  839. esac
  840. return
  841. ;;
  842. --restart)
  843. case "$cur" in
  844. on-failure:*)
  845. ;;
  846. *)
  847. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  848. ;;
  849. esac
  850. return
  851. ;;
  852. --security-opt)
  853. case "$cur" in
  854. label:*:*)
  855. ;;
  856. label:*)
  857. local cur=${cur##*:}
  858. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  859. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  860. compopt -o nospace
  861. fi
  862. ;;
  863. *)
  864. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  865. compopt -o nospace
  866. ;;
  867. esac
  868. return
  869. ;;
  870. --volumes-from)
  871. __docker_containers_all
  872. return
  873. ;;
  874. $options_with_args_glob )
  875. return
  876. ;;
  877. esac
  878. case "$cur" in
  879. -*)
  880. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  881. ;;
  882. *)
  883. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  884. if [ $cword -eq $counter ]; then
  885. __docker_image_repos_and_tags_and_ids
  886. fi
  887. ;;
  888. esac
  889. }
  890. _docker_save() {
  891. case "$prev" in
  892. --output|-o)
  893. _filedir
  894. return
  895. ;;
  896. esac
  897. case "$cur" in
  898. -*)
  899. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  900. ;;
  901. *)
  902. __docker_image_repos_and_tags_and_ids
  903. ;;
  904. esac
  905. }
  906. _docker_search() {
  907. case "$prev" in
  908. --stars|-s)
  909. return
  910. ;;
  911. esac
  912. case "$cur" in
  913. -*)
  914. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  915. ;;
  916. esac
  917. }
  918. _docker_start() {
  919. case "$cur" in
  920. -*)
  921. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  922. ;;
  923. *)
  924. __docker_containers_stopped
  925. ;;
  926. esac
  927. }
  928. _docker_stats() {
  929. case "$cur" in
  930. -*)
  931. COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) )
  932. ;;
  933. *)
  934. __docker_containers_running
  935. ;;
  936. esac
  937. }
  938. _docker_stop() {
  939. case "$prev" in
  940. --time|-t)
  941. return
  942. ;;
  943. esac
  944. case "$cur" in
  945. -*)
  946. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  947. ;;
  948. *)
  949. __docker_containers_running
  950. ;;
  951. esac
  952. }
  953. _docker_tag() {
  954. case "$cur" in
  955. -*)
  956. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  957. ;;
  958. *)
  959. local counter=$(__docker_pos_first_nonflag)
  960. if [ $cword -eq $counter ]; then
  961. __docker_image_repos_and_tags
  962. return
  963. fi
  964. (( counter++ ))
  965. if [ $cword -eq $counter ]; then
  966. __docker_image_repos_and_tags
  967. return
  968. fi
  969. ;;
  970. esac
  971. }
  972. _docker_unpause() {
  973. case "$cur" in
  974. -*)
  975. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  976. ;;
  977. *)
  978. local counter=$(__docker_pos_first_nonflag)
  979. if [ $cword -eq $counter ]; then
  980. __docker_containers_unpauseable
  981. fi
  982. ;;
  983. esac
  984. }
  985. _docker_top() {
  986. case "$cur" in
  987. -*)
  988. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  989. ;;
  990. *)
  991. local counter=$(__docker_pos_first_nonflag)
  992. if [ $cword -eq $counter ]; then
  993. __docker_containers_running
  994. fi
  995. ;;
  996. esac
  997. }
  998. _docker_version() {
  999. case "$cur" in
  1000. -*)
  1001. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1002. ;;
  1003. esac
  1004. }
  1005. _docker_wait() {
  1006. case "$cur" in
  1007. -*)
  1008. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1009. ;;
  1010. *)
  1011. __docker_containers_all
  1012. ;;
  1013. esac
  1014. }
  1015. _docker() {
  1016. local previous_extglob_setting=$(shopt -p extglob)
  1017. shopt -s extglob
  1018. local commands=(
  1019. attach
  1020. build
  1021. commit
  1022. cp
  1023. create
  1024. diff
  1025. events
  1026. exec
  1027. export
  1028. history
  1029. images
  1030. import
  1031. info
  1032. inspect
  1033. kill
  1034. load
  1035. login
  1036. logout
  1037. logs
  1038. pause
  1039. port
  1040. ps
  1041. pull
  1042. push
  1043. rename
  1044. restart
  1045. rm
  1046. rmi
  1047. run
  1048. save
  1049. search
  1050. start
  1051. stats
  1052. stop
  1053. tag
  1054. top
  1055. unpause
  1056. version
  1057. wait
  1058. )
  1059. local main_options_with_args="
  1060. --api-cors-header
  1061. --bip
  1062. --bridge -b
  1063. --default-ulimit
  1064. --dns
  1065. --dns-search
  1066. --exec-driver -e
  1067. --exec-opt
  1068. --fixed-cidr
  1069. --fixed-cidr-v6
  1070. --graph -g
  1071. --group -G
  1072. --host -H
  1073. --insecure-registry
  1074. --ip
  1075. --label
  1076. --log-driver
  1077. --log-level -l
  1078. --mtu
  1079. --pidfile -p
  1080. --registry-mirror
  1081. --storage-driver -s
  1082. --storage-opt
  1083. --tlscacert
  1084. --tlscert
  1085. --tlskey
  1086. "
  1087. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1088. COMPREPLY=()
  1089. local cur prev words cword
  1090. _get_comp_words_by_ref -n : cur prev words cword
  1091. local command='docker' cpos=0
  1092. local counter=1
  1093. while [ $counter -lt $cword ]; do
  1094. case "${words[$counter]}" in
  1095. $main_options_with_args_glob )
  1096. (( counter++ ))
  1097. ;;
  1098. -*)
  1099. ;;
  1100. *)
  1101. command="${words[$counter]}"
  1102. cpos=$counter
  1103. (( cpos++ ))
  1104. break
  1105. ;;
  1106. esac
  1107. (( counter++ ))
  1108. done
  1109. local completions_func=_docker_${command}
  1110. declare -F $completions_func >/dev/null && $completions_func
  1111. eval "$previous_extglob_setting"
  1112. return 0
  1113. }
  1114. complete -F _docker docker