_knife 5.3 KB

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