github.com/pritambaral/docker@v1.4.2-0.20150120174542-b2fe1b3dd952/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 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_image_repos() { 62 local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')" 63 COMPREPLY=( $(compgen -W "$repos" -- "$cur") ) 64 } 65 66 __docker_image_repos_and_tags() { 67 local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')" 68 COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") ) 69 __ltrim_colon_completions "$cur" 70 } 71 72 __docker_image_repos_and_tags_and_ids() { 73 local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')" 74 COMPREPLY=( $(compgen -W "$images" -- "$cur") ) 75 __ltrim_colon_completions "$cur" 76 } 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 85 __docker_pos_first_nonflag() { 86 local argument_flags=$1 87 88 local counter=$cpos 89 while [ $counter -le $cword ]; do 90 if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then 91 (( counter++ )) 92 else 93 case "${words[$counter]}" in 94 -*) 95 ;; 96 *) 97 break 98 ;; 99 esac 100 fi 101 (( counter++ )) 102 done 103 104 echo $counter 105 } 106 107 __docker_resolve_hostname() { 108 command -v host >/dev/null 2>&1 || return 109 COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') ) 110 } 111 112 __docker_capabilities() { 113 # The list of capabilities is defined in types.go, ALL was added manually. 114 COMPREPLY=( $( compgen -W " 115 ALL 116 AUDIT_CONTROL 117 AUDIT_WRITE 118 BLOCK_SUSPEND 119 CHOWN 120 DAC_OVERRIDE 121 DAC_READ_SEARCH 122 FOWNER 123 FSETID 124 IPC_LOCK 125 IPC_OWNER 126 KILL 127 LEASE 128 LINUX_IMMUTABLE 129 MAC_ADMIN 130 MAC_OVERRIDE 131 MKNOD 132 NET_ADMIN 133 NET_BIND_SERVICE 134 NET_BROADCAST 135 NET_RAW 136 SETFCAP 137 SETGID 138 SETPCAP 139 SETUID 140 SYS_ADMIN 141 SYS_BOOT 142 SYS_CHROOT 143 SYSLOG 144 SYS_MODULE 145 SYS_NICE 146 SYS_PACCT 147 SYS_PTRACE 148 SYS_RAWIO 149 SYS_RESOURCE 150 SYS_TIME 151 SYS_TTY_CONFIG 152 WAKE_ALARM 153 " -- "$cur" ) ) 154 } 155 156 _docker_docker() { 157 case "$prev" in 158 -H) 159 return 160 ;; 161 esac 162 163 case "$cur" in 164 -*) 165 COMPREPLY=( $( compgen -W "-H" -- "$cur" ) ) 166 ;; 167 *) 168 COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) ) 169 ;; 170 esac 171 } 172 173 _docker_attach() { 174 case "$cur" in 175 -*) 176 COMPREPLY=( $( compgen -W "--no-stdin --sig-proxy" -- "$cur" ) ) 177 ;; 178 *) 179 local counter="$(__docker_pos_first_nonflag)" 180 if [ $cword -eq $counter ]; then 181 __docker_containers_running 182 fi 183 ;; 184 esac 185 } 186 187 _docker_build() { 188 case "$prev" in 189 --tag|-t) 190 __docker_image_repos_and_tags 191 return 192 ;; 193 esac 194 195 case "$cur" in 196 -*) 197 COMPREPLY=( $( compgen -W "--force-rm --no-cache --quiet -q --rm --tag -t" -- "$cur" ) ) 198 ;; 199 *) 200 local counter="$(__docker_pos_first_nonflag '--tag|-t')" 201 if [ $cword -eq $counter ]; then 202 _filedir -d 203 fi 204 ;; 205 esac 206 } 207 208 _docker_commit() { 209 case "$prev" in 210 --author|-a|--message|-m|--run) 211 return 212 ;; 213 esac 214 215 case "$cur" in 216 -*) 217 COMPREPLY=( $( compgen -W "--author -a --message -m --run" -- "$cur" ) ) 218 ;; 219 *) 220 local counter=$(__docker_pos_first_nonflag '--author|-a|--message|-m|--run') 221 222 if [ $cword -eq $counter ]; then 223 __docker_containers_all 224 return 225 fi 226 (( counter++ )) 227 228 if [ $cword -eq $counter ]; then 229 __docker_image_repos_and_tags 230 return 231 fi 232 ;; 233 esac 234 } 235 236 _docker_cp() { 237 local counter=$(__docker_pos_first_nonflag) 238 if [ $cword -eq $counter ]; then 239 case "$cur" in 240 *:) 241 return 242 ;; 243 *) 244 __docker_containers_all 245 COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) ) 246 compopt -o nospace 247 return 248 ;; 249 esac 250 fi 251 (( counter++ )) 252 253 if [ $cword -eq $counter ]; then 254 _filedir 255 return 256 fi 257 } 258 259 _docker_create() { 260 _docker_run 261 } 262 263 _docker_diff() { 264 local counter=$(__docker_pos_first_nonflag) 265 if [ $cword -eq $counter ]; then 266 __docker_containers_all 267 fi 268 } 269 270 _docker_events() { 271 case "$prev" in 272 --since) 273 return 274 ;; 275 esac 276 277 case "$cur" in 278 -*) 279 COMPREPLY=( $( compgen -W "--since" -- "$cur" ) ) 280 ;; 281 esac 282 } 283 284 _docker_exec() { 285 case "$cur" in 286 -*) 287 COMPREPLY=( $( compgen -W "--detach -d --interactive -i -t --tty" -- "$cur" ) ) 288 ;; 289 *) 290 __docker_containers_running 291 ;; 292 esac 293 } 294 295 _docker_export() { 296 local counter=$(__docker_pos_first_nonflag) 297 if [ $cword -eq $counter ]; then 298 __docker_containers_all 299 fi 300 } 301 302 _docker_help() { 303 local counter=$(__docker_pos_first_nonflag) 304 if [ $cword -eq $counter ]; then 305 COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) 306 fi 307 } 308 309 _docker_history() { 310 case "$cur" in 311 -*) 312 COMPREPLY=( $( compgen -W "--no-trunc --quiet -q" -- "$cur" ) ) 313 ;; 314 *) 315 local counter=$(__docker_pos_first_nonflag) 316 if [ $cword -eq $counter ]; then 317 __docker_image_repos_and_tags_and_ids 318 fi 319 ;; 320 esac 321 } 322 323 _docker_images() { 324 case "$cur" in 325 -*) 326 COMPREPLY=( $( compgen -W "--all -a --no-trunc --quiet -q" -- "$cur" ) ) 327 ;; 328 *) 329 local counter=$(__docker_pos_first_nonflag) 330 if [ $cword -eq $counter ]; then 331 __docker_image_repos 332 fi 333 ;; 334 esac 335 } 336 337 _docker_import() { 338 local counter=$(__docker_pos_first_nonflag) 339 if [ $cword -eq $counter ]; then 340 return 341 fi 342 (( counter++ )) 343 344 if [ $cword -eq $counter ]; then 345 __docker_image_repos_and_tags 346 return 347 fi 348 } 349 350 _docker_info() { 351 return 352 } 353 354 _docker_inspect() { 355 case "$prev" in 356 --format|-f) 357 return 358 ;; 359 esac 360 361 case "$cur" in 362 -*) 363 COMPREPLY=( $( compgen -W "--format -f" -- "$cur" ) ) 364 ;; 365 *) 366 __docker_containers_and_images 367 ;; 368 esac 369 } 370 371 _docker_kill() { 372 __docker_containers_running 373 } 374 375 _docker_load() { 376 case "$prev" in 377 --input|-i) 378 _filedir 379 return 380 ;; 381 esac 382 383 case "$cur" in 384 -*) 385 COMPREPLY=( $( compgen -W "--input -i" -- "$cur" ) ) 386 ;; 387 esac 388 } 389 390 _docker_login() { 391 case "$prev" in 392 --email|-e|--password|-p|--username|-u) 393 return 394 ;; 395 esac 396 397 case "$cur" in 398 -*) 399 COMPREPLY=( $( compgen -W "--email -e --password -p --username -u" -- "$cur" ) ) 400 ;; 401 esac 402 } 403 404 _docker_logs() { 405 case "$cur" in 406 -*) 407 COMPREPLY=( $( compgen -W "--follow -f" -- "$cur" ) ) 408 ;; 409 *) 410 local counter=$(__docker_pos_first_nonflag) 411 if [ $cword -eq $counter ]; then 412 __docker_containers_all 413 fi 414 ;; 415 esac 416 } 417 418 _docker_pause() { 419 local counter=$(__docker_pos_first_nonflag) 420 if [ $cword -eq $counter ]; then 421 __docker_containers_pauseable 422 fi 423 } 424 425 _docker_port() { 426 local counter=$(__docker_pos_first_nonflag) 427 if [ $cword -eq $counter ]; then 428 __docker_containers_all 429 fi 430 } 431 432 _docker_ps() { 433 case "$prev" in 434 --before|--since) 435 __docker_containers_all 436 ;; 437 -n) 438 return 439 ;; 440 esac 441 442 case "$cur" in 443 -*) 444 COMPREPLY=( $( compgen -W "--all -a --before --latest -l --no-trunc -n --quiet -q --size -s --since" -- "$cur" ) ) 445 ;; 446 esac 447 } 448 449 _docker_pull() { 450 case "$prev" in 451 --tag|-t) 452 return 453 ;; 454 esac 455 456 case "$cur" in 457 -*) 458 COMPREPLY=( $( compgen -W "--tag -t" -- "$cur" ) ) 459 ;; 460 *) 461 local counter=$(__docker_pos_first_nonflag '--tag|-t') 462 if [ $cword -eq $counter ]; then 463 __docker_image_repos_and_tags 464 fi 465 ;; 466 esac 467 } 468 469 _docker_push() { 470 local counter=$(__docker_pos_first_nonflag) 471 if [ $cword -eq $counter ]; then 472 __docker_image_repos_and_tags 473 fi 474 } 475 476 _docker_restart() { 477 case "$prev" in 478 --time|-t) 479 return 480 ;; 481 esac 482 483 case "$cur" in 484 -*) 485 COMPREPLY=( $( compgen -W "--time -t" -- "$cur" ) ) 486 ;; 487 *) 488 __docker_containers_all 489 ;; 490 esac 491 } 492 493 _docker_rm() { 494 case "$cur" in 495 -*) 496 COMPREPLY=( $( compgen -W "--force -f --link -l --volumes -v" -- "$cur" ) ) 497 return 498 ;; 499 *) 500 for arg in "${COMP_WORDS[@]}"; do 501 case "$arg" in 502 --force|-f) 503 __docker_containers_all 504 return 505 ;; 506 esac 507 done 508 __docker_containers_stopped 509 return 510 ;; 511 esac 512 } 513 514 _docker_rmi() { 515 __docker_image_repos_and_tags_and_ids 516 } 517 518 _docker_run() { 519 local options_with_args=" 520 --add-host 521 --attach -a 522 --cap-add 523 --cap-drop 524 --cidfile 525 --cpuset 526 --cpu-shares -c 527 --device 528 --dns 529 --dns-search 530 --entrypoint 531 --env -e 532 --env-file 533 --expose 534 --hostname -h 535 --ipc 536 --link 537 --lxc-conf 538 --mac-address 539 --memory -m 540 --name 541 --net 542 --publish -p 543 --restart 544 --security-opt 545 --user -u 546 --volumes-from 547 --volume -v 548 --workdir -w 549 " 550 551 local all_options="$options_with_args 552 --interactive -i 553 --privileged 554 --publish-all -P 555 --tty -t 556 " 557 558 [ "$command" = "run" ] && all_options="$all_options 559 --detach -d 560 --rm 561 --sig-proxy 562 " 563 564 case "$prev" in 565 --add-host) 566 case "$cur" in 567 *:) 568 __docker_resolve_hostname 569 return 570 ;; 571 esac 572 ;; 573 --attach|-a) 574 COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) ) 575 return 576 ;; 577 --cap-add|--cap-drop) 578 __docker_capabilities 579 return 580 ;; 581 --cidfile|--env-file) 582 _filedir 583 return 584 ;; 585 --device|-d|--volume) 586 case "$cur" in 587 *:*) 588 # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine) 589 ;; 590 '') 591 COMPREPLY=( $( compgen -W '/' -- "$cur" ) ) 592 compopt -o nospace 593 ;; 594 /*) 595 _filedir 596 compopt -o nospace 597 ;; 598 esac 599 return 600 ;; 601 --env|-e) 602 COMPREPLY=( $( compgen -e -- "$cur" ) ) 603 compopt -o nospace 604 return 605 ;; 606 --ipc) 607 case "$cur" in 608 *:*) 609 cur="${cur#*:}" 610 __docker_containers_running 611 ;; 612 *) 613 COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) ) 614 if [ "$COMPREPLY" = "container:" ]; then 615 compopt -o nospace 616 fi 617 ;; 618 esac 619 return 620 ;; 621 --link) 622 case "$cur" in 623 *:*) 624 ;; 625 *) 626 __docker_containers_running 627 COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) ) 628 compopt -o nospace 629 ;; 630 esac 631 return 632 ;; 633 --net) 634 case "$cur" in 635 container:*) 636 local cur=${cur#*:} 637 __docker_containers_all 638 ;; 639 *) 640 COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") ) 641 if [ "${COMPREPLY[*]}" = "container:" ] ; then 642 compopt -o nospace 643 fi 644 ;; 645 esac 646 return 647 ;; 648 --restart) 649 case "$cur" in 650 on-failure:*) 651 ;; 652 *) 653 COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") ) 654 ;; 655 esac 656 return 657 ;; 658 --security-opt) 659 case "$cur" in 660 label:*:*) 661 ;; 662 label:*) 663 local cur=${cur##*:} 664 COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") ) 665 if [ "${COMPREPLY[*]}" != "disable" ] ; then 666 compopt -o nospace 667 fi 668 ;; 669 *) 670 COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") ) 671 compopt -o nospace 672 ;; 673 esac 674 return 675 ;; 676 --volumes-from) 677 __docker_containers_all 678 return 679 ;; 680 --cpuset|--cpu-shares|-c|--dns|--dns-search|--entrypoint|--expose|--hostname|-h|--lxc-conf|--mac-address|--memory|-m|--name|-n|--publish|-p|--user|-u|--workdir|-w) 681 return 682 ;; 683 esac 684 685 case "$cur" in 686 -*) 687 COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) ) 688 ;; 689 *) 690 local counter=$( __docker_pos_first_nonflag $( echo $options_with_args | tr -d "\n" | tr " " "|" ) ) 691 692 if [ $cword -eq $counter ]; then 693 __docker_image_repos_and_tags_and_ids 694 fi 695 ;; 696 esac 697 } 698 699 _docker_save() { 700 case "$prev" in 701 --output|-o) 702 _filedir 703 return 704 ;; 705 esac 706 707 case "$cur" in 708 -*) 709 COMPREPLY=( $( compgen -W "-o --output" -- "$cur" ) ) 710 ;; 711 *) 712 __docker_image_repos_and_tags_and_ids 713 ;; 714 esac 715 } 716 717 _docker_search() { 718 case "$prev" in 719 --stars|-s) 720 return 721 ;; 722 esac 723 724 case "$cur" in 725 -*) 726 COMPREPLY=( $( compgen -W "--automated --no-trunc --stars -s" -- "$cur" ) ) 727 ;; 728 esac 729 } 730 731 _docker_start() { 732 case "$cur" in 733 -*) 734 COMPREPLY=( $( compgen -W "--attach -a --interactive -i" -- "$cur" ) ) 735 ;; 736 *) 737 __docker_containers_stopped 738 ;; 739 esac 740 } 741 742 _docker_stop() { 743 case "$prev" in 744 --time|-t) 745 return 746 ;; 747 esac 748 749 case "$cur" in 750 -*) 751 COMPREPLY=( $( compgen -W "--time -t" -- "$cur" ) ) 752 ;; 753 *) 754 __docker_containers_running 755 ;; 756 esac 757 } 758 759 _docker_tag() { 760 case "$cur" in 761 -*) 762 COMPREPLY=( $( compgen -W "--force -f" -- "$cur" ) ) 763 ;; 764 *) 765 local counter=$(__docker_pos_first_nonflag) 766 767 if [ $cword -eq $counter ]; then 768 __docker_image_repos_and_tags 769 return 770 fi 771 (( counter++ )) 772 773 if [ $cword -eq $counter ]; then 774 __docker_image_repos_and_tags 775 return 776 fi 777 ;; 778 esac 779 } 780 781 _docker_unpause() { 782 local counter=$(__docker_pos_first_nonflag) 783 if [ $cword -eq $counter ]; then 784 __docker_containers_unpauseable 785 fi 786 } 787 788 _docker_top() { 789 local counter=$(__docker_pos_first_nonflag) 790 if [ $cword -eq $counter ]; then 791 __docker_containers_running 792 fi 793 } 794 795 _docker_version() { 796 return 797 } 798 799 _docker_wait() { 800 __docker_containers_all 801 } 802 803 _docker() { 804 local commands=( 805 attach 806 build 807 commit 808 cp 809 create 810 diff 811 events 812 exec 813 export 814 history 815 images 816 import 817 info 818 insert 819 inspect 820 kill 821 load 822 login 823 logs 824 pause 825 port 826 ps 827 pull 828 push 829 restart 830 rm 831 rmi 832 run 833 save 834 search 835 start 836 stop 837 tag 838 top 839 unpause 840 version 841 wait 842 ) 843 844 COMPREPLY=() 845 local cur prev words cword 846 _get_comp_words_by_ref -n : cur prev words cword 847 848 local command='docker' cpos=0 849 local counter=1 850 while [ $counter -lt $cword ]; do 851 case "${words[$counter]}" in 852 -H) 853 (( counter++ )) 854 ;; 855 -*) 856 ;; 857 *) 858 command="${words[$counter]}" 859 cpos=$counter 860 (( cpos++ )) 861 break 862 ;; 863 esac 864 (( counter++ )) 865 done 866 867 local completions_func=_docker_${command} 868 declare -F $completions_func >/dev/null && $completions_func 869 870 return 0 871 } 872 873 complete -F _docker docker