3ce06883a700ff03d4db6307ae807a430d42e585

Author
Pavle Portic <git@theedgeofrage.com>
Committer
Pavle Portic <git@theedgeofrage.com>
Date

Message

Add kc script for kubectl context switching

Diff

This diff is truncated to protect this page.

  1diff --git a/dot_local/bin/executable_kc b/dot_local/bin/executable_kc
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..7757ed4be104a0e85dfda2bf4c07489cc47140b7
  4--- /dev/null
  5+++ b/dot_local/bin/executable_kc
  6@@ -0,0 +1,128 @@
  7+#!/usr/bin/env bash
  8+
  9+[[ -n $DEBUG ]] && set -x
 10+
 11+set -eou pipefail
 12+IFS=$'\n\t'
 13+
 14+
 15+usage() {
 16+  local SELF
 17+  SELF="kc"
 18+
 19+  cat <<EOF
 20+USAGE:
 21+  $SELF                       : list the configs
 22+  $SELF <NAME>                : switch to context <NAME>
 23+  $SELF -c, --current         : show the current context name
 24+  $SELF -u, --unset           : unset the current context, better use: Ctrl+d
 25+
 26+  $SELF -h,--help             : show this message
 27+EOF
 28+}
 29+
 30+exit_err() {
 31+   echo >&2 "${1}"
 32+   exit 1
 33+}
 34+
 35+current_context() {
 36+  kubectl config view -o=jsonpath='{.current-context}'
 37+}
 38+
 39+get_configs() {
 40+  #kubectl config get-contexts -o=name | sort -n
 41+  ls -p --color=never -1 $HOME/.kube | grep -v '/$'
 42+}
 43+
 44+list_configs() {
 45+  set -u pipefail
 46+  local cur ctx_list
 47+  cur="$(current_context)" || exit_err "error getting current context"
 48+  ctx_list=$(get_configs) || exit_err "error getting context list"
 49+
 50+  local yellow darkbg normal
 51+  yellow=$(tput setaf 3 || true)
 52+  darkbg=$(tput setab 0 || true)
 53+  normal=$(tput sgr0 || true)
 54+
 55+  local cur_ctx_fg cur_ctx_bg
 56+  cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
 57+  cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
 58+
 59+  for c in $ctx_list; do
 60+  if [[ -n "${_KUBECTX_FORCE_COLOR:-}" || \
 61+       -t 1 && -z "${NO_COLOR:-}" ]]; then
 62+    # colored output mode
 63+    if [[ "${c}" = "${cur}" ]]; then
 64+      echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
 65+    else
 66+      echo "${c}"
 67+    fi
 68+  else
 69+    echo "${c}"
 70+  fi
 71+  done
 72+}
 73+
 74+choose_context_interactive() {
 75+  local choice
 76+  choice="$(get_configs | _KUBECTX_FORCE_COLOR=1 fzf --ansi --no-preview || true)"
 77+  if [[ -z "${choice}" ]]; then
 78+    echo 2>&1 "error: you did not choose any of the options"
 79+    exit 1
 80+  else
 81+    set_context "${choice}"
 82+  fi
 83+}
 84+
 85+set_context() {
 86+  echo "Set context to ${1}." >&2
 87+  export KUBECONFIG="$HOME/.kube/${1}"
 88+  echo -e "\n\nYou are in a subshell. Press Ctrl+d for exit!\n\n"
 89+  $SHELL
 90+}
 91+
 92+unset_context() {
 93+  echo "Unsetting current context." >&2
 94+  kubectl config unset current-context
 95+  echo "Ctrl+d for exiting subshell." >&2
 96+}
 97+
 98+main() {
 99+  if [[ "$#" -eq 0 ]]; then
100+    if [[ -t 1 &&  -z "${KUBECTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
101+      choose_context_interactive
102+    else
103+      list_configs
104+    fi
105+  elif [[ "$#" -gt 1 ]]; then