connect 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. _connect_complete_subcommand ()
  2. {
  3. local command="${COMP_WORDS[1]}"
  4. local subcommands="$(${COMP_WORDS[0]} $command --help 2> /dev/null | awk '/^ [a-z]/ { print $1 }')"
  5. COMPREPLY=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
  6. }
  7. _connect_complete_host ()
  8. {
  9. local prefix="$1"
  10. if [ -z "$_connect_host" ]
  11. then
  12. # Cache the complete list because it rarely changes and makes
  13. # completion much faster.
  14. _connect_host="$((awk '/\.intr/ { print $1 }' $HOME/.ssh/known_hosts | cut -d: -f 1 | sed -e 's/\[//' -e 's/\]//' | cut -d, -f 1) 2> /dev/null)"
  15. fi
  16. COMPREPLY=($(compgen -W "$_connect_host" -- "$prefix"))
  17. }
  18. _connect_complete_option ()
  19. {
  20. local subcommand
  21. case "${COMP_WORDS[2]}" in
  22. -*) subcommand="";;
  23. [a-z]*) subcommand="${COMP_WORDS[2]}";;
  24. esac
  25. local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} $subcommand --help 2> /dev/null \
  26. | grep '^ \+-' \
  27. | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g')"
  28. compopt -o nospace
  29. COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[${#COMP_WORDS[*]} - 1]}"))
  30. }
  31. _connect_is_command ()
  32. {
  33. local word
  34. local result="false"
  35. for word in ${COMP_WORDS[*]}
  36. do
  37. if [ "$word" = "$1" ]
  38. then
  39. result=true
  40. break
  41. fi
  42. done
  43. $result
  44. }
  45. _connect_complete()
  46. {
  47. local word_count=${#COMP_WORDS[*]}
  48. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  49. if [ "$COMP_CWORD" -gt 1 ]
  50. then
  51. case "$word_at_point" in
  52. -*)
  53. _connect_complete_option "$word_at_point"
  54. return
  55. ;;
  56. esac
  57. fi
  58. case $COMP_CWORD in
  59. 1)
  60. if [ -z "$_connect_subcommands" ]
  61. then
  62. # Cache the list of subcommands to speed things up.
  63. _connect_subcommands="$(connect --help 2> /dev/null | awk '/^ [a-z]/ { print $1 }')"
  64. fi
  65. COMPREPLY=($(compgen -W "$_connect_subcommands" -- "$word_at_point"))
  66. ;;
  67. *)
  68. if _connect_is_command "containers" \
  69. || _connect_is_command "deploy" \
  70. || _connect_is_command "emacs" \
  71. || _connect_is_command "filter" \
  72. || _connect_is_command "firewall" \
  73. || _connect_is_command "images" \
  74. || _connect_is_command "interfaces" \
  75. || _connect_is_command "io" \
  76. || _connect_is_command "ip" \
  77. || _connect_is_command "mysql" \
  78. || _connect_is_command "net" \
  79. || _connect_is_command "nginx" \
  80. || _connect_is_command "nginx-config" \
  81. || _connect_is_command "ping" \
  82. || _connect_is_command "php" \
  83. || _connect_is_command "protected" \
  84. || _connect_is_command "route" \
  85. || _connect_is_command "sg" \
  86. || _connect_is_command "shell" \
  87. || _connect_is_command "sniff" \
  88. || _connect_is_command "ssh" \
  89. || _connect_is_command "sshrc" \
  90. || _connect_is_command "te" \
  91. || _connect_is_command "traceroute" \
  92. || _connect_is_command "unfilter" \
  93. || _connect_is_command "uptime"
  94. then
  95. _connect_complete_host "$word_at_point"
  96. elif _connect_is_command "br1-mr14.intr" \
  97. || _connect_is_command "sr1-mr13-14.intr" \
  98. || _connect_is_command "sr1-dh507-508.intr"
  99. then
  100. COMPREPLY=($(compgen -W "configuration bgp log" -- "${COMP_WORDS[$COMP_CWORD]}"))
  101. elif _connect_is_command "ip-filter"
  102. then
  103. case $COMP_CWORD in
  104. 2) _connect_complete_subcommand;;
  105. esac
  106. fi
  107. ;;
  108. esac
  109. }
  110. complete -F _connect_complete connect