Introduction
Almost everything tech related I do on my computers revolves around my terminal. So naturally I have spent quite some time setting up my terminal the way I want it to with auto-completing, aliases and what not to optimize my workflow. And I must admit, it never stops evolving. Which is one of the beautiful things with having your own personalized CLI environment.
Why workflow matters
There are alot of tools, products, APIs, IDEs, you name it, I as a cloud engineer come across. A workflow is something personal which you both grow into and enhance with the experience you gain in your technical adventures.
Some people like the GUI, some like CLI tools, some do a mix. I love seeing how other people have customized their work environment to optimize the way they are working. My preference lies between GUI and Terminals (TUIs to the rescue!). Although not a TUI in the classic sense, I use fzf
and jq
alot to convert my terminal experience into a more graphical one (if that makes sense).
My work in general revolves heavily around Kubernetes and Azure nowadays and I find it tedious to nagivate between subscriptions, namespaces and clusters because it takes away valuable time and made me have to context switch to write long CLI commands. This is however no hard problem to fix and well worth the (small) investment. Although there are plugins and extentions for kubectl
for this, I love the simplicity of not installing too much stuff. Not to mention that I can apply the same practices to extend various tools, not just limited to kubectl
.
I have created three functions in my .zshrc
which solved that particular problem for me. They allow me to navigate between the aforementioned systems/components through a UI without leaving my terminal (the perfect mix). I wanted to share them in this post as a reminder to myself but also for the sake of sharing.
Pre-requisites
To make the following functions work I had to install the following packages
fzf
jq
Now to the functions as hand.
1. swctx
function swctx {
kubectl config get-contexts -o name | fzf | xargs -I {} bash -c 'kubectl config use-context {}'
}
This function allows to be quickly navigate and switch kubeconfig contexts between different clusters. It works by listing existing contexts and piping the outcome to fzf, which allows me to fuzzy search. The outcome from fzf is then piped to kubectl again to set the context to a new cluster.
2. swns
function swns {
kubectl get namespace -o name | awk -F "/" '{print $2}'| fzf | xargs -I {} bash -c 'kubectl config set-context --current --namespace {}'
}
This function allows to be quickly navigate and switch between different k8s namespaces. By piping all existing namespaces in the current context to fzf, I can fuzzy search or orient myself to a new namespace with the arrow keys. Once a new namespace is marked it will modify the current kubeconfig context.
3. accset
function accset {
az account list -o json | jq -r '.[].name' | fzf | xargs -I {} bash -c 'az account set -s "{}"; az account show'
}
Similar to the ones above but for Azure subscriptions instead.
Update (2024-11-24)
I recently added a couple of new functions. One for starting a dockerized azurite process in a new tmux tab
function start_azurite {
tmux new-window -n azurite 'docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 --rm mcr.microsoft.com/azure-storage/azurite & &>/dev/null'
}
And another for quickly searching for Azure roles with fuzzy finding.
function findrole {
az role definition list -ojson --query "[].{roleName:roleName}" | jq -r '.[].roleName' | fzf | xargs -I {} bash -c 'az role definition list -n "{}"'
}
That is it,
Happy coding!