_knife 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. # If you want your local cookbooks path to be calculated relative to where you are then
  6. # set the below option
  7. # KNIFE_RELATIVE_PATH=true
  8. # Read around where these are used for more detail.
  9. # These flags should be available everywhere according to man knife
  10. knife_general_flags=(--help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes)
  11. # knife has a very special syntax, some example calls are:
  12. # knife status
  13. # knife cookbook list
  14. # knife role show ROLENAME
  15. # knife data bag show DATABAGNAME
  16. # knife role show ROLENAME --attribute ATTRIBUTENAME
  17. # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
  18. # 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
  19. _knife() {
  20. local curcontext="$curcontext" state line
  21. typeset -A opt_args
  22. cloudproviders=(bluebox ec2 rackspace slicehost terremark)
  23. _arguments \
  24. '1: :->knifecmd' \
  25. '2: :->knifesubcmd' \
  26. '3: :->knifesubcmd2' \
  27. '4: :->knifesubcmd3' \
  28. '5: :->knifesubcmd4' \
  29. '6: :->knifesubcmd5'
  30. case $state in
  31. knifecmd)
  32. compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff exec environment index node recipe role search ssh status upload vault windows $cloudproviders
  33. ;;
  34. knifesubcmd)
  35. case $words[2] in
  36. bluebox|ec2|rackspace|slicehost|terremark)
  37. compadd "$@" server images
  38. ;;
  39. client)
  40. compadd -Q "$@" "bulk delete" list create show delete edit reregister
  41. ;;
  42. configure)
  43. compadd "$@" client
  44. ;;
  45. cookbook)
  46. compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
  47. ;;
  48. diff)
  49. _arguments '*:file or directory:_files -g "*"'
  50. ;;
  51. environment)
  52. compadd -Q "$@" list create delete edit show "from file"
  53. ;;
  54. node)
  55. compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
  56. ;;
  57. recipe)
  58. compadd "$@" list
  59. ;;
  60. role)
  61. compadd -Q "$@" "bulk delete" create delete edit "from file" list show
  62. ;;
  63. upload)
  64. _arguments '*:file or directory:_files -g "*"'
  65. ;;
  66. vault)
  67. compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
  68. ;;
  69. windows)
  70. compadd "$@" bootstrap
  71. ;;
  72. *)
  73. _arguments '2:Subsubcommands:($(_knife_options1))'
  74. ;;
  75. esac
  76. ;;
  77. knifesubcmd2)
  78. case $words[3] in
  79. server)
  80. compadd "$@" list create delete
  81. ;;
  82. images)
  83. compadd "$@" list
  84. ;;
  85. site)
  86. compadd "$@" vendor show share search download list unshare
  87. ;;
  88. show|delete|edit)
  89. _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
  90. ;;
  91. upload|test)
  92. _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
  93. ;;
  94. list)
  95. compadd -a "$@" knife_general_flags
  96. ;;
  97. bag)
  98. compadd -Q "$@" show edit list "from file" create delete
  99. ;;
  100. *)
  101. _arguments '3:Subsubcommands:($(_knife_options2))'
  102. ;;
  103. esac
  104. ;;
  105. knifesubcmd3)
  106. case $words[3] in
  107. show)
  108. case $words[2] in
  109. cookbook)
  110. versioncomp=1
  111. _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
  112. ;;
  113. node|client|role)
  114. compadd "$@" --attribute
  115. ;;
  116. esac
  117. ;;
  118. esac
  119. case $words[4] in
  120. show|edit)
  121. _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
  122. ;;
  123. file)
  124. case $words[2] in
  125. environment)
  126. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
  127. ;;
  128. node)
  129. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
  130. ;;
  131. role)
  132. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
  133. ;;
  134. *)
  135. _arguments '*:Subsubcommands:($(_knife_options3))'
  136. ;;
  137. esac
  138. ;;
  139. list)
  140. compadd -a "$@" knife_general_flags
  141. ;;
  142. *)
  143. _arguments '*:Subsubcommands:($(_knife_options3))'
  144. ;;
  145. esac
  146. ;;
  147. knifesubcmd4)
  148. if ((versioncomp > 0)); then
  149. compadd "$@" attributes definitions files libraries providers recipes resources templates
  150. else
  151. case $words[5] in
  152. file)
  153. _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
  154. ;;
  155. *) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
  156. esac
  157. fi
  158. ;;
  159. knifesubcmd5)
  160. case $words[5] in
  161. file)
  162. _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
  163. ;;
  164. *)
  165. _arguments '*:Subsubcommands:($(_knife_options3))'
  166. ;;
  167. esac
  168. ;;
  169. esac
  170. }
  171. # Helper functions to provide the argument completion for several depths of commands
  172. _knife_options1() {
  173. (for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  174. }
  175. _knife_options2() {
  176. (for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  177. }
  178. _knife_options3() {
  179. (for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  180. }
  181. # The chef_x_remote functions use knife to get a list of objects of type x on the server
  182. _chef_roles_remote() {
  183. (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  184. }
  185. _chef_clients_remote() {
  186. (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  187. }
  188. _chef_nodes_remote() {
  189. (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  190. }
  191. _chef_cookbooks_remote() {
  192. (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  193. }
  194. _chef_sitecookbooks_remote() {
  195. (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  196. }
  197. _chef_data_bags_remote() {
  198. (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  199. }
  200. _chef_environments_remote() {
  201. (knife environment list | awk '{print $1}')
  202. }
  203. # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
  204. _chef_cookbooks_local() {
  205. if [ $KNIFE_RELATIVE_PATH ]; then
  206. local cookbook_path="$(_chef_root)/cookbooks"
  207. else
  208. local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
  209. if [ -f ./.chef/knife.rb ]; then
  210. knife_rb="./.chef/knife.rb"
  211. fi
  212. 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/')}
  213. fi
  214. (for i in $cookbook_path; do ls $i; done)
  215. }
  216. # This function extracts the available cookbook versions on the chef server
  217. _cookbook_versions() {
  218. (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
  219. }
  220. # Searches up from current directory to find the closest folder that has a .chef folder
  221. # Useful for the knife upload/from file commands
  222. _chef_root() {
  223. directory="$PWD"
  224. while [ $directory != '/' ]; do
  225. test -e "$directory/.chef" && echo "$directory" && return
  226. directory="${directory:h}"
  227. done
  228. }
  229. _knife "$@"