docker 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. --group-add
  760. --hostname -h
  761. --ipc
  762. --label -l
  763. --label-file
  764. --link
  765. --log-driver
  766. --lxc-conf
  767. --mac-address
  768. --memory -m
  769. --memory-swap
  770. --name
  771. --net
  772. --pid
  773. --publish -p
  774. --restart
  775. --security-opt
  776. --user -u
  777. --ulimit
  778. --uts
  779. --volumes-from
  780. --volume -v
  781. --workdir -w
  782. "
  783. local all_options="$options_with_args
  784. --help
  785. --interactive -i
  786. --privileged
  787. --publish-all -P
  788. --read-only
  789. --tty -t
  790. "
  791. [ "$command" = "run" ] && all_options="$all_options
  792. --detach -d
  793. --rm
  794. --sig-proxy
  795. "
  796. local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
  797. case "$prev" in
  798. --add-host)
  799. case "$cur" in
  800. *:)
  801. __docker_resolve_hostname
  802. return
  803. ;;
  804. esac
  805. ;;
  806. --attach|-a)
  807. COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  808. return
  809. ;;
  810. --cap-add|--cap-drop)
  811. __docker_capabilities
  812. return
  813. ;;
  814. --cidfile|--env-file|--label-file)
  815. _filedir
  816. return
  817. ;;
  818. --device|--volume|-v)
  819. case "$cur" in
  820. *:*)
  821. # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  822. ;;
  823. '')
  824. COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  825. compopt -o nospace
  826. ;;
  827. /*)
  828. _filedir
  829. compopt -o nospace
  830. ;;
  831. esac
  832. return
  833. ;;
  834. --env|-e)
  835. COMPREPLY=( $( compgen -e -- "$cur" ) )
  836. compopt -o nospace
  837. return
  838. ;;
  839. --ipc)
  840. case "$cur" in
  841. *:*)
  842. cur="${cur#*:}"
  843. __docker_containers_running
  844. ;;
  845. *)
  846. COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  847. if [ "$COMPREPLY" = "container:" ]; then
  848. compopt -o nospace
  849. fi
  850. ;;
  851. esac
  852. return
  853. ;;
  854. --link)
  855. case "$cur" in
  856. *:*)
  857. ;;
  858. *)
  859. __docker_containers_running
  860. COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  861. compopt -o nospace
  862. ;;
  863. esac
  864. return
  865. ;;
  866. --log-driver)
  867. COMPREPLY=( $( compgen -W "json-file syslog none" -- "$cur") )
  868. return
  869. ;;
  870. --net)
  871. case "$cur" in
  872. container:*)
  873. local cur=${cur#*:}
  874. __docker_containers_all
  875. ;;
  876. *)
  877. COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
  878. if [ "${COMPREPLY[*]}" = "container:" ] ; then
  879. compopt -o nospace
  880. fi
  881. ;;
  882. esac
  883. return
  884. ;;
  885. --restart)
  886. case "$cur" in
  887. on-failure:*)
  888. ;;
  889. *)
  890. COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
  891. ;;
  892. esac
  893. return
  894. ;;
  895. --security-opt)
  896. case "$cur" in
  897. label:*:*)
  898. ;;
  899. label:*)
  900. local cur=${cur##*:}
  901. COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  902. if [ "${COMPREPLY[*]}" != "disable" ] ; then
  903. compopt -o nospace
  904. fi
  905. ;;
  906. *)
  907. COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
  908. compopt -o nospace
  909. ;;
  910. esac
  911. return
  912. ;;
  913. --volumes-from)
  914. __docker_containers_all
  915. return
  916. ;;
  917. $options_with_args_glob )
  918. return
  919. ;;
  920. esac
  921. case "$cur" in
  922. -*)
  923. COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  924. ;;
  925. *)
  926. local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  927. if [ $cword -eq $counter ]; then
  928. __docker_image_repos_and_tags_and_ids
  929. fi
  930. ;;
  931. esac
  932. }
  933. _docker_save() {
  934. case "$prev" in
  935. --output|-o)
  936. _filedir
  937. return
  938. ;;
  939. esac
  940. case "$cur" in
  941. -*)
  942. COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  943. ;;
  944. *)
  945. __docker_image_repos_and_tags_and_ids
  946. ;;
  947. esac
  948. }
  949. _docker_search() {
  950. case "$prev" in
  951. --stars|-s)
  952. return
  953. ;;
  954. esac
  955. case "$cur" in
  956. -*)
  957. COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  958. ;;
  959. esac
  960. }
  961. _docker_start() {
  962. case "$cur" in
  963. -*)
  964. COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
  965. ;;
  966. *)
  967. __docker_containers_stopped
  968. ;;
  969. esac
  970. }
  971. _docker_stats() {
  972. case "$cur" in
  973. -*)
  974. COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) )
  975. ;;
  976. *)
  977. __docker_containers_running
  978. ;;
  979. esac
  980. }
  981. _docker_stop() {
  982. case "$prev" in
  983. --time|-t)
  984. return
  985. ;;
  986. esac
  987. case "$cur" in
  988. -*)
  989. COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  990. ;;
  991. *)
  992. __docker_containers_running
  993. ;;
  994. esac
  995. }
  996. _docker_tag() {
  997. case "$cur" in
  998. -*)
  999. COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
  1000. ;;
  1001. *)
  1002. local counter=$(__docker_pos_first_nonflag)
  1003. if [ $cword -eq $counter ]; then
  1004. __docker_image_repos_and_tags
  1005. return
  1006. fi
  1007. (( counter++ ))
  1008. if [ $cword -eq $counter ]; then
  1009. __docker_image_repos_and_tags
  1010. return
  1011. fi
  1012. ;;
  1013. esac
  1014. }
  1015. _docker_unpause() {
  1016. case "$cur" in
  1017. -*)
  1018. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1019. ;;
  1020. *)
  1021. local counter=$(__docker_pos_first_nonflag)
  1022. if [ $cword -eq $counter ]; then
  1023. __docker_containers_unpauseable
  1024. fi
  1025. ;;
  1026. esac
  1027. }
  1028. _docker_top() {
  1029. case "$cur" in
  1030. -*)
  1031. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1032. ;;
  1033. *)
  1034. local counter=$(__docker_pos_first_nonflag)
  1035. if [ $cword -eq $counter ]; then
  1036. __docker_containers_running
  1037. fi
  1038. ;;
  1039. esac
  1040. }
  1041. _docker_version() {
  1042. case "$cur" in
  1043. -*)
  1044. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1045. ;;
  1046. esac
  1047. }
  1048. _docker_wait() {
  1049. case "$cur" in
  1050. -*)
  1051. COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1052. ;;
  1053. *)
  1054. __docker_containers_all
  1055. ;;
  1056. esac
  1057. }
  1058. _docker() {
  1059. local previous_extglob_setting=$(shopt -p extglob)
  1060. shopt -s extglob
  1061. local commands=(
  1062. attach
  1063. build
  1064. commit
  1065. cp
  1066. create
  1067. diff
  1068. events
  1069. exec
  1070. export
  1071. history
  1072. images
  1073. import
  1074. info
  1075. inspect
  1076. kill
  1077. load
  1078. login
  1079. logout
  1080. logs
  1081. pause
  1082. port
  1083. ps
  1084. pull
  1085. push
  1086. rename
  1087. restart
  1088. rm
  1089. rmi
  1090. run
  1091. save
  1092. search
  1093. start
  1094. stats
  1095. stop
  1096. tag
  1097. top
  1098. unpause
  1099. version
  1100. wait
  1101. )
  1102. local main_options_with_args="
  1103. --api-cors-header
  1104. --bip
  1105. --bridge -b
  1106. --default-gateway
  1107. --default-gateway-v6
  1108. --default-ulimit
  1109. --dns
  1110. --dns-search
  1111. --exec-driver -e
  1112. --exec-opt
  1113. --exec-root
  1114. --fixed-cidr
  1115. --fixed-cidr-v6
  1116. --graph -g
  1117. --group -G
  1118. --host -H
  1119. --insecure-registry
  1120. --ip
  1121. --label
  1122. --log-driver
  1123. --log-level -l
  1124. --mtu
  1125. --pidfile -p
  1126. --registry-mirror
  1127. --storage-driver -s
  1128. --storage-opt
  1129. --tlscacert
  1130. --tlscert
  1131. --tlskey
  1132. "
  1133. local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1134. local host
  1135. COMPREPLY=()
  1136. local cur prev words cword
  1137. _get_comp_words_by_ref -n : cur prev words cword
  1138. local command='docker' cpos=0
  1139. local counter=1
  1140. while [ $counter -lt $cword ]; do
  1141. case "${words[$counter]}" in
  1142. # save host so that completion can use custom daemon
  1143. --host|-H)
  1144. (( counter++ ))
  1145. host="${words[$counter]}"
  1146. ;;
  1147. $main_options_with_args_glob )
  1148. (( counter++ ))
  1149. ;;
  1150. -*)
  1151. ;;
  1152. =)
  1153. (( counter++ ))
  1154. ;;
  1155. *)
  1156. command="${words[$counter]}"
  1157. cpos=$counter
  1158. (( cpos++ ))
  1159. break
  1160. ;;
  1161. esac
  1162. (( counter++ ))
  1163. done
  1164. local completions_func=_docker_${command}
  1165. declare -F $completions_func >/dev/null && $completions_func
  1166. eval "$previous_extglob_setting"
  1167. return 0
  1168. }
  1169. complete -F _docker docker