_knife 6.1 KB

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