github.com/campoy/docker@v1.8.0-rc1/contrib/completion/bash/docker (about) 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 29 __docker_q() { 30 docker ${host:+-H "$host"} 2>/dev/null "$@" 31 } 32 33 __docker_containers_all() { 34 local IFS=$'\n' 35 local containers=( $(__docker_q ps -aq --no-trunc) ) 36 if [ "$1" ]; then 37 containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") ) 38 fi 39 local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") ) 40 names=( "${names[@]#/}" ) # trim off the leading "/" from the container names 41 unset IFS 42 COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") ) 43 } 44 45 __docker_containers_running() { 46 __docker_containers_all '.State.Running' 47 } 48 49 __docker_containers_stopped() { 50 __docker_containers_all 'not .State.Running' 51 } 52 53 __docker_containers_pauseable() { 54 __docker_containers_all 'and .State.Running (not .State.Paused)' 55 } 56 57 __docker_containers_unpauseable() { 58 __docker_containers_all '.State.Paused' 59 } 60 61 __docker_container_names() { 62 local containers=( $(__docker_q ps -aq --no-trunc) ) 63 local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") ) 64 names=( "${names[@]#/}" ) # trim off the leading "/" from the container names 65 COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") ) 66 } 67 68 __docker_container_ids() { 69 local containers=( $(__docker_q ps -aq) ) 70 COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") ) 71 } 72 73 __docker_image_repos() { 74 local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')" 75 COMPREPLY=( $(compgen -W "$repos" -- "$cur") ) 76 } 77 78 __docker_image_repos_and_tags() { 79 local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')" 80 COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") ) 81 __ltrim_colon_completions "$cur" 82 } 83 84 __docker_image_repos_and_tags_and_ids() { 85 local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')" 86 COMPREPLY=( $(compgen -W "$images" -- "$cur") ) 87 __ltrim_colon_completions "$cur" 88 } 89 90 __docker_containers_and_images() { 91 __docker_containers_all 92 local containers=( "${COMPREPLY[@]}" ) 93 __docker_image_repos_and_tags_and_ids 94 COMPREPLY+=( "${containers[@]}" ) 95 } 96 97 # Finds the position of the first word that is neither option nor an option's argument. 98 # If there are options that require arguments, you should pass a glob describing those 99 # options, e.g. "--option1|-o|--option2" 100 # Use this function to restrict completions to exact positions after the argument list. 101 __docker_pos_first_nonflag() { 102 local argument_flags=$1 103 104 local counter=$((command_pos + 1)) 105 while [ $counter -le $cword ]; do 106 if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then 107 (( counter++ )) 108 # eat "=" in case of --option=arg syntax 109 [ "${words[$counter]}" = "=" ] && (( counter++ )) 110 else 111 case "${words[$counter]}" in 112 -*) 113 ;; 114 *) 115 break 116 ;; 117 esac 118 fi 119 120 # Bash splits words at "=", retaining "=" as a word, examples: 121 # "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words 122 while [ "${words[$counter + 1]}" = "=" ] ; do 123 counter=$(( counter + 2)) 124 done 125 126 (( counter++ )) 127 done 128 129 echo $counter 130 } 131 132 # Returns the value of the first option matching option_glob. 133 # Valid values for option_glob are option names like '--log-level' and 134 # globs like '--log-level|-l' 135 # Only positions between the command and the current word are considered. 136 __docker_value_of_option() { 137 local option_glob=$1 138 139 local counter=$((command_pos + 1)) 140 while [ $counter -lt $cword ]; do 141 case ${words[$counter]} in 142 $option_glob ) 143 echo ${words[$counter + 1]} 144 break 145 ;; 146 esac 147 (( counter++ )) 148 done 149 } 150 151 # Transforms a multiline list of strings into a single line string 152 # with the words separated by "|". 153 # This is used to prepare arguments to __docker_pos_first_nonflag(). 154 __docker_to_alternatives() { 155 local parts=( $1 ) 156 local IFS='|' 157 echo "${parts[*]}" 158 } 159 160 # Transforms a multiline list of options into an extglob pattern 161 # suitable for use in case statements. 162 __docker_to_extglob() { 163 local extglob=$( __docker_to_alternatives "$1" ) 164 echo "@($extglob)" 165 } 166 167 __docker_resolve_hostname() { 168 command -v host >/dev/null 2>&1 || return 169 COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') ) 170 } 171 172 __docker_capabilities() { 173 # The list of capabilities is defined in types.go, ALL was added manually. 174 COMPREPLY=( $( compgen -W " 175 ALL 176 AUDIT_CONTROL 177 AUDIT_WRITE 178 AUDIT_READ 179 BLOCK_SUSPEND 180 CHOWN 181 DAC_OVERRIDE 182 DAC_READ_SEARCH 183 FOWNER 184 FSETID 185 IPC_LOCK 186 IPC_OWNER 187 KILL 188 LEASE 189 LINUX_IMMUTABLE 190 MAC_ADMIN 191 MAC_OVERRIDE 192 MKNOD 193 NET_ADMIN 194 NET_BIND_SERVICE 195 NET_BROADCAST 196 NET_RAW 197 SETFCAP 198 SETGID 199 SETPCAP 200 SETUID 201 SYS_ADMIN 202 SYS_BOOT 203 SYS_CHROOT 204 SYSLOG 205 SYS_MODULE 206 SYS_NICE 207 SYS_PACCT 208 SYS_PTRACE 209 SYS_RAWIO 210 SYS_RESOURCE 211 SYS_TIME 212 SYS_TTY_CONFIG 213 WAKE_ALARM 214 " -- "$cur" ) ) 215 } 216 217 __docker_log_drivers() { 218 COMPREPLY=( $( compgen -W " 219 fluentd 220 gelf 221 journald 222 json-file 223 none 224 syslog 225 " -- "$cur" ) ) 226 } 227 228 __docker_log_driver_options() { 229 # see docs/reference/logging/index.md 230 local fluentd_options="fluentd-address fluentd-tag" 231 local gelf_options="gelf-address gelf-tag" 232 local syslog_options="syslog-address syslog-facility syslog-tag" 233 234 case $(__docker_value_of_option --log-driver) in 235 '') 236 COMPREPLY=( $( compgen -W "$fluentd_options $gelf_options $syslog_options" -S = -- "$cur" ) ) 237 ;; 238 fluentd) 239 COMPREPLY=( $( compgen -W "$fluentd_options" -S = -- "$cur" ) ) 240 ;; 241 gelf) 242 COMPREPLY=( $( compgen -W "$gelf_options" -S = -- "$cur" ) ) 243 ;; 244 syslog) 245 COMPREPLY=( $( compgen -W "$syslog_options" -S = -- "$cur" ) ) 246 ;; 247 *) 248 return 249 ;; 250 esac 251 252 compopt -o nospace 253 } 254 255 __docker_complete_log_driver_options() { 256 # "=" gets parsed to a word and assigned to either $cur or $prev depending on whether 257 # it is the last character or not. So we search for "xxx=" in the the last two words. 258 case "${words[$cword-2]}$prev=" in 259 *gelf-address=*) 260 COMPREPLY=( $( compgen -W "udp" -S "://" -- "${cur#=}" ) ) 261 compopt -o nospace 262 return 263 ;; 264 *syslog-address=*) 265 COMPREPLY=( $( compgen -W "tcp udp unix" -S "://" -- "${cur#=}" ) ) 266 compopt -o nospace 267 return 268 ;; 269 *syslog-facility=*) 270 COMPREPLY=( $( compgen -W " 271 auth 272 authpriv 273 cron 274 daemon 275 ftp 276 kern 277 local0 278 local1 279 local2 280 local3 281 local4 282 local5 283 local6 284 local7 285 lpr 286 mail 287 news 288 syslog 289 user 290 uucp 291 " -- "${cur#=}" ) ) 292 return 293 ;; 294 esac 295 return 1 296 } 297 298 # a selection of the available signals that is most likely of interest in the 299 # context of docker containers. 300 __docker_signals() { 301 local signals=( 302 SIGCONT 303 SIGHUP 304 SIGINT 305 SIGKILL 306 SIGQUIT 307 SIGSTOP 308 SIGTERM 309 SIGUSR1 310 SIGUSR2 311 ) 312 COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) ) 313 } 314 315 _docker_docker() { 316 local boolean_options=" 317 --daemon -d 318 --debug -D 319 --help -h 320 --icc 321 --ip-forward 322 --ip-masq 323 --iptables 324 --ipv6 325 --selinux-enabled 326 --tls 327 --tlsverify 328 --userland-proxy=false 329 --version -v 330 " 331 332 case "$prev" in 333 --exec-root|--graph|-g) 334 _filedir -d 335 return 336 ;; 337 --log-driver) 338 __docker_log_drivers 339 return 340 ;; 341 --log-level|-l) 342 COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) ) 343 return 344 ;; 345 --log-opt) 346 __docker_log_driver_options 347 return 348 ;; 349 --pidfile|-p|--tlscacert|--tlscert|--tlskey) 350 _filedir 351 return 352 ;; 353 --storage-driver|-s) 354 COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) ) 355 return 356 ;; 357 $main_options_with_args_glob ) 358 return 359 ;; 360 esac 361 362 __docker_complete_log_driver_options && return 363 364 case "$cur" in 365 -*) 366 COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) ) 367 ;; 368 *) 369 local counter="$(__docker_pos_first_nonflag $main_options_with_args_glob)" 370 if [ $cword -eq $counter ]; then 371 COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) ) 372 fi 373 ;; 374 esac 375 } 376 377 _docker_attach() { 378 case "$cur" in 379 -*) 380 COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) ) 381 ;; 382 *) 383 local counter="$(__docker_pos_first_nonflag)" 384 if [ $cword -eq $counter ]; then 385 __docker_containers_running 386 fi 387 ;; 388 esac 389 } 390 391 _docker_build() { 392 case "$prev" in 393 --cgroup-parent|--cpuset-cpus|--cpuset-mems|--cpu-shares|-c|--cpu-period|--cpu-quota|--memory|-m|--memory-swap) 394 return 395 ;; 396 --file|-f) 397 _filedir 398 return 399 ;; 400 --tag|-t) 401 __docker_image_repos_and_tags 402 return 403 ;; 404 esac 405 406 case "$cur" in 407 -*) 408 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 --ulimit" -- "$cur" ) ) 409 ;; 410 *) 411 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')" 412 if [ $cword -eq $counter ]; then 413 _filedir -d 414 fi 415 ;; 416 esac 417 } 418 419 _docker_commit() { 420 case "$prev" in 421 --author|-a|--change|-c|--message|-m) 422 return 423 ;; 424 esac 425 426 case "$cur" in 427 -*) 428 COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) ) 429 ;; 430 *) 431 local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m') 432 433 if [ $cword -eq $counter ]; then 434 __docker_containers_all 435 return 436 fi 437 (( counter++ )) 438 439 if [ $cword -eq $counter ]; then 440 __docker_image_repos_and_tags 441 return 442 fi 443 ;; 444 esac 445 } 446 447 _docker_cp() { 448 case "$cur" in 449 -*) 450 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 451 ;; 452 *) 453 local counter=$(__docker_pos_first_nonflag) 454 if [ $cword -eq $counter ]; then 455 case "$cur" in 456 *:) 457 return 458 ;; 459 *) 460 __docker_containers_all 461 COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) ) 462 compopt -o nospace 463 return 464 ;; 465 esac 466 fi 467 (( counter++ )) 468 469 if [ $cword -eq $counter ]; then 470 _filedir -d 471 return 472 fi 473 ;; 474 esac 475 } 476 477 _docker_create() { 478 _docker_run 479 } 480 481 _docker_diff() { 482 case "$cur" in 483 -*) 484 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 485 ;; 486 *) 487 local counter=$(__docker_pos_first_nonflag) 488 if [ $cword -eq $counter ]; then 489 __docker_containers_all 490 fi 491 ;; 492 esac 493 } 494 495 _docker_events() { 496 case "$prev" in 497 --filter|-f) 498 COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) ) 499 compopt -o nospace 500 return 501 ;; 502 --since|--until) 503 return 504 ;; 505 esac 506 507 case "${words[$cword-2]}$prev=" in 508 *container=*) 509 cur="${cur#=}" 510 __docker_containers_all 511 return 512 ;; 513 *event=*) 514 COMPREPLY=( $( compgen -W " 515 attach 516 commit 517 copy 518 create 519 delete 520 destroy 521 die 522 exec_create 523 exec_start 524 export 525 import 526 kill 527 oom 528 pause 529 pull 530 push 531 rename 532 resize 533 restart 534 start 535 stop 536 tag 537 top 538 unpause 539 untag 540 " -- "${cur#=}" ) ) 541 return 542 ;; 543 *image=*) 544 cur="${cur#=}" 545 __docker_image_repos_and_tags_and_ids 546 return 547 ;; 548 esac 549 550 case "$cur" in 551 -*) 552 COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) ) 553 ;; 554 esac 555 } 556 557 _docker_exec() { 558 case "$prev" in 559 --user|-u) 560 return 561 ;; 562 esac 563 564 case "$cur" in 565 -*) 566 COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty -u --user" -- "$cur" ) ) 567 ;; 568 *) 569 __docker_containers_running 570 ;; 571 esac 572 } 573 574 _docker_export() { 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 588 _docker_help() { 589 local counter=$(__docker_pos_first_nonflag) 590 if [ $cword -eq $counter ]; then 591 COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) 592 fi 593 } 594 595 _docker_history() { 596 case "$cur" in 597 -*) 598 COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) ) 599 ;; 600 *) 601 local counter=$(__docker_pos_first_nonflag) 602 if [ $cword -eq $counter ]; then 603 __docker_image_repos_and_tags_and_ids 604 fi 605 ;; 606 esac 607 } 608 609 _docker_images() { 610 case "$prev" in 611 --filter|-f) 612 COMPREPLY=( $( compgen -W "dangling=true label=" -- "$cur" ) ) 613 if [ "$COMPREPLY" = "label=" ]; then 614 compopt -o nospace 615 fi 616 return 617 ;; 618 esac 619 620 case "${words[$cword-2]}$prev=" in 621 *dangling=*) 622 COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) ) 623 return 624 ;; 625 *label=*) 626 return 627 ;; 628 esac 629 630 case "$cur" in 631 -*) 632 COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --help --no-trunc --quiet -q" -- "$cur" ) ) 633 ;; 634 =) 635 return 636 ;; 637 *) 638 __docker_image_repos 639 ;; 640 esac 641 } 642 643 _docker_import() { 644 case "$cur" in 645 -*) 646 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 647 ;; 648 *) 649 local counter=$(__docker_pos_first_nonflag) 650 if [ $cword -eq $counter ]; then 651 return 652 fi 653 (( counter++ )) 654 655 if [ $cword -eq $counter ]; then 656 __docker_image_repos_and_tags 657 return 658 fi 659 ;; 660 esac 661 } 662 663 _docker_info() { 664 case "$cur" in 665 -*) 666 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 667 ;; 668 esac 669 } 670 671 _docker_inspect() { 672 case "$prev" in 673 --format|-f) 674 return 675 ;; 676 --type) 677 COMPREPLY=( $( compgen -W "image container" -- "$cur" ) ) 678 return 679 ;; 680 681 esac 682 683 case "$cur" in 684 -*) 685 COMPREPLY=( $( compgen -W "--format -f --type --help" -- "$cur" ) ) 686 ;; 687 *) 688 __docker_containers_and_images 689 ;; 690 esac 691 } 692 693 _docker_kill() { 694 case "$prev" in 695 --signal|-s) 696 __docker_signals 697 return 698 ;; 699 esac 700 701 case "$cur" in 702 -*) 703 COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) ) 704 ;; 705 *) 706 __docker_containers_running 707 ;; 708 esac 709 } 710 711 _docker_load() { 712 case "$prev" in 713 --input|-i) 714 _filedir 715 return 716 ;; 717 esac 718 719 case "$cur" in 720 -*) 721 COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) ) 722 ;; 723 esac 724 } 725 726 _docker_login() { 727 case "$prev" in 728 --email|-e|--password|-p|--username|-u) 729 return 730 ;; 731 esac 732 733 case "$cur" in 734 -*) 735 COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) ) 736 ;; 737 esac 738 } 739 740 _docker_logout() { 741 case "$cur" in 742 -*) 743 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 744 ;; 745 esac 746 } 747 748 _docker_logs() { 749 case "$prev" in 750 --since|--tail) 751 return 752 ;; 753 esac 754 755 case "$cur" in 756 -*) 757 COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) ) 758 ;; 759 *) 760 local counter=$(__docker_pos_first_nonflag '--tail') 761 if [ $cword -eq $counter ]; then 762 __docker_containers_all 763 fi 764 ;; 765 esac 766 } 767 768 _docker_pause() { 769 case "$cur" in 770 -*) 771 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 772 ;; 773 *) 774 local counter=$(__docker_pos_first_nonflag) 775 if [ $cword -eq $counter ]; then 776 __docker_containers_pauseable 777 fi 778 ;; 779 esac 780 } 781 782 _docker_port() { 783 case "$cur" in 784 -*) 785 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 786 ;; 787 *) 788 local counter=$(__docker_pos_first_nonflag) 789 if [ $cword -eq $counter ]; then 790 __docker_containers_all 791 fi 792 ;; 793 esac 794 } 795 796 _docker_ps() { 797 case "$prev" in 798 --before|--since) 799 __docker_containers_all 800 ;; 801 --filter|-f) 802 COMPREPLY=( $( compgen -S = -W "exited id label name status" -- "$cur" ) ) 803 compopt -o nospace 804 return 805 ;; 806 -n) 807 return 808 ;; 809 esac 810 811 case "${words[$cword-2]}$prev=" in 812 *id=*) 813 cur="${cur#=}" 814 __docker_container_ids 815 return 816 ;; 817 *name=*) 818 cur="${cur#=}" 819 __docker_container_names 820 return 821 ;; 822 *status=*) 823 COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) ) 824 return 825 ;; 826 esac 827 828 case "$cur" in 829 -*) 830 COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) ) 831 ;; 832 esac 833 } 834 835 _docker_pull() { 836 case "$cur" in 837 -*) 838 COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) ) 839 ;; 840 *) 841 local counter=$(__docker_pos_first_nonflag) 842 if [ $cword -eq $counter ]; then 843 for arg in "${COMP_WORDS[@]}"; do 844 case "$arg" in 845 --all-tags|-a) 846 __docker_image_repos 847 return 848 ;; 849 esac 850 done 851 __docker_image_repos_and_tags 852 fi 853 ;; 854 esac 855 } 856 857 _docker_push() { 858 case "$cur" in 859 -*) 860 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 861 ;; 862 *) 863 local counter=$(__docker_pos_first_nonflag) 864 if [ $cword -eq $counter ]; then 865 __docker_image_repos_and_tags 866 fi 867 ;; 868 esac 869 } 870 871 _docker_rename() { 872 case "$cur" in 873 -*) 874 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 875 ;; 876 *) 877 local counter=$(__docker_pos_first_nonflag) 878 if [ $cword -eq $counter ]; then 879 __docker_containers_all 880 fi 881 ;; 882 esac 883 } 884 885 _docker_restart() { 886 case "$prev" in 887 --time|-t) 888 return 889 ;; 890 esac 891 892 case "$cur" in 893 -*) 894 COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) ) 895 ;; 896 *) 897 __docker_containers_all 898 ;; 899 esac 900 } 901 902 _docker_rm() { 903 case "$cur" in 904 -*) 905 COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) ) 906 ;; 907 *) 908 for arg in "${COMP_WORDS[@]}"; do 909 case "$arg" in 910 --force|-f) 911 __docker_containers_all 912 return 913 ;; 914 esac 915 done 916 __docker_containers_stopped 917 ;; 918 esac 919 } 920 921 _docker_rmi() { 922 case "$cur" in 923 -*) 924 COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) ) 925 ;; 926 *) 927 __docker_image_repos_and_tags_and_ids 928 ;; 929 esac 930 } 931 932 _docker_run() { 933 local options_with_args=" 934 --add-host 935 --blkio-weight 936 --attach -a 937 --cap-add 938 --cap-drop 939 --cgroup-parent 940 --cidfile 941 --cpuset 942 --cpu-period 943 --cpu-quota 944 --cpu-shares -c 945 --device 946 --dns 947 --dns-search 948 --entrypoint 949 --env -e 950 --env-file 951 --expose 952 --group-add 953 --hostname -h 954 --ipc 955 --label -l 956 --label-file 957 --link 958 --log-driver 959 --log-opt 960 --lxc-conf 961 --mac-address 962 --memory -m 963 --memory-swap 964 --name 965 --net 966 --pid 967 --publish -p 968 --restart 969 --security-opt 970 --user -u 971 --ulimit 972 --uts 973 --volumes-from 974 --volume -v 975 --workdir -w 976 " 977 978 local all_options="$options_with_args 979 --help 980 --interactive -i 981 --privileged 982 --publish-all -P 983 --read-only 984 --tty -t 985 " 986 987 [ "$command" = "run" ] && all_options="$all_options 988 --detach -d 989 --rm 990 --sig-proxy 991 " 992 993 local options_with_args_glob=$(__docker_to_extglob "$options_with_args") 994 995 case "$prev" in 996 --add-host) 997 case "$cur" in 998 *:) 999 __docker_resolve_hostname 1000 return 1001 ;; 1002 esac 1003 ;; 1004 --attach|-a) 1005 COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) ) 1006 return 1007 ;; 1008 --cap-add|--cap-drop) 1009 __docker_capabilities 1010 return 1011 ;; 1012 --cidfile|--env-file|--label-file) 1013 _filedir 1014 return 1015 ;; 1016 --device|--volume|-v) 1017 case "$cur" in 1018 *:*) 1019 # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine) 1020 ;; 1021 '') 1022 COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) 1023 compopt -o nospace 1024 ;; 1025 /*) 1026 _filedir 1027 compopt -o nospace 1028 ;; 1029 esac 1030 return 1031 ;; 1032 --env|-e) 1033 COMPREPLY=( $( compgen -e -- "$cur" ) ) 1034 compopt -o nospace 1035 return 1036 ;; 1037 --ipc) 1038 case "$cur" in 1039 *:*) 1040 cur="${cur#*:}" 1041 __docker_containers_running 1042 ;; 1043 *) 1044 COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) ) 1045 if [ "$COMPREPLY" = "container:" ]; then 1046 compopt -o nospace 1047 fi 1048 ;; 1049 esac 1050 return 1051 ;; 1052 --link) 1053 case "$cur" in 1054 *:*) 1055 ;; 1056 *) 1057 __docker_containers_running 1058 COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) ) 1059 compopt -o nospace 1060 ;; 1061 esac 1062 return 1063 ;; 1064 --log-driver) 1065 __docker_log_drivers 1066 return 1067 ;; 1068 --log-opt) 1069 __docker_log_driver_options 1070 return 1071 ;; 1072 --net) 1073 case "$cur" in 1074 container:*) 1075 local cur=${cur#*:} 1076 __docker_containers_all 1077 ;; 1078 *) 1079 COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") ) 1080 if [ "${COMPREPLY[*]}" = "container:" ] ; then 1081 compopt -o nospace 1082 fi 1083 ;; 1084 esac 1085 return 1086 ;; 1087 --restart) 1088 case "$cur" in 1089 on-failure:*) 1090 ;; 1091 *) 1092 COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") ) 1093 ;; 1094 esac 1095 return 1096 ;; 1097 --security-opt) 1098 case "$cur" in 1099 label:*:*) 1100 ;; 1101 label:*) 1102 local cur=${cur##*:} 1103 COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") ) 1104 if [ "${COMPREPLY[*]}" != "disable" ] ; then 1105 compopt -o nospace 1106 fi 1107 ;; 1108 *) 1109 COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") ) 1110 compopt -o nospace 1111 ;; 1112 esac 1113 return 1114 ;; 1115 --volumes-from) 1116 __docker_containers_all 1117 return 1118 ;; 1119 $options_with_args_glob ) 1120 return 1121 ;; 1122 esac 1123 1124 __docker_complete_log_driver_options && return 1125 1126 case "$cur" in 1127 -*) 1128 COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) ) 1129 ;; 1130 *) 1131 local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) ) 1132 1133 if [ $cword -eq $counter ]; then 1134 __docker_image_repos_and_tags_and_ids 1135 fi 1136 ;; 1137 esac 1138 } 1139 1140 _docker_save() { 1141 case "$prev" in 1142 --output|-o) 1143 _filedir 1144 return 1145 ;; 1146 esac 1147 1148 case "$cur" in 1149 -*) 1150 COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) ) 1151 ;; 1152 *) 1153 __docker_image_repos_and_tags_and_ids 1154 ;; 1155 esac 1156 } 1157 1158 _docker_search() { 1159 case "$prev" in 1160 --stars|-s) 1161 return 1162 ;; 1163 esac 1164 1165 case "$cur" in 1166 -*) 1167 COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) ) 1168 ;; 1169 esac 1170 } 1171 1172 _docker_start() { 1173 case "$cur" in 1174 -*) 1175 COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) ) 1176 ;; 1177 *) 1178 __docker_containers_stopped 1179 ;; 1180 esac 1181 } 1182 1183 _docker_stats() { 1184 case "$cur" in 1185 -*) 1186 COMPREPLY=( $( compgen -W "--no-stream --help" -- "$cur" ) ) 1187 ;; 1188 *) 1189 __docker_containers_running 1190 ;; 1191 esac 1192 } 1193 1194 _docker_stop() { 1195 case "$prev" in 1196 --time|-t) 1197 return 1198 ;; 1199 esac 1200 1201 case "$cur" in 1202 -*) 1203 COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) ) 1204 ;; 1205 *) 1206 __docker_containers_running 1207 ;; 1208 esac 1209 } 1210 1211 _docker_tag() { 1212 case "$cur" in 1213 -*) 1214 COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) ) 1215 ;; 1216 *) 1217 local counter=$(__docker_pos_first_nonflag) 1218 1219 if [ $cword -eq $counter ]; then 1220 __docker_image_repos_and_tags 1221 return 1222 fi 1223 (( counter++ )) 1224 1225 if [ $cword -eq $counter ]; then 1226 __docker_image_repos_and_tags 1227 return 1228 fi 1229 ;; 1230 esac 1231 } 1232 1233 _docker_unpause() { 1234 case "$cur" in 1235 -*) 1236 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 1237 ;; 1238 *) 1239 local counter=$(__docker_pos_first_nonflag) 1240 if [ $cword -eq $counter ]; then 1241 __docker_containers_unpauseable 1242 fi 1243 ;; 1244 esac 1245 } 1246 1247 _docker_top() { 1248 case "$cur" in 1249 -*) 1250 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 1251 ;; 1252 *) 1253 local counter=$(__docker_pos_first_nonflag) 1254 if [ $cword -eq $counter ]; then 1255 __docker_containers_running 1256 fi 1257 ;; 1258 esac 1259 } 1260 1261 _docker_version() { 1262 case "$cur" in 1263 -*) 1264 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 1265 ;; 1266 esac 1267 } 1268 1269 _docker_wait() { 1270 case "$cur" in 1271 -*) 1272 COMPREPLY=( $( compgen -W "--help" -- "$cur" ) ) 1273 ;; 1274 *) 1275 __docker_containers_all 1276 ;; 1277 esac 1278 } 1279 1280 _docker() { 1281 local previous_extglob_setting=$(shopt -p extglob) 1282 shopt -s extglob 1283 1284 local commands=( 1285 attach 1286 build 1287 commit 1288 cp 1289 create 1290 diff 1291 events 1292 exec 1293 export 1294 history 1295 images 1296 import 1297 info 1298 inspect 1299 kill 1300 load 1301 login 1302 logout 1303 logs 1304 pause 1305 port 1306 ps 1307 pull 1308 push 1309 rename 1310 restart 1311 rm 1312 rmi 1313 run 1314 save 1315 search 1316 start 1317 stats 1318 stop 1319 tag 1320 top 1321 unpause 1322 version 1323 wait 1324 ) 1325 1326 local main_options_with_args=" 1327 --api-cors-header 1328 --bip 1329 --bridge -b 1330 --default-gateway 1331 --default-gateway-v6 1332 --default-ulimit 1333 --dns 1334 --dns-search 1335 --exec-driver -e 1336 --exec-opt 1337 --exec-root 1338 --fixed-cidr 1339 --fixed-cidr-v6 1340 --graph -g 1341 --group -G 1342 --host -H 1343 --insecure-registry 1344 --ip 1345 --label 1346 --log-driver 1347 --log-level -l 1348 --log-opt 1349 --mtu 1350 --pidfile -p 1351 --registry-mirror 1352 --storage-driver -s 1353 --storage-opt 1354 --tlscacert 1355 --tlscert 1356 --tlskey 1357 " 1358 1359 local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args") 1360 local host 1361 1362 COMPREPLY=() 1363 local cur prev words cword 1364 _get_comp_words_by_ref -n : cur prev words cword 1365 1366 local command='docker' command_pos=0 1367 local counter=1 1368 while [ $counter -lt $cword ]; do 1369 case "${words[$counter]}" in 1370 # save host so that completion can use custom daemon 1371 --host|-H) 1372 (( counter++ )) 1373 host="${words[$counter]}" 1374 ;; 1375 $main_options_with_args_glob ) 1376 (( counter++ )) 1377 ;; 1378 -*) 1379 ;; 1380 =) 1381 (( counter++ )) 1382 ;; 1383 *) 1384 command="${words[$counter]}" 1385 command_pos=$counter 1386 break 1387 ;; 1388 esac 1389 (( counter++ )) 1390 done 1391 1392 local completions_func=_docker_${command} 1393 declare -F $completions_func >/dev/null && $completions_func 1394 1395 eval "$previous_extglob_setting" 1396 return 0 1397 } 1398 1399 complete -F _docker docker