docker 22 KB

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