From 55c9d8bb4d65684d40360301354d95a81a97b2a8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 4 Jun 2019 12:00:19 +0200 Subject: [PATCH] jsshDB: read write single key/value pairs --- bashbot.sh | 2 +- doc/7_develop.md | 4 ++-- modules/jsonDB.sh | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 16e60db..012caa8 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.91-2-g1e851bd +#### $$VERSION$$ v0.91-3-g4594e05 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/doc/7_develop.md b/doc/7_develop.md index 663f60e..0260ec8 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -87,7 +87,7 @@ This means if you register a every 5 Minutes event its first execution may < 5 M * 0 ignored * 1 execute every minute * x execute every x minutes - * -x execute ONCE in x minutes*\** + * -x execute ONCE in x minutes *\** *\* if you really want "in x minutes" you must use ```-(EVENT_TIMER+x)```* @@ -286,5 +286,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v0.91-1-gdb03e23 +#### $$VERSION$$ v0.91-4-gaad0bfe diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 2b85672..1c266bb 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,12 +5,12 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.91-0-g31808a9 +#### $$VERSION$$ v0.91-4-gaad0bfe # # source from commands.sh to use jsonDB functions # -# jsonDB rovides simple functions to read and store bash Arrays -# from to file in JSON.sh output format +# jsonDB provides simple functions to read and store bash Arrays +# from to file in JSON.sh output format, its a simple key/value storage. # read content of a file in JSON.sh format into given ARRAY # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront @@ -18,7 +18,7 @@ jssh_readDB() { local DB; DB="$(jssh_checkname "$2")" [ "${DB}" = "" ] && return 1 - [ ! -f "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 Json2Array "$1" <"${DB}" } @@ -28,15 +28,42 @@ jssh_readDB() { jssh_writeDB() { local DB; DB="$(jssh_checkname "$2")" [ "${DB}" = "" ] && return 1 - [ ! -f "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 Array2Json "$1" >"${DB}" } +# insert, update, apped key/value to jsshDB +# $1 key name, can onyl contain -a-zA-Z0-9,._ +# $2 key value +# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +jssh_insertDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + local key="$1" value="$2" + local DB; DB="$(jssh_checkname "$3")" + [ "${DB}" = "" ] && return 1 + [ ! -f "${DB}" ] && return 2 + # its append, but last one counts, its a simple DB ... + printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" + +} + +# get key/value from jsshDB +# $1 key name, can onyl contain -a-zA-Z0-9,._ +# $2 key value +# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +# returns value +jssh_getDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + declare -A getARR + jssh_readDB "getARR" "$3" || return "$?" + printf '%s\n' "${getARR[${key}]}" +} + # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' jssh_newDB() { local DB; DB="$(jssh_checkname "$1")" [ "${DB}" = "" ] && return 1 - [ -f "${DB}" ] && return 1 # already exist, do not zero out + [ -f "${DB}" ] && return 2 # already exist, do not zero out printf '\n' >"${DB}" } @@ -45,6 +72,7 @@ jssh_checkname(){ [ "$1" = "" ] && return 1 local DB="${BASHBOT_ETC:-.}/$1.jssh" [[ "$1" = "${BASHBOT_ETC:-.}"* ]] && DB="$1.jssh" - [[ "$1" = *'..'* ]] && return 1 + [[ "$1" = *'..'* ]] && return 2 printf '%s\n' "${DB}" } +