2011-06-30 21:19:50 +08:00
#compdef knife
2013-04-02 04:44:53 +08:00
# You can override the path to knife.rb and your cookbooks by setting
# KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
# KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
2019-12-27 07:49:50 +08:00
# If you want your local cookbooks path to be calculated relative to where you are then
2016-06-08 23:27:51 +08:00
# set the below option
2019-12-27 07:49:50 +08:00
# KNIFE_RELATIVE_PATH=true
2013-04-02 04:44:53 +08:00
# Read around where these are used for more detail.
2011-06-30 21:19:50 +08:00
# These flags should be available everywhere according to man knife
2019-12-27 07:49:50 +08:00
knife_general_flags=(--help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes)
2011-06-30 21:19:50 +08:00
# knife has a very special syntax, some example calls are:
# knife status
# knife cookbook list
# knife role show ROLENAME
# knife data bag show DATABAGNAME
# knife role show ROLENAME --attribute ATTRIBUTENAME
# knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
# 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
_knife() {
local curcontext="$curcontext" state line
typeset -A opt_args
cloudproviders=(bluebox ec2 rackspace slicehost terremark)
_arguments \
2019-12-27 07:49:50 +08:00
'1: :->knifecmd' \
'2: :->knifesubcmd' \
2011-06-30 21:19:50 +08:00
'3: :->knifesubcmd2' \
'4: :->knifesubcmd3' \
'5: :->knifesubcmd4' \
'6: :->knifesubcmd5'
2019-12-27 07:49:50 +08:00
2011-06-30 21:19:50 +08:00
case $state in
knifecmd)
2014-11-08 00:24:03 +08:00
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
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
knifesubcmd)
case $words[2] in
2019-12-27 07:49:50 +08:00
bluebox|ec2|rackspace|slicehost|terremark)
2011-06-30 21:19:50 +08:00
compadd "$@" server images
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
client)
compadd -Q "$@" "bulk delete" list create show delete edit reregister
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
configure)
compadd "$@" client
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
cookbook)
compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
2019-12-27 07:49:50 +08:00
;;
2013-04-09 05:07:06 +08:00
diff)
_arguments '*:file or directory:_files -g "*"'
2019-12-27 07:49:50 +08:00
;;
2013-04-09 05:07:06 +08:00
environment)
2011-12-19 13:15:00 +08:00
compadd -Q "$@" list create delete edit show "from file"
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
node)
2019-12-27 07:49:50 +08:00
compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
;;
2011-06-30 21:19:50 +08:00
recipe)
2019-12-27 07:49:50 +08:00
compadd "$@" list
;;
2011-06-30 21:19:50 +08:00
role)
compadd -Q "$@" "bulk delete" create delete edit "from file" list show
2019-12-27 07:49:50 +08:00
;;
2014-11-08 00:24:03 +08:00
solo)
compadd "$@" bootstrap clean cook init prepare
;;
2013-04-09 05:07:06 +08:00
upload)
2019-12-27 07:49:50 +08:00
_arguments '*:file or directory:_files -g "*"'
;;
2014-03-18 17:52:25 +08:00
vault)
compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
windows)
compadd "$@" bootstrap
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
*)
2019-12-27 07:49:50 +08:00
_arguments '2:Subsubcommands:($(_knife_options1))'
;;
2011-06-30 21:19:50 +08:00
esac
2019-12-27 07:49:50 +08:00
;;
knifesubcmd2)
2011-06-30 21:19:50 +08:00
case $words[3] in
2019-12-27 07:49:50 +08:00
server)
2011-06-30 21:19:50 +08:00
compadd "$@" list create delete
2019-12-27 07:49:50 +08:00
;;
images)
2011-06-30 21:19:50 +08:00
compadd "$@" list
2019-12-27 07:49:50 +08:00
;;
site)
2011-06-30 21:19:50 +08:00
compadd "$@" vendor show share search download list unshare
2019-12-27 07:49:50 +08:00
;;
show|delete|edit)
_arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
;;
upload|test)
_arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
;;
2011-06-30 21:19:50 +08:00
list)
2019-12-27 07:49:50 +08:00
compadd -a "$@" knife_general_flags
;;
2011-06-30 21:19:50 +08:00
bag)
compadd -Q "$@" show edit list "from file" create delete
2019-12-27 07:49:50 +08:00
;;
2014-11-08 00:24:03 +08:00
bootstrap|clean|cook|prepare)
2020-03-04 16:12:58 +08:00
compadd "$@" nodes/*.json(N:t:r)
2014-11-08 00:24:03 +08:00
;;
init)
2020-03-04 16:12:58 +08:00
compadd "$@" ./*(/N:t)
2014-11-08 00:24:03 +08:00
;;
2011-06-30 21:19:50 +08:00
*)
_arguments '3:Subsubcommands:($(_knife_options2))'
2019-12-27 07:49:50 +08:00
;;
2011-06-30 21:19:50 +08:00
esac
;;
2019-12-27 07:49:50 +08:00
knifesubcmd3)
case $words[3] in
show)
case $words[2] in
cookbook)
versioncomp=1
_arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
;;
node|client|role)
compadd "$@" --attribute
;;
esac
;;
esac
case $words[4] in
show|edit)
_arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
;;
file)
2016-06-08 23:27:51 +08:00
case $words[2] in
environment)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
2019-12-27 07:49:50 +08:00
;;
2016-06-08 23:27:51 +08:00
node)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
2019-12-27 07:49:50 +08:00
;;
2016-06-08 23:27:51 +08:00
role)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
2019-12-27 07:49:50 +08:00
;;
2016-06-08 23:27:51 +08:00
*)
_arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac
2019-12-27 07:49:50 +08:00
;;
list)
compadd -a "$@" knife_general_flags
;;
*)
_arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac
;;
knifesubcmd4)
if ((versioncomp > 0)); then
compadd "$@" attributes definitions files libraries providers recipes resources templates
else
case $words[5] in
file)
_arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
2016-06-08 23:27:51 +08:00
;;
2019-12-27 07:49:50 +08:00
*) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
2016-06-08 23:27:51 +08:00
esac
2019-12-27 07:49:50 +08:00
fi
;;
knifesubcmd5)
case $words[5] in
file)
_arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
;;
*)
_arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac
;;
esac
2011-06-30 21:19:50 +08:00
}
# Helper functions to provide the argument completion for several depths of commands
_knife_options1() {
2019-12-27 07:49:50 +08:00
(for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
2011-06-30 21:19:50 +08:00
}
_knife_options2() {
2019-12-27 07:49:50 +08:00
(for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
2011-06-30 21:19:50 +08:00
}
_knife_options3() {
2019-12-27 07:49:50 +08:00
(for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
2011-06-30 21:19:50 +08:00
}
# The chef_x_remote functions use knife to get a list of objects of type x on the server
_chef_roles_remote() {
2019-12-27 07:49:50 +08:00
(knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
_chef_clients_remote() {
2019-12-27 07:49:50 +08:00
(knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
_chef_nodes_remote() {
2019-12-27 07:49:50 +08:00
(knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
_chef_cookbooks_remote() {
2019-12-27 07:49:50 +08:00
(knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
_chef_sitecookbooks_remote() {
2019-12-27 07:49:50 +08:00
(knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
_chef_data_bags_remote() {
2019-12-27 07:49:50 +08:00
(knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
2011-06-30 21:19:50 +08:00
}
2011-12-19 13:15:00 +08:00
_chef_environments_remote() {
(knife environment list | awk '{print $1}')
}
2011-06-30 21:19:50 +08:00
# The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
_chef_cookbooks_local() {
2019-12-27 07:49:50 +08:00
if [ $KNIFE_RELATIVE_PATH ]; then
2016-06-08 23:27:51 +08:00
local cookbook_path="$(_chef_root)/cookbooks"
2019-12-27 07:49:50 +08:00
else
2016-06-08 23:27:51 +08:00
local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
if [ -f ./.chef/knife.rb ]; then
knife_rb="./.chef/knife.rb"
fi
2019-12-27 07:49:50 +08:00
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/')}
2013-04-02 04:34:59 +08:00
fi
(for i in $cookbook_path; do ls $i; done)
2011-06-30 21:19:50 +08:00
}
# This function extracts the available cookbook versions on the chef server
_cookbook_versions() {
(knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
}
2019-12-27 07:49:50 +08:00
# Searches up from current directory to find the closest folder that has a .chef folder
# Useful for the knife upload/from file commands
_chef_root() {
2016-06-08 23:27:51 +08:00
directory="$PWD"
2019-12-27 07:49:50 +08:00
while [ $directory != '/' ]; do
2016-06-08 23:27:51 +08:00
test -e "$directory/.chef" && echo "$directory" && return
directory="${directory:h}"
done
}
2011-06-30 21:19:50 +08:00
_knife "$@"