#!/bin/bash declare -f kstore > /dev/null if [ $? -ne 0 ]; then echo "kcontext depends on kstore" return 1 fi function kcontext { case $1 in k8s) shift; _kcontext-k8s "$@" ;; docker) shift; _kcontext-docker "$@" ;; s3-arch) shift; _kcontext-s3-arch "$@" ;; *) __func_head "k8s ..." __func_help "s3-arch ..." __func_help "docker ..." return 1 ;; esac return $? } function _kcontext { local CUR=${COMP_WORDS[COMP_CWORD]} local SCOPE=${COMP_WORDS[COMP_CWORD-1]} COMPREPLY=() case "$SCOPE" in kcontext) COMPREPLY=( $(compgen -W "k8s docker s3-arch" -- $CUR) ) ;; k8s|s3-arch) COMPREPLY=( $(compgen -W "use del list save" -- $CUR) ) return ;; use|del) SCOPE=${COMP_WORDS[COMP_CWORD-2]} ;; esac case "$SCOPE" in k8s|s3-arch) COMPREPLY=( $(compgen -W "$( kcontext $SCOPE list )" -- $CUR) ) ;; esac } complete -F _kcontext kcontext function _kcontext-k8s { local _NAME _CONF case $1 in list) kstore query -list "SELECT SUBSTR( prop, 5 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 'k8s.%'" return $? ;; save) _NAME=$( kubectl config current-context ) if [ $? -ne 0 ]; then return 1 fi kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kubectl config view --minify --flatten | kstore update "kcontext" - "k8s.$_NAME" return $? else kubectl config view --minify --flatten | kstore add "kcontext" - "k8s.$_NAME" return $? fi ;; use) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi _CONF="$RBASH_HOME/k8s.conf" kstore get "kcontext" "k8s.$_NAME" > "$_CONF" if [ $? -ne 0 ]; then echo "No such context: $_NAME" >&2 return 1 fi chmod 600 "$_CONF" echo $KUBECONFIG | grep "$_CONF" -q case $OSTYPE in cygwin) _CONF=$( cygpath -w "$_CONF" ) ;; esac if [ $? -eq 0 ]; then export KUBECONFIG="$_CONF" else export KUBECONFIG="$_CONF:$KUBECONFIG" fi ;; del) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'" return $? else echo "No such context: $_NAME" >&2 fi ;; *) __func_head "list" __func_help "save" __func_help "use NAME" __func_help "del NAME" ;; esac return 1 } function _kcontext-s3-arch { local _NAME _CONFIRM _URL _AUTH case $1 in list) kstore query -list "SELECT SUBSTR( prop, 16 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'" return $? ;; save) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi if [ -n "$ARCH_S3_BUCKET_URL" ] && [ -n "$ARCH_S3_AUTH" ]; then kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kstore update "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME" kstore update "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME" return $? else kstore add "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME" kstore add "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME" return $? fi else echo "No s3-arch settings can be found in env" >&2 fi ;; use) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi _URL=$( kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null ) if [ $? -ne 0 ]; then echo "No such context: $_NAME" >&2 return 1 fi _AUTH=$( kstore get "kcontext" "s3-arch.auth.$_NAME" 2> /dev/null ) if [ $? -ne 0 ]; then echo "No such context: $_NAME" >&2 return 1 fi export ARCH_S3_BUCKET_URL=$_URL export ARCH_S3_AUTH=$_AUTH ;; del) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'" kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'" return $? else echo "No such context: $_NAME" >&2 fi ;; *) __func_head "list" __func_help "save NAME" __func_help "use NAME" __func_help "del NAME" ;; esac return 1 }