17_kcontext draft

This commit is contained in:
斟酌 鵬兄 2023-09-23 23:11:09 +08:00
parent 3395589b9e
commit b134d89d40
2 changed files with 109 additions and 1 deletions

View File

@ -20,6 +20,8 @@ if [ -f "$RBASH_HOME/keystore.secret" ]; then
_AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" )
fi
complete -W "add del get list query update search secret sync" kstore
function kstore {
case $1 in
add) shift; kstore-add "$@" ;;
@ -260,6 +262,7 @@ function kstore-get {
$_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | kstore-dec
else
echo "\"$1\" not found" >&2
return 1
fi
}
@ -330,5 +333,5 @@ function kstore-list {
}
function kstore-query {
sqlite3 "$_AUTH_DB" "$@"
$_SQLITE "$_AUTH_DB" "$@"
}

105
bash/sources/17_kcontext Normal file
View File

@ -0,0 +1,105 @@
#!/bin/bash
declare -f kstore > /dev/null
if [ $? -ne 0 ]; then
echo "kcontext depends on kstore"
return 1
fi
complete -W "k8s docker s3-arch" kcontext
function kcontext {
case $1 in
k8s) shift; kcontext-k8s "$@" ;;
docker) shift; kcontext-docker "$@" ;;
s3-arch) shift; kcontext-s3-arch "$@" ;;
*)
__func_help "k8s ..."
__func_head "s3-arch ..."
__func_help "docker ..."
return 1
;;
esac
return $?
}
function kcontext-s3-arch {
local _NAME _CONFIRM _URL _AUTH
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 16 ) FROM store WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'"
;;
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
;;
load)
_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 store WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'"
kstore query "DELETE FROM store 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 "load NAME"
__func_help "del NAME"
;;
esac
return 1
}