From 9e020a964496ea9b57ad7027120f50f6db743eff Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 27 May 2019 14:30:21 +0200 Subject: [PATCH] Array2Json, module jsshDB.sh --- bashbot.sh | 21 ++++++++++++++++++--- modules/jsonDB.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 modules/jsonDB.sh diff --git a/bashbot.sh b/bashbot.sh index b4c25c7..b950b8f 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.90-dev2-8-g9b05ae0 +#### $$VERSION$$ v0.90-dev2-9-gbbbc8ae # # Exit Codes: # - 0 sucess (hopefully) @@ -314,14 +314,29 @@ JsonGetLine() { JsonGetValue() { sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p' } +# read JSON.sh style data and asssign to an ARRAY +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Json2Array() { + # shellcheck source=./commands.sh + [ "$1" = "" ] || source <( printf "$1"'=( %s )' "$(sed -E -e 's/\t/=/g' -e 's/=(true|false)/="\1"/')" ) +} +# output ARRAY as JSON.sh style data +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Array2Json() { + local ARRAY, key + declare -n ARRAY="$1" + for key in "${!ARRAY[@]}" + do + printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" + done +} ################ # processing of updates starts here process_updates() { local max num debug="$1" max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')" - # shellcheck source=./commands.sh - source <( printf 'UPD=( %s )' "$(sed <<<"${UPDATE}" -E -e 's/\t/=/g' -e 's/=(true|false)/="\1"/')" ) + Json2Array 'UPD' <<<"${UPDATE}" for ((num=0; num<=max; num++)); do process_client "$num" "${debug}" done diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh new file mode 100644 index 0000000..8cffbca --- /dev/null +++ b/modules/jsonDB.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# file: modules/jsshDB.sh +# do not edit, this file will be overwritten on update + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.90-dev2-9-gbbbc8ae +# +# 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 + +# read content of a file in JSON.sh format into given ARRAY +# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront +# $2 filename, must be relative to BASHBOT_ETC, and not contain '..' +jssh_readDB() { + local DB="${BASHBOT_ETC:-.}/$2.jssh" + [ "$2" = "" ] && return 1 + [[ "$2" = *'..'* ]] && return 1 + [ ! -f "${DB}" ] && return 1 + Json2Array "$1" <"${DB}" +} + +# write ARRAY content to a file in JSON.sh format +# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront +# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +jssh_writeDB() { + local DB="${BASHBOT_ETC:-.}/$2.jssh" + [ "$2" = "" ] && return 1 + [[ "$2" = *'..'* ]] && return 1 + [ ! -f "${DB}" ] && return 1 + Array2Json "$1" >"${DB}" +} + +# $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +jssh_newDB() { + local DB="${BASHBOT_ETC:-.}/$1.jssh" + [ "$1" = "" ] && return 1 + [[ "$2" = *'..'* ]] && return 1 + [ -f "${DB}" ] && return 1 # already exist, do not zero out + printf '\n' >"${DB}" +}