Update 19-json_repair.sh

This commit is contained in:
Alexandre
2022-02-17 21:24:09 +01:00
committed by GitHub
parent 8694cddeeb
commit df3913cf62

View File

@@ -1,43 +1,44 @@
#!/usr/bin/with-contenv bashio #!/usr/bin/with-contenv bashio
# shellcheck shell=bash # shellcheck shell=bash
# shellchek disable=SC2015
JSONTOCHECK='/config/transmission/settings.json' JSONTOCHECK='/config/transmission/settings.json'
JSONSOURCE='/defaults/settings.json' JSONSOURCE='/defaults/settings.json'
# If json already exists # If json already exists
if [ -f ${JSONTOCHECK} ]; then if [ -f "${JSONTOCHECK}" ]; then
# Variables # Variables
echo "Checking settings.json format" echo "Checking settings.json format"
# Check if json file valid or not # Check if json file valid or not
jq . -S ${JSONTOCHECK} &>/dev/null && ERROR=false || ERROR=true jq . -S "${JSONTOCHECK}" &>/dev/null && ERROR=false || ERROR=true
if [ $ERROR = true ]; then if [ $ERROR = true ]; then
bashio::log.fatal "Settings.json structure is abnormal, restoring options from scratch. Your old file is renamed as settings.json_old" bashio::log.fatal "Settings.json structure is abnormal, restoring options from scratch. Your old file is renamed as settings.json_old"
mv ${JSONSOURCE} ${JSONSOURCE}_old mv "${JSONSOURCE}" "${JSONSOURCE}"_old
cp ${JSONSOURCE} ${JSONTOCHECK} cp "${JSONSOURCE}" "${JSONTOCHECK}"
exit 0 exit 0
fi fi
# Get the default keys from the original file # Get the default keys from the original file
mapfile -t arr < <(jq -r 'keys[]' ${JSONSOURCE}) mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")
# Check if all keys are still there, or add them # Check if all keys are still there, or add them
# spellcheck disable=SC2068 # spellcheck disable=SC2068
for KEYS in "${arr[@]}"; do for KEYS in "${arr[@]}"; do
# Check if key exists # Check if key exists
KEYSTHERE=$(jq "has(\"${KEYS}\")" ${JSONTOCHECK}) KEYSTHERE=$(jq "has(\"${KEYS}\")" "${JSONTOCHECK}")
if [ "$KEYSTHERE" != "true" ]; then if [ "$KEYSTHERE" != "true" ]; then
#Fetch initial value #Fetch initial value
JSONSOURCEVALUE=$(jq -r ".\"$KEYS\"" ${JSONSOURCE}) JSONSOURCEVALUE=$(jq -r ".\"$KEYS\"" "${JSONSOURCE}")
#Add key #Add key
sed -i "3 i\"${KEYS}\": \"${JSONSOURCEVALUE}\"," ${JSONTOCHECK} sed -i "3 i\"${KEYS}\": \"${JSONSOURCEVALUE}\"," "${JSONTOCHECK}"
# Message # Message
bashio::log.warning "${KEYS} was missing from your settings.json, it was added with the default value ${JSONSOURCEVALUE}" bashio::log.warning "${KEYS} was missing from your settings.json, it was added with the default value ${JSONSOURCEVALUE}"
fi fi
done done
# Show structure in a nice way # Show structure in a nice way
jq . -S ${JSONTOCHECK} | cat >temp.json && mv temp.json ${JSONTOCHECK} jq . -S "${JSONTOCHECK}" | cat >temp.json && mv temp.json "${JSONTOCHECK}"
# Message # Message
bashio::log.info "Your settings.json was checked and seems perfectly normal!" bashio::log.info "Your settings.json was checked and seems perfectly normal!"