Skip to content

Instantly share code, notes, and snippets.

@shouya
Last active June 3, 2021 08:54
Show Gist options
  • Select an option

  • Save shouya/ef46374d4987caae8b703df38efbc5d6 to your computer and use it in GitHub Desktop.

Select an option

Save shouya/ef46374d4987caae8b703df38efbc5d6 to your computer and use it in GitHub Desktop.
Kubectl explain with syntax highlighting and cache
#!/bin/bash
KUBE_EXPLAIN_CACHE_DIR=$HOME/.kube/cache/explain
p="$1"
f="$KUBE_EXPLAIN_CACHE_DIR/$p"
if [[ -z "$p" ]]; then
echo "Usage: $0 <path>"
echo "Example: $0 deploy.spec.template.spec.volumes"
exit
fi
[[ -d "$KUBE_EXPLAIN_CACHE_DIR" ]] || mkdir -p "$KUBE_EXPLAIN_CACHE_DIR"
if [[ -f "$f.err" ]]; then
cat -- "$f.err"
exit
elif [[ -f "$f" ]]; then
nvim -c 'set ft=man' -- "$f"
exit
fi
kubectl explain "$p" > "$f" 2>&1
errcode="$?"
if [[ "$errcode" != 0 ]]; then
mv -- "$f" "$f.err"
cat -- "$f.err"
exit "$errcode"
else
nvim -c 'set ft=man' -- "$f"
fi
@shouya
Copy link
Author

shouya commented Jun 3, 2021

Installation:

  1. Ensure you have neovim installed.
  2. Put this file somewhere under your $PATH.
  3. chmod +x /path/to/kubectl-exp.

Usage:

Whenever you type kubectl explain <SOMETHING>, type kubectl exp <SOMETHING> instead.

Screenshot:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment