knife: sync with oh-my-zsh

This commit is contained in:
midchildan 2021-11-19 00:02:10 +09:00
parent a1a11caf4f
commit b5de3d53e0
No known key found for this signature in database
GPG Key ID: D9A5748BACC6E3C2
1 changed files with 160 additions and 95 deletions

View File

@ -38,6 +38,17 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# 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
# If you want your local cookbooks path to be calculated relative to where you are then
# set the below option
# KNIFE_RELATIVE_PATH=true
# Read around where these are used for more detail.
# These flags should be available everywhere according to man knife
knife_general_flags=(--help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes)
# knife has a very special syntax, some example calls are: # knife has a very special syntax, some example calls are:
# knife status # knife status
# knife cookbook list # knife cookbook list
@ -50,14 +61,10 @@
_knife() { _knife() {
local curcontext="$curcontext" state line local curcontext="$curcontext" state line
typeset -A opt_args typeset -A opt_args
# These flags should be available everywhere according to man knife
knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes --environment )
cloudproviders=(bluebox ec2 rackspace slicehost terremark) cloudproviders=(bluebox ec2 rackspace slicehost terremark)
_arguments \ _arguments \
'1: :->knifecmd'\ '1: :->knifecmd' \
'2: :->knifesubcmd'\ '2: :->knifesubcmd' \
'3: :->knifesubcmd2' \ '3: :->knifesubcmd2' \
'4: :->knifesubcmd3' \ '4: :->knifesubcmd3' \
'5: :->knifesubcmd4' \ '5: :->knifesubcmd4' \
@ -65,11 +72,11 @@ _knife() {
case $state in case $state in
knifecmd) knifecmd)
compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff environment user exec index node recipe role search ssh status windows $cloudproviders 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
;; ;;
knifesubcmd) knifesubcmd)
case $words[2] in case $words[2] in
(bluebox|ec2|rackspace|slicehost|terremark) bluebox|ec2|rackspace|slicehost|terremark)
compadd "$@" server images compadd "$@" server images
;; ;;
client) client)
@ -82,13 +89,10 @@ _knife() {
compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
;; ;;
diff) diff)
_arguments '*:file or directory:_files -g "*.(rb|json)"' _arguments '*:file or directory:_files -g "*"'
;; ;;
environment) environment)
compadd -Q "$@" create delete edit "from file" list show compadd -Q "$@" list create delete edit show "from file"
;;
user)
compadd -Q "$@" create delete edit list reregister show
;; ;;
node) node)
compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete" compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
@ -99,11 +103,21 @@ _knife() {
role) role)
compadd -Q "$@" "bulk delete" create delete edit "from file" list show compadd -Q "$@" "bulk delete" create delete edit "from file" list show
;; ;;
solo)
compadd "$@" bootstrap clean cook init prepare
;;
upload)
_arguments '*:file or directory:_files -g "*"'
;;
vault)
compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update
;;
windows) windows)
compadd "$@" bootstrap compadd "$@" bootstrap
;; ;;
*) *)
_arguments '2:Subsubcommands:($(_knife_options1))' _arguments '2:Subsubcommands:($(_knife_options1))'
;;
esac esac
;; ;;
knifesubcmd2) knifesubcmd2)
@ -117,10 +131,10 @@ _knife() {
site) site)
compadd "$@" vendor show share search download list unshare compadd "$@" vendor show share search download list unshare
;; ;;
(show|delete|edit) show|delete|edit)
_arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))' _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
;; ;;
(upload|test) upload|test)
_arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)' _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
;; ;;
list) list)
@ -129,8 +143,15 @@ _knife() {
bag) bag)
compadd -Q "$@" show edit list "from file" create delete compadd -Q "$@" show edit list "from file" create delete
;; ;;
bootstrap|clean|cook|prepare)
compadd "$@" nodes/*.json(N:t:r)
;;
init)
compadd "$@" ./*(/N:t)
;;
*) *)
_arguments '3:Subsubcommands:($(_knife_options2))' _arguments '3:Subsubcommands:($(_knife_options2))'
;;
esac esac
;; ;;
knifesubcmd3) knifesubcmd3)
@ -141,85 +162,119 @@ _knife() {
versioncomp=1 versioncomp=1
_arguments '4:Cookbookversions:($(_cookbook_versions) latest)' _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
;; ;;
(node|client|role|environment) node|client|role)
compadd "$@" --attribute compadd "$@" --attribute
;;
esac esac
;;
esac esac
case $words[4] in case $words[4] in
(show|edit) show|edit)
_arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))' _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
;; ;;
file) file)
_arguments '*:file or directory:_files -g "*.(rb|json)"' case $words[2] in
environment)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
;;
node)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
;;
role)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
;;
*)
_arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac
;; ;;
list) list)
compadd -a "$@" knife_general_flags compadd -a "$@" knife_general_flags
;; ;;
*) *)
_arguments '*:Subsubcommands:($(_knife_options3))' _arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac esac
;; ;;
knifesubcmd4) knifesubcmd4)
if (( versioncomp > 0 )); then if ((versioncomp > 0)); then
compadd "$@" attributes definitions files libraries providers recipes resources templates compadd "$@" attributes definitions files libraries providers recipes resources templates
else else
_arguments '*:Subsubcommands:($(_knife_options2))' case $words[5] in
file)
_arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
;;
*) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
esac
fi fi
;; ;;
knifesubcmd5) knifesubcmd5)
case $words[5] in
file)
_arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
;;
*)
_arguments '*:Subsubcommands:($(_knife_options3))' _arguments '*:Subsubcommands:($(_knife_options3))'
;;
esac
;;
esac esac
} }
# Helper functions to provide the argument completion for several depths of commands # Helper functions to provide the argument completion for several depths of commands
_knife_options1() { _knife_options1() {
( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done ) (for line in $(knife $words[2] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
} }
_knife_options2() { _knife_options2() {
( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done ) (for line in $(knife $words[2] $words[3] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
} }
_knife_options3() { _knife_options3() {
( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done ) (for line in $(knife $words[2] $words[3] $words[4] --help | grep -v "^knife"); do echo $line | grep "\-\-"; done)
} }
# The chef_x_remote functions use knife to get a list of objects of type x on the server # The chef_x_remote functions use knife to get a list of objects of type x on the server
_chef_roles_remote() { _chef_roles_remote() {
(knife role list | awk '{print $1}') (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_clients_remote() { _chef_clients_remote() {
(knife client list | awk '{print $1}') (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_nodes_remote() { _chef_nodes_remote() {
(knife node list | awk '{print $1}') (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_cookbooks_remote() { _chef_cookbooks_remote() {
(knife cookbook list | awk '{print $1}') (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_sitecookbooks_remote() { _chef_sitecookbooks_remote() {
(knife cookbook site list | awk '{print $1}') (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_data_bags_remote() { _chef_data_bags_remote() {
(knife data bag list | awk '{print $1}') (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_environments_remote() { _chef_environments_remote() {
(knife environment list | awk '{print $1}') (knife environment list | awk '{print $1}')
} }
_chef_users_remote() {
(knife user list | awk '{print $1}')
}
# The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server # 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() { _chef_cookbooks_local() {
(for i in $( grep cookbook_path $HOME/.chef/knife.rb | awk 'BEGIN {FS = "[" }; {print $2}' | sed 's/\,//g' | sed "s/'//g" | sed 's/\(.*\)]/\1/' | cut -d '"' -f2 ); do ls $i; done) if [ $KNIFE_RELATIVE_PATH ]; then
local cookbook_path="$(_chef_root)/cookbooks"
else
local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
if [ -f ./.chef/knife.rb ]; then
knife_rb="./.chef/knife.rb"
fi
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/')}
fi
(for i in $cookbook_path; do ls $i; done)
} }
# This function extracts the available cookbook versions on the chef server # This function extracts the available cookbook versions on the chef server
@ -227,6 +282,16 @@ _cookbook_versions() {
(knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g') (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
} }
# 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() {
directory="$PWD"
while [ $directory != '/' ]; do
test -e "$directory/.chef" && echo "$directory" && return
directory="${directory:h}"
done
}
_knife "$@" _knife "$@"
# Local Variables: # Local Variables: