Json repair script

This commit is contained in:
Alexandre
2021-10-31 13:55:17 +01:00
parent b070f5d53a
commit a9302a9465
2 changed files with 39 additions and 17 deletions

View File

@@ -0,0 +1,32 @@
!/bin/bash
JSONTOCHECK='/config/transmission/settings.json'
JSONSOURCE='/defaults/settings.json'
# If json already exists
if [ -f ${JSONTOCHECK} ]; then
# Variables
echo "Checking settings.json format"
# Get the default keys from the original file
mapfile -t arr < <(jq -r 'keys[]' ${JSONSOURCE})
# Check if all keys are still there, or add them
for KEYS in ${arr[@]}; do
KEYSTHERE=$(jq "has(\"${KEYS}\")" ${JSONTOCHECK})
[ $KEYSTHERE != "true" ] && sed -i "3 i\"${KEYS}\": null," ${JSONTOCHECK} && echo "... $KEYS was missing, added"
done
# Show structure in a nice way
jq . -S ${JSONTOCHECK} | cat >temp.json && mv temp.json ${JSONTOCHECK}
fi
# Repair structure
################
#jq . -S $CONFIGDIR/settings.json | cat >temp.json && mv temp.json $CONFIGDIR/settings.json
#echo "Making sure settings.json structure is good"
#for KEYS in "incomplete-dir" "download-dir" "rpc-host-whitelist-enabled" "rpc-authentication-required" "rpc-username" "rpc-password" "rpc-whitelist-enabled" "rpc-whitelist"; do
# KEYSTHERE=$(jq "has(\"${KEYS}\")" $CONFIGDIR/settings.json)
# [ $KEYSTHERE != "true" ] && sed -i "3 i\"${KEYS}\": null," $CONFIGDIR/settings.json && echo "... $KEYS was missing, added"
#done
#jq . -S $CONFIGDIR/settings.json | cat >temp.json && mv temp.json $CONFIGDIR/settings.json

View File

@@ -14,6 +14,7 @@ CONFIGDIR="/config/transmission"
###############
# PERMISSIONS #
###############
#Default folders
echo "Updating folders"
mkdir -p /config/transmission || true
@@ -24,9 +25,12 @@ if ! bashio::fs.file_exists "$CONFIGDIR/settings.json"; then
cp "/defaults/settings.json" "$CONFIGDIR/settings.json"
fi
################
# Alternate UI #
################
#################
# CONFIGURATION #
#################
# Alternate UI
##############
if bashio::config.has_value 'customUI'; then
### Variables
@@ -39,20 +43,6 @@ if bashio::config.has_value 'customUI'; then
fi
bashio::log.info "UI selected : $CUSTOMUI"
#################
# CONFIGURATION #
#################
# Repair structure
################
jq . -S $CONFIGDIR/settings.json | cat >temp.json && mv temp.json $CONFIGDIR/settings.json
echo "Making sure settings.json structure is good"
for KEYS in "incomplete-dir" "download-dir" "rpc-host-whitelist-enabled" "rpc-authentication-required" "rpc-username" "rpc-password" "rpc-whitelist-enabled" "rpc-whitelist"; do
KEYSTHERE=$(jq "has(\"${KEYS}\")" $CONFIGDIR/settings.json)
[ $KEYSTHERE != "true" ] && sed -i "3 i\"${KEYS}\": null," $CONFIGDIR/settings.json && echo "... $KEYS was missing, added"
done
jq . -S $CONFIGDIR/settings.json | cat >temp.json && mv temp.json $CONFIGDIR/settings.json
# INCOMPLETE DIR
################