From a7037ab76db2763ee7641cf180b41005dbb2a919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Mon, 11 Nov 2024 06:28:53 +0800 Subject: [PATCH] Removed hard-coded table name --- bash/sources/16_keystore | 33 +++++++++++++++++---------------- bash/sources/17_kcontext | 10 +++++----- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/bash/sources/16_keystore b/bash/sources/16_keystore index 752cee1..229c026 100644 --- a/bash/sources/16_keystore +++ b/bash/sources/16_keystore @@ -16,6 +16,7 @@ _AUTH_DB=$RBASH_HOME/keystore.db _AUTH_SECRET= _KSTORE_DEF_PROP=${_KSTORE_DEF_PROP:-val} _KSTORE_DEC_FAILED="$RBASH_HOME/.decypt-failed" +_KSTORE_TABLE=store if [ -f "$RBASH_HOME/keystore.secret" ]; then _AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" ) @@ -61,7 +62,7 @@ function _kstore { return ;; get|update) - COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT key FROM store" )" -- $CUR) ) + COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT key FROM $_KSTORE_TABLE" )" -- $CUR) ) ;; esac @@ -69,7 +70,7 @@ function _kstore { case "$t" in get|update) 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 } @@ -92,7 +93,7 @@ function _kstoresecret { function _kstoreinit { if [ ! -f "$_AUTH_DB" ]; then cat <<___SQL___ | $_SQLITE "$_AUTH_DB" -CREATE TABLE IF NOT EXISTS store ( +CREATE TABLE IF NOT EXISTS $_KSTORE_TABLE ( key TEXT NOT NULL , prop TEXT NOT NULL , data TEXT @@ -250,11 +251,11 @@ function _kstoresecret-change { echo "Backed up at $_BAK" _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 - _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"` - _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"` _val=`kstore get "$_key" "$_prop"` if [ $? -ne 0 ]; then @@ -299,7 +300,7 @@ function _kstoreupdate { _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;" + $_SQLITE "$_AUTH_DB" "UPDATE $_KSTORE_TABLE SET data = '$_val' WHERE $_cond;" } function _kstoreadd { @@ -314,7 +315,7 @@ function _kstoreadd { _val=`_kstorequote "$_val"` _prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"` $_SQLITE "$_AUTH_DB" \ - "INSERT INTO store ( key, prop, data ) + "INSERT INTO $_KSTORE_TABLE ( key, prop, data ) VALUES( '$_key', '$_prop', '$_val' );" } @@ -329,9 +330,9 @@ function _kstoreget { _prop=`_kstorequote "${2:-$_KSTORE_DEF_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 - $_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | _kstoredec + $_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM $_KSTORE_TABLE WHERE $_cond;" | _kstoredec else echo "\"$1\" not found (prop: $_prop)" >&2 return 1 @@ -350,13 +351,13 @@ function _kstoredel { _prop=`_kstorequote "${2:-$_KSTORE_DEF_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 - $_SQLITE "$_AUTH_DB" "SELECT * FROM store WHERE $_cond;" + $_SQLITE "$_AUTH_DB" "SELECT * FROM $_KSTORE_TABLE WHERE $_cond;" echo read -p "Delete this entry (yes/no)? " _CONFIRM 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 echo "deleted" fi @@ -456,7 +457,7 @@ function _kstoredownload-db { return 1 fi - _T=$( $_SQLITE "$_TMP" "SELECT COUNT( * ) FROM store" ) + _T=$( $_SQLITE "$_TMP" "SELECT COUNT( * ) FROM $_KSTORE_TABLE" ) if [ $? -ne 0 ]; then return 1 fi @@ -483,11 +484,11 @@ function _kstoresearch { _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;" + $_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM $_KSTORE_TABLE WHERE $_cond;" } 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 { diff --git a/bash/sources/17_kcontext b/bash/sources/17_kcontext index df19590..f73c946 100644 --- a/bash/sources/17_kcontext +++ b/bash/sources/17_kcontext @@ -54,7 +54,7 @@ function _kcontext-k8s { local _NAME _CONF case $1 in 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 $? ;; save) @@ -118,7 +118,7 @@ function _kcontext-k8s { if [ "$_CONFIRM" != "y" ]; then return 0 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 $? else echo "No such context: $_NAME" >&2 @@ -138,7 +138,7 @@ 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.%'" + kstore query -list "SELECT SUBSTR( prop, 16 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'" return $? ;; save) @@ -199,8 +199,8 @@ function _kcontext-s3-arch { 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'" + 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