Skip to content

Instantly share code, notes, and snippets.

@sergkondr
Last active July 28, 2023 16:04
Show Gist options
  • Save sergkondr/d3e3e05e1774d83ab26ba5853c5ecdfb to your computer and use it in GitHub Desktop.
Save sergkondr/d3e3e05e1774d83ab26ba5853c5ecdfb to your computer and use it in GitHub Desktop.
k8s-handle autocompletion for oh-my-zsh
#compdef k8s-handle
# Installation:
# - Put this file to $ZSH/plugins/k8s-handle/_k8s-handle
# - Add `k8s-handle` to the `plugins` section in the zshrc config file(usually ~/.zshrc)
local -a _k8s_handle_cmds opt_args
_k8s_handle_cmds=(
'apply:Do attempt to deploy K8S resource from the existing spec'
'destroy:Do attempt to destroy K8S resources of the selected section'
'delete:Do attempt to destroy K8S resource from the existing spec'
'render:Make resources from the template and config. Created resources will be placed into the TEMP_DIR'
'diff:Show diff between current rendered yamls and apiserver yamls'
'deploy:Do attempt to create specs from templates and deploy K8S resources of the selected section'
)
__target_args=(
{-s,--section}'[Section to deploy from config file]:source:__section_list'
{-c,--config}'[Config file, default: config.yaml]:configfile:_files -g "*.yaml"'
'--tags[Only use templates tagged with these values]'
'--skip-tags[Only use templates whose tags do not match these values]'
)
__resource_args=(
{-r,--resource}'[Resource spec path, absolute (started with slash) or relative from TEMP_DIR]'
)
__provisioning_args=(
'--sync-mode[Turn on sync mode and wait deployment ending, default: false]'
'--tries[Count of tries to check deployment status, default: 360]'
'--retry-delay[Sleep between tries in seconds, default: 5]'
'--strict[Check existence of all env variables in config.yaml and stop if var is not set]'
'--use-kubeconfig[Try to use kube config]'
'--k8s-handle-debug[Show K8S client debug messages]'
'--k8s-master-uri[K8S master to connect to]'
'--k8s-ca-base64[base64-encoded K8S certificate authority]'
'--k8s-token[K8S token to use]'
)
__logs_args=(
'--show-logs[Show logs for jobs, default: false]'
'--tail-lines[Lines of recent log file to display]'
)
__section_list() {
if [ $opt_args[--config] ]; then
config_file=$opt_args[--config]
elif [ $opt_args[-c] ]; then
config_file=$opt_args[-c]
else
config_file="config.yaml"
fi
compadd $(cat $config_file | grep -Ev "^ |^$|^#|common" | awk -F ":" '{ print $1 }')
}
_arguments '*:: :->command'
if (( CURRENT == 1 )); then
_describe -t commands "k8s-handle command" _k8s_handle_cmds
return
fi
local -a _command_args
case "$words[1]" in
apply)
_arguments $__provisioning_args $__resource_args $__logs_args ;;
destroy)
_arguments $__provisioning_args $__target_args $__logs_args ;;
delete)
_arguments $__provisioning_args $__resource_args ;;
render)
_arguments $__target_args ;;
diff)
_arguments $__target_args ;;
deploy)
_arguments $__provisioning_args $__target_args $__logs_args ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment