_knife 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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" diff exec environment index node recipe role search ssh status upload vault 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. diff)
  46. _arguments '*:file or directory:_files -g "*"'
  47. ;;
  48. environment)
  49. compadd -Q "$@" list create delete edit show "from file"
  50. ;;
  51. node)
  52. compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
  53. ;;
  54. recipe)
  55. compadd "$@" list
  56. ;;
  57. role)
  58. compadd -Q "$@" "bulk delete" create delete edit "from file" list show
  59. ;;
  60. upload)
  61. _arguments '*:file or directory:_files -g "*"'
  62. ;;
  63. vault)
  64. compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
  65. ;;
  66. windows)
  67. compadd "$@" bootstrap
  68. ;;
  69. *)
  70. _arguments '2:Subsubcommands:($(_knife_options1))'
  71. esac
  72. ;;
  73. knifesubcmd2)
  74. case $words[3] in
  75. server)
  76. compadd "$@" list create delete
  77. ;;
  78. images)
  79. compadd "$@" list
  80. ;;
  81. site)
  82. compadd "$@" vendor show share search download list unshare
  83. ;;
  84. (show|delete|edit)
  85. _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
  86. ;;
  87. (upload|test)
  88. _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
  89. ;;
  90. list)
  91. compadd -a "$@" knife_general_flags
  92. ;;
  93. bag)
  94. compadd -Q "$@" show edit list "from file" create delete
  95. ;;
  96. *)
  97. _arguments '3:Subsubcommands:($(_knife_options2))'
  98. esac
  99. ;;
  100. knifesubcmd3)
  101. case $words[3] in
  102. show)
  103. case $words[2] in
  104. cookbook)
  105. versioncomp=1
  106. _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
  107. ;;
  108. (node|client|role)
  109. compadd "$@" --attribute
  110. esac
  111. esac
  112. case $words[4] in
  113. (show|edit)
  114. _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
  115. ;;
  116. file)
  117. _arguments '*:file or directory:_files -g "*.(rb|json)"'
  118. ;;
  119. list)
  120. compadd -a "$@" knife_general_flags
  121. ;;
  122. *)
  123. _arguments '*:Subsubcommands:($(_knife_options3))'
  124. esac
  125. ;;
  126. knifesubcmd4)
  127. if (( versioncomp > 0 )); then
  128. compadd "$@" attributes definitions files libraries providers recipes resources templates
  129. else
  130. _arguments '*:Subsubcommands:($(_knife_options2))'
  131. fi
  132. ;;
  133. knifesubcmd5)
  134. _arguments '*:Subsubcommands:($(_knife_options3))'
  135. esac
  136. }
  137. # Helper functions to provide the argument completion for several depths of commands
  138. _knife_options1() {
  139. ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  140. }
  141. _knife_options2() {
  142. ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  143. }
  144. _knife_options3() {
  145. ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
  146. }
  147. # The chef_x_remote functions use knife to get a list of objects of type x on the server
  148. _chef_roles_remote() {
  149. (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  150. }
  151. _chef_clients_remote() {
  152. (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  153. }
  154. _chef_nodes_remote() {
  155. (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  156. }
  157. _chef_cookbooks_remote() {
  158. (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  159. }
  160. _chef_sitecookbooks_remote() {
  161. (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  162. }
  163. _chef_data_bags_remote() {
  164. (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  165. }
  166. _chef_environments_remote() {
  167. (knife environment list | awk '{print $1}')
  168. }
  169. # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
  170. _chef_cookbooks_local() {
  171. local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
  172. if [ -f ./.chef/knife.rb ]; then
  173. knife_rb="./.chef/knife.rb"
  174. fi
  175. 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/' )}
  176. (for i in $cookbook_path; do ls $i; done)
  177. }
  178. # This function extracts the available cookbook versions on the chef server
  179. _cookbook_versions() {
  180. (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
  181. }
  182. _knife "$@"