_knife 5.5 KB

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