aboutsummaryrefslogtreecommitdiff
path: root/kontext.in
blob: a16798a117a2798875777f64cd8c9900a2c8a8f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!@ENV_ABSOLUTE_PATH@ bash

[[ "${BASH_SOURCE[0]}" != "${0}" ]] || { >&2 cat <<EOF
Warning: kontext is not being sourced, add this alias to your shell:
  alias kontext="source kontext"
EOF
exit 1; }

kontext_func () {
  local TEMP
  TEMP=$(@GNU_GETOPT@ -o hn: --long help,namespace: -n 'kontext' -- "$@")
  eval set -- "$TEMP"

  local HELP=false
  local NAMESPACE=""
  local CTX=""

  while true; do
      case "$1" in
          -h | --help ) HELP=true; shift ;;
          -n | --namespace ) NAMESPACE="$2"; shift 2 ;;
          -- ) CTX="$2"; shift; break ;;
          * ) break ;;
      esac
  done

  if [ -z "$CTX" ] && [ -n "$NAMESPACE" ]; then
    CTX=$(kubectl config current-context)
  fi

  if [ -z "$CTX" ] || [ "$HELP" = true ]; then
    cat <<-EOF
Usage:
  kontext [-h|--help]"
    Show this text.
  kontext [-n|--namespace <namespace>] [<context>]"
    Switch to the given namespace and context.

More information may be found in the kontext(1) man page.
EOF
    return 1
  else
    TMP_KUBECONFIG=$(mktemp -t kubeconfig-XXXXXX)
    kubectl config view --raw > "$TMP_KUBECONFIG"
    export KUBECONFIG="$TMP_KUBECONFIG"
    kubectl config use-context "$CTX" && \
    kubectl config set-context --current --namespace="$NAMESPACE"
  fi
}

kontext_func "$@"