3 # These flags should be available everywhere according to man knife
4 knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes )
6 # knife has a very special syntax, some example calls are:
9 # knife role show ROLENAME
10 # knife data bag show DATABAGNAME
11 # knife role show ROLENAME --attribute ATTRIBUTENAME
12 # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
14 # 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 local curcontext="$curcontext" state line
18 cloudproviders=(bluebox ec2 rackspace slicehost terremark)
22 '3: :->knifesubcmd2' \
23 '4: :->knifesubcmd3' \
24 '5: :->knifesubcmd4' \
29 compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" exec environment index node recipe role search ssh status windows $cloudproviders
33 (bluebox|ec2|rackspace|slicehost|terremark)
34 compadd "$@" server images
37 compadd -Q "$@" "bulk delete" list create show delete edit reregister
43 compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
46 compadd -Q "$@" list create delete edit show "from file"
49 compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
55 compadd -Q "$@" "bulk delete" create delete edit "from file" list show
58 compadd "$@" bootstrap
61 _arguments '2:Subsubcommands:($(_knife_options1))'
67 compadd "$@" list create delete
73 compadd "$@" vendor show share search download list unshare
76 _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
79 _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
82 compadd -a "$@" knife_general_flags
85 compadd -Q "$@" show edit list "from file" create delete
88 _arguments '3:Subsubcommands:($(_knife_options2))'
97 _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
100 compadd "$@" --attribute
105 _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
108 _arguments '*:file or directory:_files -g "*.(rb|json)"'
111 compadd -a "$@" knife_general_flags
114 _arguments '*:Subsubcommands:($(_knife_options3))'
118 if (( versioncomp > 0 )); then
119 compadd "$@" attributes definitions files libraries providers recipes resources templates
121 _arguments '*:Subsubcommands:($(_knife_options2))'
125 _arguments '*:Subsubcommands:($(_knife_options3))'
129 # Helper functions to provide the argument completion for several depths of commands
131 ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
135 ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
139 ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
142 # The chef_x_remote functions use knife to get a list of objects of type x on the server
143 _chef_roles_remote() {
144 (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
147 _chef_clients_remote() {
148 (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
151 _chef_nodes_remote() {
152 (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
155 _chef_cookbooks_remote() {
156 (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
159 _chef_sitecookbooks_remote() {
160 (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
163 _chef_data_bags_remote() {
164 (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
167 _chef_environments_remote() {
168 (knife environment list | awk '{print $1}')
171 # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
172 _chef_cookbooks_local() {
173 (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)
176 # This function extracts the available cookbook versions on the chef server
177 _cookbook_versions() {
178 (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')