_knife 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #compdef knife
  2. # You can override the path to knife.rb and your cookbooks by setting
  3. # KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
  4. # KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
  5. # Read around where these are used for more detail.
  6. # These flags should be available everywhere according to man knife
  7. knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes )
  8. # knife has a very special syntax, some example calls are:
  9. # knife status
  10. # knife cookbook list
  11. # knife role show ROLENAME
  12. # knife data bag show DATABAGNAME
  13. # knife role show ROLENAME --attribute ATTRIBUTENAME
  14. # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
  15. # The -Q switch in compadd allow for completions of things like "data bag" without having to go through two rounds of completion and avoids zsh inserting a \ for escaping spaces
  16. _knife() {
  17. local curcontext="$curcontext" state line
  18. typeset -A opt_args
  19. cloudproviders=(bluebox ec2 rackspace slicehost terremark)
  20. _arguments \
  21. '1: :->knifecmd'\
  22. '2: :->knifesubcmd'\
  23. '3: :->knifesubcmd2' \
  24. '4: :->knifesubcmd3' \
  25. '5: :->knifesubcmd4' \
  26. '6: :->knifesubcmd5'
  27. case $state in
  28. knifecmd)
  29. compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" exec environment index node recipe role search ssh status windows $cloudproviders
  30. ;;
  31. knifesubcmd)
  32. case $words[2] in
  33. (bluebox|ec2|rackspace|slicehost|terremark)
  34. compadd "$@" server images
  35. ;;
  36. client)
  37. compadd -Q "$@" "bulk delete" list create show delete edit reregister
  38. ;;
  39. configure)
  40. compadd "$@" client
  41. ;;
  42. cookbook)
  43. compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
  44. ;;
  45. environment)
  46. compadd -Q "$@" list create delete edit show "from file"
  47. ;;
  48. node)
  49. compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
  50. ;;
  51. recipe)
  52. compadd "$@" list
  53. ;;
  54. role)
  55. compadd -Q "$@" "bulk delete" create delete edit "from file" list show
  56. ;;
  57. windows)
  58. compadd "$@" bootstrap
  59. ;;
  60. *)
  61. _arguments '2:Subsubcommands:($(_knife_options1))'
  62. esac
  63. ;;
  64. knifesubcmd2)
  65. case $words[3] in
  66. server)
  67. compadd "$@" list create delete
  68. ;;
  69. images)
  70. compadd "$@" list
  71. ;;
  72. site)
  73. compadd "$@" vendor show share search download list unshare
  74. ;;
  75. (show|delete|edit)
  76. _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
  77. ;;
  78. (upload|test)
  79. _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
  80. ;;
  81. list)
  82. compadd -a "$@" knife_general_flags
  83. ;;
  84. bag)
  85. compadd -Q "$@" show edit list "from file" create delete
  86. ;;
  87. *)
  88. _arguments '3:Subsubcommands:($(_knife_options2))'
  89. esac
  90. ;;
  91. knifesubcmd3)
  92. case $words[3] in
  93. show)
  94. case $words[2] in
  95. cookbook)
  96. versioncomp=1
  97. _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
  98. ;;
  99. (node|client|role)
  100. compadd "$@" --attribute
  101. esac
  102. esac
  103. case $words[4] in
  104. (show|edit)
  105. _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
  106. ;;
  107. file)
  108. _arguments '*:file or directory:_files -g "*.(rb|json)"'
  109. ;;
  110. list)
  111. compadd -a "$@" knife_general_flags
  112. ;;
  113. *)
  114. _arguments '*:Subsubcommands:($(_knife_options3))'
  115. esac
  116. ;;
  117. knifesubcmd4)
  118. if (( versioncomp > 0 )); then
  119. compadd "$@" attributes definitions files libraries providers recipes resources templates
  120. else
  121. _arguments '*:Subsubcommands:($(_knife_options2))'
  122. fi
  123. ;;
  124. knifesubcmd5)
  125. _arguments '*:Subsubcommands:($(_knife_options3))'
  126. esac
  127. }
  128. # Helper functions to provide the argument completion for several depths of commands
  129. _knife_options1() {
  130. ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  131. }
  132. _knife_options2() {
  133. ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  134. }
  135. _knife_options3() {
  136. ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  137. }
  138. # The chef_x_remote functions use knife to get a list of objects of type x on the server
  139. _chef_roles_remote() {
  140. (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  141. }
  142. _chef_clients_remote() {
  143. (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  144. }
  145. _chef_nodes_remote() {
  146. (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  147. }
  148. _chef_cookbooks_remote() {
  149. (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  150. }
  151. _chef_sitecookbooks_remote() {
  152. (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  153. }
  154. _chef_data_bags_remote() {
  155. (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  156. }
  157. _chef_environments_remote() {
  158. (knife environment list | awk '{print $1}')
  159. }
  160. # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
  161. _chef_cookbooks_local() {
  162. local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
  163. if [ -f ./.chef/knife.rb ]; then
  164. knife_rb="./.chef/knife.rb"
  165. fi
  166. local cookbook_path=${KNIFE_COOKBOOK_PATH:-$(grep cookbook_path $knife_rb | awk 'BEGIN {FS = "[" }; {print $2}' | sed 's/\,//g' | sed "s/'//g" | sed 's/\(.*\)]/\1/' )}
  167. (for i in $cookbook_path; do ls $i; done)
  168. }
  169. # This function extracts the available cookbook versions on the chef server
  170. _cookbook_versions() {
  171. (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
  172. }
  173. _knife "$@"