my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.7 KiB

3 years ago
  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 solo 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. solo)
  64. compadd "$@" bootstrap clean cook init prepare
  65. ;;
  66. upload)
  67. _arguments '*:file or directory:_files -g "*"'
  68. ;;
  69. vault)
  70. compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
  71. ;;
  72. windows)
  73. compadd "$@" bootstrap
  74. ;;
  75. *)
  76. _arguments '2:Subsubcommands:($(_knife_options1))'
  77. ;;
  78. esac
  79. ;;
  80. knifesubcmd2)
  81. case $words[3] in
  82. server)
  83. compadd "$@" list create delete
  84. ;;
  85. images)
  86. compadd "$@" list
  87. ;;
  88. site)
  89. compadd "$@" vendor show share search download list unshare
  90. ;;
  91. show|delete|edit)
  92. _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
  93. ;;
  94. upload|test)
  95. _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
  96. ;;
  97. list)
  98. compadd -a "$@" knife_general_flags
  99. ;;
  100. bag)
  101. compadd -Q "$@" show edit list "from file" create delete
  102. ;;
  103. bootstrap|clean|cook|prepare)
  104. compadd "$@" nodes/*.json(N:t:r)
  105. ;;
  106. init)
  107. compadd "$@" ./*(/N:t)
  108. ;;
  109. *)
  110. _arguments '3:Subsubcommands:($(_knife_options2))'
  111. ;;
  112. esac
  113. ;;
  114. knifesubcmd3)
  115. case $words[3] in
  116. show)
  117. case $words[2] in
  118. cookbook)
  119. versioncomp=1
  120. _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
  121. ;;
  122. node|client|role)
  123. compadd "$@" --attribute
  124. ;;
  125. esac
  126. ;;
  127. esac
  128. case $words[4] in
  129. show|edit)
  130. _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
  131. ;;
  132. file)
  133. case $words[2] in
  134. environment)
  135. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
  136. ;;
  137. node)
  138. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
  139. ;;
  140. role)
  141. _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
  142. ;;
  143. *)
  144. _arguments '*:Subsubcommands:($(_knife_options3))'
  145. ;;
  146. esac
  147. ;;
  148. list)
  149. compadd -a "$@" knife_general_flags
  150. ;;
  151. *)
  152. _arguments '*:Subsubcommands:($(_knife_options3))'
  153. ;;
  154. esac
  155. ;;
  156. knifesubcmd4)
  157. if ((versioncomp > 0)); then
  158. compadd "$@" attributes definitions files libraries providers recipes resources templates
  159. else
  160. case $words[5] in
  161. file)
  162. _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
  163. ;;
  164. *) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
  165. esac
  166. fi
  167. ;;
  168. knifesubcmd5)
  169. case $words[5] in
  170. file)
  171. _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
  172. ;;
  173. *)
  174. _arguments '*:Subsubcommands:($(_knife_options3))'
  175. ;;
  176. esac
  177. ;;
  178. esac
  179. }
  180. # Helper functions to provide the argument completion for several depths of commands
  181. _knife_options1() {
  182. (for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  183. }
  184. _knife_options2() {
  185. (for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  186. }
  187. _knife_options3() {
  188. (for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
  189. }
  190. # The chef_x_remote functions use knife to get a list of objects of type x on the server
  191. _chef_roles_remote() {
  192. (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  193. }
  194. _chef_clients_remote() {
  195. (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  196. }
  197. _chef_nodes_remote() {
  198. (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  199. }
  200. _chef_cookbooks_remote() {
  201. (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  202. }
  203. _chef_sitecookbooks_remote() {
  204. (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  205. }
  206. _chef_data_bags_remote() {
  207. (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
  208. }
  209. _chef_environments_remote() {
  210. (knife environment list | awk '{print $1}')
  211. }
  212. # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
  213. _chef_cookbooks_local() {
  214. if [ $KNIFE_RELATIVE_PATH ]; then
  215. local cookbook_path="$(_chef_root)/cookbooks"
  216. else
  217. local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
  218. if [ -f ./.chef/knife.rb ]; then
  219. knife_rb="./.chef/knife.rb"
  220. fi
  221. 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/')}
  222. fi
  223. (for i in $cookbook_path; do ls $i; done)
  224. }
  225. # This function extracts the available cookbook versions on the chef server
  226. _cookbook_versions() {
  227. (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
  228. }
  229. # Searches up from current directory to find the closest folder that has a .chef folder
  230. # Useful for the knife upload/from file commands
  231. _chef_root() {
  232. directory="$PWD"
  233. while [ $directory != '/' ]; do
  234. test -e "$directory/.chef" && echo "$directory" && return
  235. directory="${directory:h}"
  236. done
  237. }
  238. _knife "$@"