update kcontext autocomplete

This commit is contained in:
斟酌 鵬兄 2023-10-13 20:15:41 +08:00
parent 811b56705c
commit 6d1b390256
2 changed files with 110 additions and 82 deletions

View File

@ -24,14 +24,14 @@ complete -W "add del get list query update search secret" kstore
function kstore {
case $1 in
add) shift; kstore-add "$@" ;;
del) shift; kstore-del "$@" ;;
get) shift; kstore-get "$@" ;;
list) shift; kstore-list "$@" ;;
query) shift; kstore-query "$@" ;;
update) shift; kstore-update "$@" ;;
search) shift; kstore-search $@ ;;
secret) shift; kstore-secret $@ ;;
add) shift; _kstoreadd "$@" ;;
del) shift; _kstoredel "$@" ;;
get) shift; _kstoreget "$@" ;;
list) shift; _kstorelist "$@" ;;
query) shift; _kstorequery "$@" ;;
update) shift; _kstoreupdate "$@" ;;
search) shift; _kstoresearch $@ ;;
secret) shift; _kstoresecret $@ ;;
*)
__func_head "add [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
__func_help "update [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
@ -48,11 +48,11 @@ function kstore {
return $?
}
function kstore-secret {
function _kstoresecret {
case "$1" in
clear) shift; kstore-secret-clear "$@" ;;
config) shift; kstore-secret-config "$@" ;;
change) shift; kstore-secret-change "$@" ;;
clear) shift; _kstoresecret-clear "$@" ;;
config) shift; _kstoresecret-config "$@" ;;
change) shift; _kstoresecret-change "$@" ;;
*)
__func_head "clear"
__func_help "config"
@ -61,7 +61,7 @@ function kstore-secret {
esac
}
function kstore-init {
function _kstoreinit {
if [ ! -f "$_AUTH_DB" ]; then
cat <<___SQL___ | $_SQLITE "$_AUTH_DB"
CREATE TABLE IF NOT EXISTS store (
@ -76,11 +76,11 @@ ___SQL___
kstore secret config
}
function kstore-quote {
function _kstorequote {
echo -n "$1" | sed -e "s/'/''/g"
}
function kstore-enc {
function _kstoreenc {
if [ -z "$_AUTH_SECRET" ]; then
echo "Secret key is not set yet" >&2
return 1
@ -95,7 +95,7 @@ function kstore-enc {
fi
}
function kstore-dec {
function _kstoredec {
if [ -z "$_AUTH_SECRET" ]; then
echo "Secret key is not set yet" >&2
return 1
@ -104,22 +104,22 @@ function kstore-dec {
openssl enc -d -aes-256-cbc -pbkdf2 -k "$_AUTH_SECRET" -a -A
}
function kstore-secret-auto {
function _kstoresecret-auto {
case $OSTYPE in
darwin*) kstore-secret-macos "$@" ;;
cygwin) kstore-secret-cygwin "$@" ;;
darwin*) _kstoresecret-macos "$@" ;;
cygwin) _kstoresecret-cygwin "$@" ;;
*)
echo "$OSTYPE is Not supported yet" >&2
;;
esac
}
function kstore-secret-cygwin {
function _kstoresecret-cygwin {
local _A
which kstorecred 2>/dev/null > /dev/null
if [ $? -ne 0 ]; then
kstore-dl-kstorecred
_kstoredl-kstorecred
fi
case $1 in
@ -143,7 +143,7 @@ function kstore-secret-cygwin {
esac
}
function kstore-secret-macos {
function _kstoresecret-macos {
local _A
case $1 in
get)
@ -166,11 +166,11 @@ function kstore-secret-macos {
esac
}
function kstore-secret-config {
function _kstoresecret-config {
local _CONFIRM
if [ -z "$_AUTH_SECRET" ]; then
kstore-secret-auto get
_kstoresecret-auto get
if [ -n "$_AUTH_SECRET" ]; then
return 0
fi
@ -187,12 +187,12 @@ function kstore-secret-config {
return 0
fi
kstore-secret-auto set
_kstoresecret-auto set
fi
}
function kstore-secret-change {
kstore-secret-config
function _kstoresecret-change {
_kstoresecret-config
local _NEW_SECRET i _key _prop _O_SECRET _BAK
read -sp "Enter the new passphrase: " _NEW_SECRET
@ -213,9 +213,9 @@ function kstore-secret-change {
for i in `$_SQLITE -list "$_AUTH_DB" "SELECT _ROWID_ FROM store;"`; do
_AUTH_SECRET=$_O_SECRET
_key=`$_SQLITE -list "$_AUTH_DB" "SELECT key FROM store WHERE _ROWID_ = $i;"`
_key=`kstore-quote "$_key"`
_key=`_kstorequote "$_key"`
_prop=`$_SQLITE -list "$_AUTH_DB" "SELECT prop FROM store WHERE _ROWID_ = $i;"`
_prop=`kstore-quote "$_prop"`
_prop=`_kstorequote "$_prop"`
_val=`kstore get "$_key" "$_prop"`
if [ $? -ne 0 ]; then
echo "Current passphrase is incorrect?" >&2
@ -227,12 +227,12 @@ function kstore-secret-change {
kstore update "$_key" "$_val" "$_prop"
done
kstore-secret-auto get
_kstoresecret-auto get
if [ -n "$_AUTH_SECRET" ]; then
kstore-secret-auto del 2>&1 > /dev/null
_kstoresecret-auto del 2>&1 > /dev/null
_AUTH_SECRET=$_NEW_SECRET
kstore-secret-auto set
_kstoresecret-auto set
if [ $? -eq 0 ]; then
echo "New passphrase saved in OS's keystore."
@ -242,72 +242,72 @@ function kstore-secret-change {
_AUTH_SECRET=$_NEW_SECRET
}
function kstore-secret-clear {
function _kstoresecret-clear {
unset _AUTH_SECRET
kstore-secret-auto del
_kstoresecret-auto del
}
function kstore-update {
function _kstoreupdate {
if [ -z "$1" ]; then
__func_head "[key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
kstore-init || return 1
_kstoreinit || return 1
local _key _val _prop
_key=`kstore-quote "$1"`
_val=`kstore-enc "$2"`
_val=`kstore-quote "$_val"`
_prop=`kstore-quote "${3:-$_KSTORE_DEF_PROP}"`
_key=`_kstorequote "$1"`
_val=`_kstoreenc "$2"`
_val=`_kstorequote "$_val"`
_prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "UPDATE store SET data = '$_val' WHERE $_cond;"
}
function kstore-add {
function _kstoreadd {
if [ -z "$1" ]; then
__func_head "[key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
kstore-init || return 1
_kstoreinit || return 1
local _key _val _prop
_key=`kstore-quote "$1"`
_val=`kstore-enc "$2"`
_val=`kstore-quote "$_val"`
_prop=`kstore-quote "${3:-$_KSTORE_DEF_PROP}"`
_key=`_kstorequote "$1"`
_val=`_kstoreenc "$2"`
_val=`_kstorequote "$_val"`
_prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
$_SQLITE "$_AUTH_DB" \
"INSERT INTO store ( key, prop, data )
VALUES( '$_key', '$_prop', '$_val' );"
}
function kstore-get {
function _kstoreget {
if [ -z "$1" ]; then
__func_head "[key] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
kstore-init || return 1
_kstoreinit || return 1
local _key _prop _cond
_key=`kstore-quote "$1"`
_prop=`kstore-quote "${2:-$_KSTORE_DEF_PROP}"`
_key=`_kstorequote "$1"`
_prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111
if [ $? -eq 0 ]; then
$_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | kstore-dec
$_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | _kstoredec
else
echo "\"$1\" not found (prop: $_prop)" >&2
return 1
fi
}
function kstore-del {
function _kstoredel {
if [ -z "$1" ]; then
__func_head "[key] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
kstore-init || return 1
_kstoreinit || return 1
local _CONFIRM _key _prop _cond
_key=`kstore-quote "$1"`
_prop=`kstore-quote "${2:-$_KSTORE_DEF_PROP}"`
_key=`_kstorequote "$1"`
_prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111
@ -328,7 +328,7 @@ function kstore-del {
fi
}
function kstore-dl-s3au {
function _kstoredl-s3au {
local p tmp _NAME
p="https://git.k8s.astropenguin.net/penguin/s3-arch-utils/raw/branch/master"
@ -349,7 +349,7 @@ function kstore-dl-s3au {
fi
}
function kstore-dl-kstorecred {
function _kstoredl-kstorecred {
local p tmp _NAME
_NAME="kstorecred"
@ -369,29 +369,29 @@ function kstore-dl-kstorecred {
fi
}
function kstore-init-s3au {
kstore-dl-s3au "574d106cdced150fa6e04a9437d356d8688035cb2c63a045fa0d4ead8b3ec941" "arch_delete_aws4.sh" || return 1
kstore-dl-s3au "ce04b3f90b7d9a2578587c8ff841186810d977545943fb4a8234a6d6e9f7b568" "arch_delete_many_aws4.sh" || return 1
kstore-dl-s3au "31709a639fab27b5ba071dd4c843fd3e90d4cea07bbb055c08c9720d31a8d11d" "arch_download_aws4.sh" || return 1
kstore-dl-s3au "0bee6f925a41f496f66654062b49ea2a1cc55d877f4617d56bf91244aaf0be51" "arch_getacl_aws4.sh" || return 1
kstore-dl-s3au "dcb89894e78351af702fabb521cf1cf6ca512db9cd4cf3c70631bb1e51ab9340" "arch_list_aws4.sh" || return 1
kstore-dl-s3au "759ef3841525ce240d00e03578c39533c30bf246d61c12c2f3ba2ab37c5f814e" "arch_upload_aws4.sh" || return 1
function _kstoreinit-s3au {
_kstoredl-s3au "574d106cdced150fa6e04a9437d356d8688035cb2c63a045fa0d4ead8b3ec941" "arch_delete_aws4.sh" || return 1
_kstoredl-s3au "ce04b3f90b7d9a2578587c8ff841186810d977545943fb4a8234a6d6e9f7b568" "arch_delete_many_aws4.sh" || return 1
_kstoredl-s3au "31709a639fab27b5ba071dd4c843fd3e90d4cea07bbb055c08c9720d31a8d11d" "arch_download_aws4.sh" || return 1
_kstoredl-s3au "0bee6f925a41f496f66654062b49ea2a1cc55d877f4617d56bf91244aaf0be51" "arch_getacl_aws4.sh" || return 1
_kstoredl-s3au "dcb89894e78351af702fabb521cf1cf6ca512db9cd4cf3c70631bb1e51ab9340" "arch_list_aws4.sh" || return 1
_kstoredl-s3au "759ef3841525ce240d00e03578c39533c30bf246d61c12c2f3ba2ab37c5f814e" "arch_upload_aws4.sh" || return 1
}
function kstore-upload-db {
function _kstoreupload-db {
local _T _W
kstore-init || return 1
_kstoreinit || return 1
_T=$( date +%Y%m%d%H%M%S )
echo $_T > "$RBASH_HOME/keystore.latest"
which arch-upload-aws4 2>&1 > /dev/null
if [ $? -ne 0 ]; then
kstore-init-s3au
_kstoreinit-s3au
fi
kstore-enc $_AUTH_DB > "$RBASH_HOME/$_T.enc"
_kstoreenc $_AUTH_DB > "$RBASH_HOME/$_T.enc"
arch-upload-aws4 "keystore/$_T.enc" "$RBASH_HOME/$_T.enc"
arch-upload-aws4 "keystore/latest" "$RBASH_HOME/keystore.latest"
@ -399,7 +399,7 @@ function kstore-upload-db {
rm "$RBASH_HOME/keystore.latest"
}
function kstore-download-db {
function _kstoredownload-db {
local _T _DOMAIN _URL _TMP _CONFIRM
kstore secret config || return 1
@ -411,7 +411,7 @@ function kstore-download-db {
fi
_TMP=$( mktemp )
__download "https://$_DOMAIN/keystore/$_T.enc" | kstore-dec > $_TMP
__download "https://$_DOMAIN/keystore/$_T.enc" | _kstoredec > $_TMP
if [ $? -ne 0 ]; then
return 1
fi
@ -437,19 +437,19 @@ function kstore-download-db {
fi
}
function kstore-search {
function _kstoresearch {
local _termk _termp _cond
_termk=`kstore-quote "$1"`
_termp=`kstore-quote "$2"`
_termk=`_kstorequote "$1"`
_termp=`_kstorequote "$2"`
_cond="key LIKE '%$_termk%' AND prop LIKE '%$_termp%'"
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store WHERE $_cond;"
}
function kstore-list {
function _kstorelist {
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store;"
}
function kstore-query {
function _kstorequery {
$_SQLITE "$_AUTH_DB" "$@"
}

View File

@ -6,13 +6,11 @@ if [ $? -ne 0 ]; then
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 "$@" ;;
k8s) shift; _kcontext-k8s "$@" ;;
docker) shift; _kcontext-docker "$@" ;;
s3-arch) shift; _kcontext-s3-arch "$@" ;;
*)
__func_head "k8s ..."
__func_help "s3-arch ..."
@ -23,7 +21,37 @@ function kcontext {
return $?
}
function kcontext-k8s {
function _kcontext {
local CUR=${COMP_WORDS[COMP_CWORD]}
local SCOPE=${COMP_WORDS[COMP_CWORD-1]}
local t
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)
@ -107,7 +135,7 @@ function kcontext-k8s {
return 1
}
function kcontext-s3-arch {
function _kcontext-s3-arch {
local _NAME _CONFIRM _URL _AUTH
case $1 in
list)