commit 43d9118998bd041c740bf4baa17644be0b9d720c
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2026-04-24T09:21:57Z |
| subject | Added kcontext k8s import |
commit 43d9118998bd041c740bf4baa17644be0b9d720c
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2026-04-24T09:21:57Z
Added kcontext k8s import
---
bash/sources/17_kcontext | 86 +++++++++++++++++++++++++++++++++++++++---------
1 file changed, 71 insertions(+), 15 deletions(-)
diff --git a/bash/sources/17_kcontext b/bash/sources/17_kcontext
index 7c933fc..59c36cb 100644
--- a/bash/sources/17_kcontext
+++ b/bash/sources/17_kcontext
@@ -22,36 +22,45 @@ function kcontext {
}
function _kcontext {
- local CUR=${COMP_WORDS[COMP_CWORD]}
- local SCOPE=${COMP_WORDS[COMP_CWORD-1]}
+ local cur cmd subcmd
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ cmd="${COMP_WORDS[1]}"
+ subcmd="${COMP_WORDS[2]}"
COMPREPLY=()
- case "$SCOPE" in
- kcontext)
- COMPREPLY=( $(compgen -W "k8s docker s3-arch" -- $CUR) )
- ;;
- k8s|s3-arch)
- COMPREPLY=( $(compgen -W "use del list save" -- $CUR) )
+ case "$COMP_CWORD" in
+ 1)
+ COMPREPLY=( $(compgen -W "k8s s3-arch" -- "$cur") )
return
;;
- use|del)
- SCOPE=${COMP_WORDS[COMP_CWORD-2]}
+
+ 2)
+ case "$cmd" in
+ k8s)
+ COMPREPLY=( $(compgen -W "use del import list save" -- "$cur") )
+ return
+ ;;
+ s3-arch)
+ COMPREPLY=( $(compgen -W "use del list save" -- "$cur") )
+ return
+ ;;
+ esac
;;
esac
- case "$SCOPE" in
- k8s|s3-arch)
- COMPREPLY=( $(compgen -W "$( kcontext $SCOPE list )" -- $CUR) )
+ case "$cmd:$subcmd" in
+ k8s:use|k8s:del|s3-arch:use|s3-arch:del)
+ COMPREPLY=( $(compgen -W "$(kcontext "$cmd" list 2>/dev/null)" -- "$cur") )
+ return
;;
esac
-
}
complete -F _kcontext kcontext
function _kcontext-k8s {
- local _NAME _CONF
+ local _NAME _CONF _TMP_CONF
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 5 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 'k8s.%'"
@@ -76,6 +85,52 @@ function _kcontext-k8s {
return $?
fi
;;
+ import)
+ _NAME=$2
+ _CONF=$3
+
+ if [ -z "$_NAME" ]; then
+ echo "Please specify a context name" >&2
+ return 1
+ fi
+
+ if [ -z "$_CONF" ]; then
+ echo "Please specify the kube config path, or '-' for stdin" >&2
+ return 1
+ fi
+
+ _TMP_CONF=$(mktemp "${RBASH_HOME}/k8s.import.XXXXXX") || return 1
+ trap 'rm -f "$_TMP_CONF"' RETURN
+
+ if [ "$_CONF" = "-" ]; then
+ cat > "$_TMP_CONF"
+ elif [ -f "$_CONF" ]; then
+ cp "$_CONF" "$_TMP_CONF" || return 1
+ else
+ echo "Kube config not found: $_CONF" >&2
+ return 1
+ fi
+
+ if ! KUBECONFIG="$_TMP_CONF" kubectl config view --minify --flatten >/dev/null 2>&1; then
+ echo "Invalid kube config: $_CONF" >&2
+ return 1
+ fi
+
+ if kstore get "kcontext" "k8s.$_NAME" >/dev/null 2>&1; then
+ read -r -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
+ if [ "$_CONFIRM" != "y" ]; then
+ return 0
+ fi
+
+ KUBECONFIG="$_TMP_CONF" kubectl config view --minify --flatten \
+ | kstore update "kcontext" - "k8s.$_NAME"
+ return $?
+ fi
+
+ KUBECONFIG="$_TMP_CONF" kubectl config view --minify --flatten \
+ | kstore add "kcontext" - "k8s.$_NAME"
+ return $?
+ ;;
use)
_NAME=$2
if [ -z "$_NAME" ]; then
@@ -127,6 +182,7 @@ function _kcontext-k8s {
*)
__func_head "list"
__func_help "save"
+ __func_help "import NAME FILE"
__func_help "use NAME"
__func_help "del NAME"
;;