3 # You can override the path to knife.rb and your cookbooks by setting
4 # KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
5 # KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
6 # If you want your local cookbooks path to be calculated relative to where you are then
8 # KNIFE_RELATIVE_PATH=true
9 # Read around where these are used for more detail.
11 # These flags should be available everywhere according to man knife
12 knife_general_flags=(--help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes)
14 # knife has a very special syntax, some example calls are:
17 # knife role show ROLENAME
18 # knife data bag show DATABAGNAME
19 # knife role show ROLENAME --attribute ATTRIBUTENAME
20 # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
22 # 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
24 local curcontext="$curcontext" state line
26 cloudproviders=(bluebox ec2 rackspace slicehost terremark)
30 '3: :->knifesubcmd2' \
31 '4: :->knifesubcmd3' \
32 '5: :->knifesubcmd4' \
37 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
41 bluebox|ec2|rackspace|slicehost|terremark)
42 compadd "$@" server images
45 compadd -Q "$@" "bulk delete" list create show delete edit reregister
51 compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
54 _arguments '*:file or directory:_files -g "*"'
57 compadd -Q "$@" list create delete edit show "from file"
60 compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
66 compadd -Q "$@" "bulk delete" create delete edit "from file" list show
69 compadd "$@" bootstrap clean cook init prepare
72 _arguments '*:file or directory:_files -g "*"'
75 compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
78 compadd "$@" bootstrap
81 _arguments '2:Subsubcommands:($(_knife_options1))'
88 compadd "$@" list create delete
94 compadd "$@" vendor show share search download list unshare
97 _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
100 _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
103 compadd -a "$@" knife_general_flags
106 compadd -Q "$@" show edit list "from file" create delete
108 bootstrap|clean|cook|prepare)
109 compadd "$@" nodes/*.json(N:t:r)
112 compadd "$@" ./*(/N:t)
115 _arguments '3:Subsubcommands:($(_knife_options2))'
125 _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
128 compadd "$@" --attribute
135 _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
140 _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
143 _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
146 _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
149 _arguments '*:Subsubcommands:($(_knife_options3))'
154 compadd -a "$@" knife_general_flags
157 _arguments '*:Subsubcommands:($(_knife_options3))'
162 if ((versioncomp > 0)); then
163 compadd "$@" attributes definitions files libraries providers recipes resources templates
167 _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
169 *) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
176 _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
179 _arguments '*:Subsubcommands:($(_knife_options3))'
186 # Helper functions to provide the argument completion for several depths of commands
188 (for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
192 (for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
196 (for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
199 # The chef_x_remote functions use knife to get a list of objects of type x on the server
200 _chef_roles_remote() {
201 (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
204 _chef_clients_remote() {
205 (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
208 _chef_nodes_remote() {
209 (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
212 _chef_cookbooks_remote() {
213 (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
216 _chef_sitecookbooks_remote() {
217 (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
220 _chef_data_bags_remote() {
221 (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
224 _chef_environments_remote() {
225 (knife environment list | awk '{print $1}')
228 # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
229 _chef_cookbooks_local() {
230 if [ $KNIFE_RELATIVE_PATH ]; then
231 local cookbook_path="$(_chef_root)/cookbooks"
233 local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
234 if [ -f ./.chef/knife.rb ]; then
235 knife_rb="./.chef/knife.rb"
237 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/')}
239 (for i in $cookbook_path; do ls $i; done)
242 # This function extracts the available cookbook versions on the chef server
243 _cookbook_versions() {
244 (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
247 # Searches up from current directory to find the closest folder that has a .chef folder
248 # Useful for the knife upload/from file commands
251 while [ $directory != '/' ]; do
252 test -e "$directory/.chef" && echo "$directory" && return
253 directory="${directory:h}"