knife: add README and reformat completion file

This commit is contained in:
Marc Cornellà 2019-12-27 00:49:50 +01:00
parent a952854c12
commit 132607447d
2 changed files with 136 additions and 104 deletions

25
plugins/knife/README.md Normal file
View file

@ -0,0 +1,25 @@
# knife plugin
This plugin adds completion for [knife](https://docs.chef.io/knife.html), a command-line tool
to interact with [Chef](https://chef.io), a platform to automate and manage infrastructure via
code.
To use it, add `knife` to the plugins array in your zshrc file:
```zsh
plugins=(... knife)
```
## Options
- `KNIFE_RELATIVE_PATH`: if set to `true`, the completion script will look for local cookbooks
under the `cookbooks` folder in the chef root directory. It has preference over the other two
options below. **Default:** empty.
- `KNIFE_COOKBOOK_PATH`: if set, it points to the folder that contains local cookbooks, for
example: `/path/to/my/chef/cookbooks`. **Default:** `cookbook_path` field in `knife.rb`
(see below).
- `KNIFE_CONF_PATH`: variable pointing to the `knife.rb` configuration file, for example
`/path/to/my/.chef/knife.rb`. Only used if `$KNIFE_COOKBOOK_PATH` isn't set. If it exists,
`$PWD/.chef/knife.rb` is used instead. Otherwise, if it's set, its value is used.
**Default**: `$HOME/.chef/knife.rb`.

View file

@ -3,13 +3,13 @@
# You can override the path to knife.rb and your cookbooks by setting # You can override the path to knife.rb and your cookbooks by setting
# KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb # KNIFE_CONF_PATH=/path/to/my/.chef/knife.rb
# KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks # KNIFE_COOKBOOK_PATH=/path/to/my/chef/cookbooks
# If you want your local cookbooks path to be calculated relative to where you are then # If you want your local cookbooks path to be calculated relative to where you are then
# set the below option # set the below option
# KNIFE_RELATIVE_PATH=true # KNIFE_RELATIVE_PATH=true
# Read around where these are used for more detail. # Read around where these are used for more detail.
# These flags should be available everywhere according to man knife # 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_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
@ -25,183 +25,191 @@ _knife() {
typeset -A opt_args typeset -A opt_args
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' \
'6: :->knifesubcmd5' '6: :->knifesubcmd5'
case $state in case $state in
knifecmd) knifecmd)
compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff exec environment index node recipe role search ssh status upload vault windows $cloudproviders compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" diff exec environment index node recipe role search 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)
compadd -Q "$@" "bulk delete" list create show delete edit reregister compadd -Q "$@" "bulk delete" list create show delete edit reregister
;; ;;
configure) configure)
compadd "$@" client compadd "$@" client
;; ;;
cookbook) cookbook)
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 "*"' _arguments '*:file or directory:_files -g "*"'
;; ;;
environment) environment)
compadd -Q "$@" list create delete edit show "from file" compadd -Q "$@" list create delete edit show "from file"
;; ;;
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"
;; ;;
recipe) recipe)
compadd "$@" list compadd "$@" list
;; ;;
role) role)
compadd -Q "$@" "bulk delete" create delete edit "from file" list show compadd -Q "$@" "bulk delete" create delete edit "from file" list show
;; ;;
upload) upload)
_arguments '*:file or directory:_files -g "*"' _arguments '*:file or directory:_files -g "*"'
;; ;;
vault) vault)
compadd -Q "$@" create decrypt delete edit remove "rotate all keys" "rotate keys" show update 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)
case $words[3] in case $words[3] in
server) server)
compadd "$@" list create delete compadd "$@" list create delete
;; ;;
images) images)
compadd "$@" list compadd "$@" list
;; ;;
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)
compadd -a "$@" knife_general_flags compadd -a "$@" knife_general_flags
;; ;;
bag) bag)
compadd -Q "$@" show edit list "from file" create delete compadd -Q "$@" show edit list "from file" create delete
;; ;;
*) *)
_arguments '3:Subsubcommands:($(_knife_options2))' _arguments '3:Subsubcommands:($(_knife_options2))'
;;
esac esac
;;
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) 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)
case $words[2] in case $words[2] in
environment) environment)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"' _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/environments"'
;; ;;
node) node)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"' _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/nodes"'
;; ;;
role) role)
_arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"' _arguments '*:files:_path_files -g "*.(rb|json)" -W "$(_chef_root)/roles"'
;; ;;
*) *)
_arguments '*:Subsubcommands:($(_knife_options3))' _arguments '*:Subsubcommands:($(_knife_options3))'
esac ;;
;; esac
list) ;;
compadd -a "$@" knife_general_flags list)
;; 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
case $words[5] in case $words[5] in
file) file)
_arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ ' _arguments '*:directory:_path_files -/ -W "$(_chef_root)/data_bags" -qS \ '
;; ;;
*) _arguments '*:Subsubcommands:($(_knife_options2))' *) _arguments '*:Subsubcommands:($(_knife_options2))' ;;
esac esac
fi fi
;; ;;
knifesubcmd5) knifesubcmd5)
case $words[5] in case $words[5] in
file) file)
_arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"' _arguments '*:files:_path_files -g "*.json" -W "$(_chef_root)/data_bags/$words[6]"'
;; ;;
*) *)
_arguments '*:Subsubcommands:($(_knife_options3))' _arguments '*:Subsubcommands:($(_knife_options3))'
esac ;;
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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (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 --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}') (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
} }
_chef_environments_remote() { _chef_environments_remote() {
@ -210,14 +218,14 @@ _chef_environments_remote() {
# 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() {
if [ $KNIFE_RELATIVE_PATH ]; then if [ $KNIFE_RELATIVE_PATH ]; then
local cookbook_path="$(_chef_root)/cookbooks" local cookbook_path="$(_chef_root)/cookbooks"
else else
local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb} local knife_rb=${KNIFE_CONF_PATH:-${HOME}/.chef/knife.rb}
if [ -f ./.chef/knife.rb ]; then if [ -f ./.chef/knife.rb ]; then
knife_rb="./.chef/knife.rb" knife_rb="./.chef/knife.rb"
fi 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/' )} 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 fi
(for i in $cookbook_path; do ls $i; done) (for i in $cookbook_path; do ls $i; done)
} }
@ -227,12 +235,11 @@ _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 # Searches up from current directory to find the closest folder that has a .chef folder
# Useful for the knife upload/from file commands # Useful for the knife upload/from file commands
_chef_root () { _chef_root() {
directory="$PWD" directory="$PWD"
while [ $directory != '/' ] while [ $directory != '/' ]; do
do
test -e "$directory/.chef" && echo "$directory" && return test -e "$directory/.chef" && echo "$directory" && return
directory="${directory:h}" directory="${directory:h}"
done done