From 8ed19ab54d6980da777ccea185658e213b26f533 Mon Sep 17 00:00:00 2001 From: Shaun Tabone Date: Sun, 29 Dec 2019 05:37:38 +0100 Subject: [PATCH] Added MicroK8s plugin for ZSH (#8499) * Added MicroK8s plugin for ZSH * Added caching for kubectl and helm commands * Added title in compadd and enhanced README --- plugins/microk8s/README.md | 24 ++++++++ plugins/microk8s/microk8s.plugin.zsh | 82 ++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 plugins/microk8s/README.md create mode 100644 plugins/microk8s/microk8s.plugin.zsh diff --git a/plugins/microk8s/README.md b/plugins/microk8s/README.md new file mode 100644 index 000000000..2b4ea206f --- /dev/null +++ b/plugins/microk8s/README.md @@ -0,0 +1,24 @@ +# MicroK8s plugin + +This plugin provides completion and useful aliases for [MicroK8s](https://microk8s.io/). + +To use it, add `microk8s` to the plugins array in your zshrc file. + +```zsh +plugins=(... microk8s) +``` + +## Aliases + +| Alias | Command | Description | +|-------|------------------|----------------------------------------------------------------------------------------------------------| +| mco | microk8s.config | Shows the Kubernetes config file. | +| mct | microk8s.ctr | Interact with containerd CLI. | +| mdi | microk8s.disable | Disables an addon. | +| me | microk8s.enable | Enables an addon. | +| mh | microk8s.helm | Interact with Helm CLI. | +| mis | microk8s.istio | Interact with Istio CLI. | +| mk | microk8s.kubectl | Interact with Kubernetes CLI. | +| msp | microk8s.stop | Stops all Kubernetes services. | +| mst | microk8s.start | Starts MicroK8s after it is being stopped. | +| msts | microk8s.status | Provides an overview of the MicroK8s state (running / not running) as well as the set of enabled addons. | \ No newline at end of file diff --git a/plugins/microk8s/microk8s.plugin.zsh b/plugins/microk8s/microk8s.plugin.zsh new file mode 100644 index 000000000..048a9ab83 --- /dev/null +++ b/plugins/microk8s/microk8s.plugin.zsh @@ -0,0 +1,82 @@ +# ---------------------------------------------------------- # +# Aliases and Completions for MicroK8s (https://microk8s.io) # +# Author: Shaun Tabone (https://github.com/xontab) # +# ---------------------------------------------------------- # + +# Helper function to cache and load completions +_microk8s_cache_completion() { + local cache="${ZSH_CACHE_DIR}/microk8s_$(echo $1)_completion" + if [[ ! -f $cache ]]; then + $2 $cache + fi + + [[ -f $cache ]] && source $cache +} + +# ---------------------------------------------------------- # +# microk8s.enable # +# ALIAS: me # +# ---------------------------------------------------------- # +_microk8s_enable_get_command_list() { + microk8s.enable --help | tail -n +7 | awk '{$1=$1;print}' +} + +_microk8s_enable() { + compadd -X "MicroK8s Addons" $(_microk8s_enable_get_command_list) +} + +compdef _microk8s_enable microk8s.enable +alias me='microk8s.enable' + +# ---------------------------------------------------------- # +# microk8s.disable # +# ALIAS: mdi # +# ---------------------------------------------------------- # +_microk8s_disable_get_command_list() { + microk8s.disable --help | tail -n +7 | awk '{$1=$1;print}' +} + +_microk8s_disable() { + compadd -X "MicroK8s Addons" $(_microk8s_disable_get_command_list) +} + +compdef _microk8s_disable microk8s.disable +alias mdi='microk8s.disable' + +# ---------------------------------------------------------- # +# microk8s.kubectl # +# ALIAS: mk # +# ---------------------------------------------------------- # +_microk8s_kubectl_completion() { + if [ $commands[microk8s.kubectl] ]; then + microk8s.kubectl 2>/dev/null >/dev/null && microk8s.kubectl completion zsh | sed 's/__start_kubectl kubectl/__start_kubectl microk8s.kubectl/g' >$1 + fi +} + +_microk8s_cache_completion 'kubectl' _microk8s_kubectl_completion + +alias mk='microk8s.kubectl' + +# ---------------------------------------------------------- # +# microk8s.helm # +# ALIAS: mh # +# ---------------------------------------------------------- # +_microk8s_helm_completion() { + if [ $commands[microk8s.helm] ]; then + microk8s.helm completion zsh | sed 's/__start_helm helm/__start_helm microk8s.helm/g' >$1 + fi +} + +_microk8s_cache_completion 'helm' _microk8s_helm_completion + +alias mh='microk8s.helm' + +# ---------------------------------------------------------- # +# Other Aliases # +# ---------------------------------------------------------- # +alias mco='microk8s.config' +alias mct='microk8s.ctr' +alias mis='microk8s.istio' +alias mst='microk8s.start' +alias msts='microk8s.status' +alias msp='microk8s.stop'