move Json2Arru and Array2Json to jsshDB module

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-13 17:56:43 +02:00
parent 5baab14cd2
commit e0470aadc0
2 changed files with 22 additions and 22 deletions

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.98-dev-4-gf32833c #### $$VERSION$$ v0.98-dev-5-g5baab14
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -53,27 +53,9 @@ _round_float() {
local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0" local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0"
LC_ALL=C printf "%.${digit}f" "${1}" LC_ALL=C printf "%.${digit}f" "${1}"
} }
# 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
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -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 key
declare -n ARRAY="$1"
for key in "${!ARRAY[@]}"
do
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}"
done
}
getConfigKey() { getConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
declare -A confARR sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1
Json2Array "confARR" <"${BOTDATABASE}.jssh"
printf '%s' "${confARR["$1"]}"
} }
# get location and name of bashbot.sh # get location and name of bashbot.sh
@ -136,7 +118,7 @@ if [ -z "${BOTTOKEN}" ]; then
[ ! -f "${BOTDATABASE}.jssh" ] && [ ! -f "${BOTDATABASE}.jssh" ] &&
printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTDATABASE}.jssh" printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTDATABASE}.jssh"
# BOTTOKEN empty read ask user # BOTTOKEN empty read ask user
if ! grep -qs '\["bottoken"\]' "${BOTDATABASE}.jssh" ; then if [ -z "$(getConfigKey "bottoken")" ]; then
# convert old token # convert old token
if [ -r "${TOKENFILE}" ]; then if [ -r "${TOKENFILE}" ]; then
printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh" printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh"

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.98-dev-0-ga8fe178 #### $$VERSION$$ v0.98-dev-5-g5baab14
# #
# source from commands.sh to use jsonDB functions # source from commands.sh to use jsonDB functions
# #
@ -323,4 +323,22 @@ jssh_clearDB_async() {
printf '' >"${DB}" printf '' >"${DB}"
} }
# 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
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
}
# get Config Key from jssh file without jsshDB
# output ARRAY as JSON.sh style data
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
Array2Json() {
[ -z "$1" ] && return 1
local key
declare -n ARRAY="$1"
for key in "${!ARRAY[@]}"
do
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}"
done
}