github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/dist/bash_completion/rkt.bash (about) 1 #!/bin/bash 2 3 __debug() 4 { 5 if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then 6 echo "$*" >> "${BASH_COMP_DEBUG_FILE}" 7 fi 8 } 9 10 # Homebrew on Macs have version 1.3 of bash-completion which doesn't include 11 # _init_completion. This is a very minimal version of that function. 12 __my_init_completion() 13 { 14 COMPREPLY=() 15 _get_comp_words_by_ref cur prev words cword 16 } 17 18 __index_of_word() 19 { 20 local w word=$1 21 shift 22 index=0 23 for w in "$@"; do 24 [[ $w = "$word" ]] && return 25 index=$((index+1)) 26 done 27 index=-1 28 } 29 30 __contains_word() 31 { 32 local w word=$1; shift 33 for w in "$@"; do 34 [[ $w = "$word" ]] && return 35 done 36 return 1 37 } 38 39 __handle_reply() 40 { 41 __debug "${FUNCNAME}" 42 case $cur in 43 -*) 44 if [[ $(type -t compopt) = "builtin" ]]; then 45 compopt -o nospace 46 fi 47 local allflags 48 if [ ${#must_have_one_flag[@]} -ne 0 ]; then 49 allflags=("${must_have_one_flag[@]}") 50 else 51 allflags=("${flags[*]} ${two_word_flags[*]}") 52 fi 53 COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") ) 54 if [[ $(type -t compopt) = "builtin" ]]; then 55 [[ $COMPREPLY == *= ]] || compopt +o nospace 56 fi 57 return 0; 58 ;; 59 esac 60 61 # check if we are handling a flag with special work handling 62 local index 63 __index_of_word "${prev}" "${flags_with_completion[@]}" 64 if [[ ${index} -ge 0 ]]; then 65 ${flags_completion[${index}]} 66 return 67 fi 68 69 # we are parsing a flag and don't have a special handler, no completion 70 if [[ ${cur} != "${words[cword]}" ]]; then 71 return 72 fi 73 74 local completions 75 if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then 76 completions=("${must_have_one_flag[@]}") 77 elif [[ ${#must_have_one_noun[@]} -ne 0 ]]; then 78 completions=("${must_have_one_noun[@]}") 79 else 80 completions=("${commands[@]}") 81 fi 82 COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") ) 83 84 if [[ ${#COMPREPLY[@]} -eq 0 ]]; then 85 declare -F __custom_func >/dev/null && __custom_func 86 fi 87 } 88 89 # The arguments should be in the form "ext1|ext2|extn" 90 __handle_filename_extension_flag() 91 { 92 local ext="$1" 93 _filedir "@(${ext})" 94 } 95 96 __handle_subdirs_in_dir_flag() 97 { 98 local dir="$1" 99 pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 100 } 101 102 __handle_flag() 103 { 104 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" 105 106 # if a command required a flag, and we found it, unset must_have_one_flag() 107 local flagname=${words[c]} 108 # if the word contained an = 109 if [[ ${words[c]} == *"="* ]]; then 110 flagname=${flagname%=*} # strip everything after the = 111 flagname="${flagname}=" # but put the = back 112 fi 113 __debug "${FUNCNAME}: looking for ${flagname}" 114 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then 115 must_have_one_flag=() 116 fi 117 118 # skip the argument to a two word flag 119 if __contains_word "${words[c]}" "${two_word_flags[@]}"; then 120 c=$((c+1)) 121 # if we are looking for a flags value, don't show commands 122 if [[ $c -eq $cword ]]; then 123 commands=() 124 fi 125 fi 126 127 # skip the flag itself 128 c=$((c+1)) 129 130 } 131 132 __handle_noun() 133 { 134 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" 135 136 if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then 137 must_have_one_noun=() 138 fi 139 140 nouns+=("${words[c]}") 141 c=$((c+1)) 142 } 143 144 __handle_command() 145 { 146 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" 147 148 local next_command 149 if [[ -n ${last_command} ]]; then 150 next_command="_${last_command}_${words[c]}" 151 else 152 next_command="_${words[c]}" 153 fi 154 c=$((c+1)) 155 __debug "${FUNCNAME}: looking for ${next_command}" 156 declare -F $next_command >/dev/null && $next_command 157 } 158 159 __handle_word() 160 { 161 if [[ $c -ge $cword ]]; then 162 __handle_reply 163 return 164 fi 165 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" 166 if [[ "${words[c]}" == -* ]]; then 167 __handle_flag 168 elif __contains_word "${words[c]}" "${commands[@]}"; then 169 __handle_command 170 else 171 __handle_noun 172 fi 173 __handle_word 174 } 175 176 __rkt_parse_image() 177 { 178 local rkt_output 179 if rkt_output=$(rkt image list --no-legend 2>/dev/null); then 180 out=($(echo "${rkt_output}" | awk '{print $1}')) 181 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) 182 fi 183 } 184 185 __rkt_parse_list() 186 { 187 local rkt_output 188 if rkt_output=$(rkt list --no-legend 2>/dev/null); then 189 if [[ -n "$1" ]]; then 190 out=($(echo "${rkt_output}" | grep ${1} | awk '{print $1}')) 191 else 192 out=($(echo "${rkt_output}" | awk '{print $1}')) 193 fi 194 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) 195 fi 196 } 197 198 __custom_func() { 199 case ${last_command} in 200 rkt_image_export | \ 201 rkt_image_extract | \ 202 rkt_image_cat-manifest | \ 203 rkt_image_render | \ 204 rkt_image_rm | \ 205 rkt_run | \ 206 rkt_prepare) 207 __rkt_parse_image 208 return 209 ;; 210 rkt_run-prepared) 211 __rkt_parse_list prepared 212 return 213 ;; 214 rkt_enter) 215 __rkt_parse_list running 216 return 217 ;; 218 rkt_rm) 219 __rkt_parse_list "prepare\|exited" 220 return 221 ;; 222 rkt_status) 223 __rkt_parse_list 224 return 225 ;; 226 *) 227 ;; 228 esac 229 } 230 231 _rkt_api-service() 232 { 233 last_command="rkt_api-service" 234 commands=() 235 236 flags=() 237 two_word_flags=() 238 flags_with_completion=() 239 flags_completion=() 240 241 flags+=("--listen=") 242 flags+=("--debug") 243 flags+=("--dir=") 244 flags+=("--insecure-options=") 245 flags+=("--user-config=") 246 flags+=("--local-config=") 247 flags+=("--system-config=") 248 flags+=("--trust-keys-from-https") 249 250 must_have_one_flag=() 251 must_have_one_noun=() 252 } 253 254 _rkt_cat-manifest() 255 { 256 last_command="rkt_cat-manifest" 257 commands=() 258 259 flags=() 260 two_word_flags=() 261 flags_with_completion=() 262 flags_completion=() 263 264 flags+=("--pretty-print") 265 flags+=("--debug") 266 flags+=("--dir=") 267 flags+=("--insecure-options=") 268 flags+=("--user-config=") 269 flags+=("--local-config=") 270 flags+=("--system-config=") 271 flags+=("--trust-keys-from-https") 272 273 must_have_one_flag=() 274 must_have_one_noun=() 275 } 276 277 _rkt_enter() 278 { 279 last_command="rkt_enter" 280 commands=() 281 282 flags=() 283 two_word_flags=() 284 flags_with_completion=() 285 flags_completion=() 286 287 flags+=("--app=") 288 flags+=("--debug") 289 flags+=("--dir=") 290 flags+=("--insecure-options=") 291 flags+=("--user-config=") 292 flags+=("--local-config=") 293 flags+=("--system-config=") 294 flags+=("--trust-keys-from-https") 295 296 must_have_one_flag=() 297 must_have_one_noun=() 298 } 299 300 _rkt_fetch() 301 { 302 last_command="rkt_fetch" 303 commands=() 304 305 flags=() 306 two_word_flags=() 307 flags_with_completion=() 308 flags_completion=() 309 310 flags+=("--no-store") 311 flags+=("--signature=") 312 flags+=("--store-only") 313 flags+=("--debug") 314 flags+=("--dir=") 315 flags+=("--insecure-options=") 316 flags+=("--user-config=") 317 flags+=("--local-config=") 318 flags+=("--system-config=") 319 flags+=("--trust-keys-from-https") 320 321 must_have_one_flag=() 322 must_have_one_noun=() 323 } 324 325 _rkt_gc() 326 { 327 last_command="rkt_gc" 328 commands=() 329 330 flags=() 331 two_word_flags=() 332 flags_with_completion=() 333 flags_completion=() 334 335 flags+=("--expire-prepared=") 336 flags+=("--grace-period=") 337 flags+=("--debug") 338 flags+=("--dir=") 339 flags+=("--insecure-options=") 340 flags+=("--user-config=") 341 flags+=("--local-config=") 342 flags+=("--system-config=") 343 flags+=("--trust-keys-from-https") 344 345 must_have_one_flag=() 346 must_have_one_noun=() 347 } 348 349 _rkt_image_cat-manifest() 350 { 351 last_command="rkt_image_cat-manifest" 352 commands=() 353 354 flags=() 355 two_word_flags=() 356 flags_with_completion=() 357 flags_completion=() 358 359 flags+=("--pretty-print") 360 flags+=("--debug") 361 flags+=("--dir=") 362 flags+=("--insecure-options=") 363 flags+=("--user-config=") 364 flags+=("--local-config=") 365 flags+=("--system-config=") 366 flags+=("--trust-keys-from-https") 367 368 must_have_one_flag=() 369 must_have_one_noun=() 370 } 371 372 _rkt_image_export() 373 { 374 last_command="rkt_image_export" 375 commands=() 376 377 flags=() 378 two_word_flags=() 379 flags_with_completion=() 380 flags_completion=() 381 382 flags+=("--overwrite") 383 flags+=("--debug") 384 flags+=("--dir=") 385 flags+=("--insecure-options=") 386 flags+=("--user-config=") 387 flags+=("--local-config=") 388 flags+=("--system-config=") 389 flags+=("--trust-keys-from-https") 390 391 must_have_one_flag=() 392 must_have_one_noun=() 393 } 394 395 _rkt_image_extract() 396 { 397 last_command="rkt_image_extract" 398 commands=() 399 400 flags=() 401 two_word_flags=() 402 flags_with_completion=() 403 flags_completion=() 404 405 flags+=("--overwrite") 406 flags+=("--rootfs-only") 407 flags+=("--debug") 408 flags+=("--dir=") 409 flags+=("--insecure-options=") 410 flags+=("--user-config=") 411 flags+=("--local-config=") 412 flags+=("--system-config=") 413 flags+=("--trust-keys-from-https") 414 415 must_have_one_flag=() 416 must_have_one_noun=() 417 } 418 419 _rkt_image_gc() 420 { 421 last_command="rkt_image_gc" 422 commands=() 423 424 flags=() 425 two_word_flags=() 426 flags_with_completion=() 427 flags_completion=() 428 429 flags+=("--grace-period=") 430 flags+=("--debug") 431 flags+=("--dir=") 432 flags+=("--insecure-options=") 433 flags+=("--user-config=") 434 flags+=("--local-config=") 435 flags+=("--system-config=") 436 flags+=("--trust-keys-from-https") 437 438 must_have_one_flag=() 439 must_have_one_noun=() 440 } 441 442 _rkt_image_list() 443 { 444 last_command="rkt_image_list" 445 commands=() 446 447 flags=() 448 two_word_flags=() 449 flags_with_completion=() 450 flags_completion=() 451 452 flags+=("--fields=") 453 flags+=("--full") 454 flags+=("--no-legend") 455 flags+=("--order=") 456 flags+=("--sort=") 457 flags+=("--debug") 458 flags+=("--dir=") 459 flags+=("--insecure-options=") 460 flags+=("--user-config=") 461 flags+=("--local-config=") 462 flags+=("--system-config=") 463 flags+=("--trust-keys-from-https") 464 465 must_have_one_flag=() 466 must_have_one_noun=() 467 } 468 469 _rkt_image_render() 470 { 471 last_command="rkt_image_render" 472 commands=() 473 474 flags=() 475 two_word_flags=() 476 flags_with_completion=() 477 flags_completion=() 478 479 flags+=("--overwrite") 480 flags+=("--rootfs-only") 481 flags+=("--debug") 482 flags+=("--dir=") 483 flags+=("--insecure-options=") 484 flags+=("--user-config=") 485 flags+=("--local-config=") 486 flags+=("--system-config=") 487 flags+=("--trust-keys-from-https") 488 489 must_have_one_flag=() 490 must_have_one_noun=() 491 } 492 493 _rkt_image_rm() 494 { 495 last_command="rkt_image_rm" 496 commands=() 497 498 flags=() 499 two_word_flags=() 500 flags_with_completion=() 501 flags_completion=() 502 503 flags+=("--debug") 504 flags+=("--dir=") 505 flags+=("--insecure-options=") 506 flags+=("--user-config=") 507 flags+=("--local-config=") 508 flags+=("--system-config=") 509 flags+=("--trust-keys-from-https") 510 511 must_have_one_flag=() 512 must_have_one_noun=() 513 } 514 515 _rkt_image() 516 { 517 last_command="rkt_image" 518 commands=() 519 commands+=("cat-manifest") 520 commands+=("export") 521 commands+=("extract") 522 commands+=("gc") 523 commands+=("list") 524 commands+=("render") 525 commands+=("rm") 526 527 flags=() 528 two_word_flags=() 529 flags_with_completion=() 530 flags_completion=() 531 532 flags+=("--debug") 533 flags+=("--dir=") 534 flags+=("--insecure-options=") 535 flags+=("--user-config=") 536 flags+=("--local-config=") 537 flags+=("--system-config=") 538 flags+=("--trust-keys-from-https") 539 540 must_have_one_flag=() 541 must_have_one_noun=() 542 } 543 544 _rkt_list() 545 { 546 last_command="rkt_list" 547 commands=() 548 549 flags=() 550 two_word_flags=() 551 flags_with_completion=() 552 flags_completion=() 553 554 flags+=("--full") 555 flags+=("--no-legend") 556 flags+=("--debug") 557 flags+=("--dir=") 558 flags+=("--insecure-options=") 559 flags+=("--user-config=") 560 flags+=("--local-config=") 561 flags+=("--system-config=") 562 flags+=("--trust-keys-from-https") 563 564 must_have_one_flag=() 565 must_have_one_noun=() 566 } 567 568 _rkt_metadata-service() 569 { 570 last_command="rkt_metadata-service" 571 commands=() 572 573 flags=() 574 two_word_flags=() 575 flags_with_completion=() 576 flags_completion=() 577 578 flags+=("--listen-port=") 579 flags+=("--debug") 580 flags+=("--dir=") 581 flags+=("--insecure-options=") 582 flags+=("--user-config=") 583 flags+=("--local-config=") 584 flags+=("--system-config=") 585 flags+=("--trust-keys-from-https") 586 587 must_have_one_flag=() 588 must_have_one_noun=() 589 } 590 591 _rkt_prepare() 592 { 593 last_command="rkt_prepare" 594 commands=() 595 596 flags=() 597 two_word_flags=() 598 flags_with_completion=() 599 flags_completion=() 600 601 flags+=("--exec=") 602 flags+=("--inherit-env") 603 flags+=("--mount=") 604 flags+=("--no-overlay") 605 flags+=("--no-store") 606 flags+=("--pod-manifest=") 607 flags+=("--port=") 608 flags+=("--private-users") 609 flags+=("--quiet") 610 flags+=("--set-env=") 611 flags+=("--stage1-path=") 612 flags+=("--stage1-url=") 613 flags+=("--stage1-name=") 614 flags+=("--stage1-hash=") 615 flags+=("--stage1-from-dir=") 616 flags+=("--store-only") 617 flags+=("--volume=") 618 flags+=("--debug") 619 flags+=("--dir=") 620 flags+=("--insecure-options=") 621 flags+=("--user-config=") 622 flags+=("--local-config=") 623 flags+=("--system-config=") 624 flags+=("--trust-keys-from-https") 625 626 must_have_one_flag=() 627 must_have_one_noun=() 628 } 629 630 _rkt_rm() 631 { 632 last_command="rkt_rm" 633 commands=() 634 635 flags=() 636 two_word_flags=() 637 flags_with_completion=() 638 flags_completion=() 639 640 flags+=("--uuid-file=") 641 flags+=("--debug") 642 flags+=("--dir=") 643 flags+=("--insecure-options=") 644 flags+=("--user-config=") 645 flags+=("--local-config=") 646 flags+=("--system-config=") 647 flags+=("--trust-keys-from-https") 648 649 must_have_one_flag=() 650 must_have_one_noun=() 651 } 652 653 _rkt_run() 654 { 655 last_command="rkt_run" 656 commands=() 657 658 flags=() 659 two_word_flags=() 660 flags_with_completion=() 661 flags_completion=() 662 663 flags+=("--cpu=") 664 flags+=("--exec=") 665 flags+=("--inherit-env") 666 flags+=("--interactive") 667 flags+=("--mds-register") 668 flags+=("--memory=") 669 flags+=("--mount=") 670 flags+=("--net=") 671 flags+=("--no-overlay") 672 flags+=("--no-store") 673 flags+=("--pod-manifest=") 674 flags+=("--port=") 675 flags+=("--private-users") 676 flags+=("--set-env=") 677 flags+=("--signature=") 678 flags+=("--stage1-path=") 679 flags+=("--stage1-url=") 680 flags+=("--stage1-name=") 681 flags+=("--stage1-hash=") 682 flags+=("--stage1-from-dir=") 683 flags+=("--store-only") 684 flags+=("--uuid-file-save=") 685 flags+=("--volume=") 686 flags+=("--debug") 687 flags+=("--dir=") 688 flags+=("--insecure-options=") 689 flags+=("--user-config=") 690 flags+=("--local-config=") 691 flags+=("--system-config=") 692 flags+=("--trust-keys-from-https") 693 694 must_have_one_flag=() 695 must_have_one_noun=() 696 } 697 698 _rkt_run-prepared() 699 { 700 last_command="rkt_run-prepared" 701 commands=() 702 703 flags=() 704 two_word_flags=() 705 flags_with_completion=() 706 flags_completion=() 707 708 flags+=("--interactive") 709 flags+=("--mds-register") 710 flags+=("--net=") 711 flags+=("--debug") 712 flags+=("--dir=") 713 flags+=("--insecure-options=") 714 flags+=("--user-config=") 715 flags+=("--local-config=") 716 flags+=("--system-config=") 717 flags+=("--trust-keys-from-https") 718 719 must_have_one_flag=() 720 must_have_one_noun=() 721 } 722 723 _rkt_status() 724 { 725 last_command="rkt_status" 726 commands=() 727 728 flags=() 729 two_word_flags=() 730 flags_with_completion=() 731 flags_completion=() 732 733 flags+=("--wait") 734 flags+=("--debug") 735 flags+=("--dir=") 736 flags+=("--insecure-options=") 737 flags+=("--user-config=") 738 flags+=("--local-config=") 739 flags+=("--system-config=") 740 flags+=("--trust-keys-from-https") 741 742 must_have_one_flag=() 743 must_have_one_noun=() 744 } 745 746 _rkt_trust() 747 { 748 last_command="rkt_trust" 749 commands=() 750 751 flags=() 752 two_word_flags=() 753 flags_with_completion=() 754 flags_completion=() 755 756 flags+=("--insecure-allow-http") 757 flags+=("--prefix=") 758 flags+=("--root") 759 flags+=("--skip-fingerprint-review") 760 flags+=("--debug") 761 flags+=("--dir=") 762 flags+=("--insecure-options=") 763 flags+=("--user-config=") 764 flags+=("--local-config=") 765 flags+=("--system-config=") 766 flags+=("--trust-keys-from-https") 767 768 must_have_one_flag=() 769 must_have_one_noun=() 770 } 771 772 _rkt_version() 773 { 774 last_command="rkt_version" 775 commands=() 776 777 flags=() 778 two_word_flags=() 779 flags_with_completion=() 780 flags_completion=() 781 782 flags+=("--debug") 783 flags+=("--dir=") 784 flags+=("--insecure-options=") 785 flags+=("--user-config=") 786 flags+=("--local-config=") 787 flags+=("--system-config=") 788 flags+=("--trust-keys-from-https") 789 790 must_have_one_flag=() 791 must_have_one_noun=() 792 } 793 794 _rkt() 795 { 796 last_command="rkt" 797 commands=() 798 commands+=("api-service") 799 commands+=("cat-manifest") 800 commands+=("enter") 801 commands+=("fetch") 802 commands+=("gc") 803 commands+=("image") 804 commands+=("list") 805 commands+=("metadata-service") 806 commands+=("prepare") 807 commands+=("rm") 808 commands+=("run") 809 commands+=("run-prepared") 810 commands+=("status") 811 commands+=("trust") 812 commands+=("version") 813 814 flags=() 815 two_word_flags=() 816 flags_with_completion=() 817 flags_completion=() 818 819 flags+=("--debug") 820 flags+=("--dir=") 821 flags+=("--insecure-options=") 822 flags+=("--user-config=") 823 flags+=("--local-config=") 824 flags+=("--system-config=") 825 flags+=("--trust-keys-from-https") 826 827 must_have_one_flag=() 828 must_have_one_noun=() 829 } 830 831 __start_rkt() 832 { 833 local cur prev words cword 834 if declare -F _init_completion >/dev/null 2>&1; then 835 _init_completion -s || return 836 else 837 __my_init_completion || return 838 fi 839 840 local c=0 841 local flags=() 842 local two_word_flags=() 843 local flags_with_completion=() 844 local flags_completion=() 845 local commands=("rkt") 846 local must_have_one_flag=() 847 local must_have_one_noun=() 848 local last_command 849 local nouns=() 850 851 __handle_word 852 } 853 854 if [[ $(type -t compopt) = "builtin" ]]; then 855 complete -F __start_rkt rkt 856 else 857 complete -o nospace -F __start_rkt rkt 858 fi 859 860 # ex: ts=4 sw=4 et filetype=sh