Removed hard-coded table name

This commit is contained in:
斟酌 鵬兄 2024-11-11 06:28:53 +08:00
parent 80ec3e8691
commit a7037ab76d
2 changed files with 22 additions and 21 deletions

View File

@ -16,6 +16,7 @@ _AUTH_DB=$RBASH_HOME/keystore.db
_AUTH_SECRET= _AUTH_SECRET=
_KSTORE_DEF_PROP=${_KSTORE_DEF_PROP:-val} _KSTORE_DEF_PROP=${_KSTORE_DEF_PROP:-val}
_KSTORE_DEC_FAILED="$RBASH_HOME/.decypt-failed" _KSTORE_DEC_FAILED="$RBASH_HOME/.decypt-failed"
_KSTORE_TABLE=store
if [ -f "$RBASH_HOME/keystore.secret" ]; then if [ -f "$RBASH_HOME/keystore.secret" ]; then
_AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" ) _AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" )
@ -61,7 +62,7 @@ function _kstore {
return return
;; ;;
get|update) get|update)
COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT key FROM store" )" -- $CUR) ) COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT key FROM $_KSTORE_TABLE" )" -- $CUR) )
;; ;;
esac esac
@ -69,7 +70,7 @@ function _kstore {
case "$t" in case "$t" in
get|update) get|update)
t=`_kstorequote "$SCOPE"` t=`_kstorequote "$SCOPE"`
COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT prop FROM store WHERE key = '$t'" )" -- $CUR) ) COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT prop FROM $_KSTORE_TABLE WHERE key = '$t'" )" -- $CUR) )
;; ;;
esac esac
} }
@ -92,7 +93,7 @@ function _kstoresecret {
function _kstoreinit { function _kstoreinit {
if [ ! -f "$_AUTH_DB" ]; then if [ ! -f "$_AUTH_DB" ]; then
cat <<___SQL___ | $_SQLITE "$_AUTH_DB" cat <<___SQL___ | $_SQLITE "$_AUTH_DB"
CREATE TABLE IF NOT EXISTS store ( CREATE TABLE IF NOT EXISTS $_KSTORE_TABLE (
key TEXT NOT NULL key TEXT NOT NULL
, prop TEXT NOT NULL , prop TEXT NOT NULL
, data TEXT , data TEXT
@ -250,11 +251,11 @@ function _kstoresecret-change {
echo "Backed up at $_BAK" echo "Backed up at $_BAK"
_O_SECRET="$_AUTH_SECRET" _O_SECRET="$_AUTH_SECRET"
for i in `$_SQLITE -list "$_AUTH_DB" "SELECT _ROWID_ FROM store;"`; do for i in `$_SQLITE -list "$_AUTH_DB" "SELECT _ROWID_ FROM $_KSTORE_TABLE;"`; do
_AUTH_SECRET=$_O_SECRET _AUTH_SECRET=$_O_SECRET
_key=`$_SQLITE -list "$_AUTH_DB" "SELECT key FROM store WHERE _ROWID_ = $i;"` _key=`$_SQLITE -list "$_AUTH_DB" "SELECT key FROM $_KSTORE_TABLE WHERE _ROWID_ = $i;"`
_key=`_kstorequote "$_key"` _key=`_kstorequote "$_key"`
_prop=`$_SQLITE -list "$_AUTH_DB" "SELECT prop FROM store WHERE _ROWID_ = $i;"` _prop=`$_SQLITE -list "$_AUTH_DB" "SELECT prop FROM $_KSTORE_TABLE WHERE _ROWID_ = $i;"`
_prop=`_kstorequote "$_prop"` _prop=`_kstorequote "$_prop"`
_val=`kstore get "$_key" "$_prop"` _val=`kstore get "$_key" "$_prop"`
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -299,7 +300,7 @@ function _kstoreupdate {
_val=`_kstorequote "$_val"` _val=`_kstorequote "$_val"`
_prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "UPDATE store SET data = '$_val' WHERE $_cond;" $_SQLITE "$_AUTH_DB" "UPDATE $_KSTORE_TABLE SET data = '$_val' WHERE $_cond;"
} }
function _kstoreadd { function _kstoreadd {
@ -314,7 +315,7 @@ function _kstoreadd {
_val=`_kstorequote "$_val"` _val=`_kstorequote "$_val"`
_prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
$_SQLITE "$_AUTH_DB" \ $_SQLITE "$_AUTH_DB" \
"INSERT INTO store ( key, prop, data ) "INSERT INTO $_KSTORE_TABLE ( key, prop, data )
VALUES( '$_key', '$_prop', '$_val' );" VALUES( '$_key', '$_prop', '$_val' );"
} }
@ -329,9 +330,9 @@ function _kstoreget {
_prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111 $_SQLITE "$_AUTH_DB" "SELECT 1111 FROM $_KSTORE_TABLE WHERE $_cond;" | grep -q 1111
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
$_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | _kstoredec $_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM $_KSTORE_TABLE WHERE $_cond;" | _kstoredec
else else
echo "\"$1\" not found (prop: $_prop)" >&2 echo "\"$1\" not found (prop: $_prop)" >&2
return 1 return 1
@ -350,13 +351,13 @@ function _kstoredel {
_prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111 $_SQLITE "$_AUTH_DB" "SELECT 1111 FROM $_KSTORE_TABLE WHERE $_cond;" | grep -q 1111
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
$_SQLITE "$_AUTH_DB" "SELECT * FROM store WHERE $_cond;" $_SQLITE "$_AUTH_DB" "SELECT * FROM $_KSTORE_TABLE WHERE $_cond;"
echo echo
read -p "Delete this entry (yes/no)? " _CONFIRM read -p "Delete this entry (yes/no)? " _CONFIRM
if [ "$_CONFIRM" == "yes" ]; then if [ "$_CONFIRM" == "yes" ]; then
$_SQLITE "$_AUTH_DB" "DELETE FROM store WHERE $_cond;" $_SQLITE "$_AUTH_DB" "DELETE FROM $_KSTORE_TABLE WHERE $_cond;"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "deleted" echo "deleted"
fi fi
@ -456,7 +457,7 @@ function _kstoredownload-db {
return 1 return 1
fi fi
_T=$( $_SQLITE "$_TMP" "SELECT COUNT( * ) FROM store" ) _T=$( $_SQLITE "$_TMP" "SELECT COUNT( * ) FROM $_KSTORE_TABLE" )
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
return 1 return 1
fi fi
@ -483,11 +484,11 @@ function _kstoresearch {
_termp=`_kstorequote "$2"` _termp=`_kstorequote "$2"`
_cond="key LIKE '%$_termk%' AND prop LIKE '%$_termp%'" _cond="key LIKE '%$_termk%' AND prop LIKE '%$_termp%'"
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store WHERE $_cond;" $_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM $_KSTORE_TABLE WHERE $_cond;"
} }
function _kstorelist { function _kstorelist {
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store;" $_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM $_KSTORE_TABLE;"
} }
function _kstorequery { function _kstorequery {

View File

@ -54,7 +54,7 @@ function _kcontext-k8s {
local _NAME _CONF local _NAME _CONF
case $1 in case $1 in
list) list)
kstore query -list "SELECT SUBSTR( prop, 5 ) FROM store WHERE key = 'kcontext' AND prop LIKE 'k8s.%'" kstore query -list "SELECT SUBSTR( prop, 5 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 'k8s.%'"
return $? return $?
;; ;;
save) save)
@ -118,7 +118,7 @@ function _kcontext-k8s {
if [ "$_CONFIRM" != "y" ]; then if [ "$_CONFIRM" != "y" ]; then
return 0 return 0
fi fi
kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'" kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'"
return $? return $?
else else
echo "No such context: $_NAME" >&2 echo "No such context: $_NAME" >&2
@ -138,7 +138,7 @@ function _kcontext-s3-arch {
local _NAME _CONFIRM _URL _AUTH local _NAME _CONFIRM _URL _AUTH
case $1 in case $1 in
list) list)
kstore query -list "SELECT SUBSTR( prop, 16 ) FROM store WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'" kstore query -list "SELECT SUBSTR( prop, 16 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'"
return $? return $?
;; ;;
save) save)
@ -199,8 +199,8 @@ function _kcontext-s3-arch {
if [ "$_CONFIRM" != "y" ]; then if [ "$_CONFIRM" != "y" ]; then
return 0 return 0
fi fi
kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'" kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'"
kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'" kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'"
return $? return $?
else else
echo "No such context: $_NAME" >&2 echo "No such context: $_NAME" >&2