Satan made me do it

This commit is contained in:
kake26 2024-11-15 22:48:57 -06:00
parent 7c113360c8
commit 795f72db03
Signed by: kake26
GPG key ID: E8AFC43591876B4D
9 changed files with 118 additions and 69 deletions

44
modules/config_json.bash Executable file
View file

@ -0,0 +1,44 @@
#!/bin/bash
JSON_FILE="config.json"
read_json() {
local JSON_FILE="$1"
local SECTION="$2"
local KEY="$3"
jq -r ".${SECTION}[\"${KEY}\"]" "$JSON_FILE"
}
# Function to write a value to a JSON configuration file
write_json() {
local JSON_FILE="$1"
local SECTION="$2"
local KEY="$3"
local VALUE="$4"
# It important to check if the file is valid otherwise stuff breaks
if ! jq -e . < "$JSON_FILE" > /dev/null 2>&1; then
echo "Error: JSON file is not valid JSON."
return 1
fi
# Using jq to set the value. The original file is replaced with a new file with the updated value.
jq ".${SECTION}.${KEY} = \"$VALUE\"" "$JSON_FILE" > "temp.json" && mv "temp.json" "$JSON_FILE"
}
create_json() {
local JSON_FILE="$1"
echo '{"bashini": "new"}' | jq '.' > "$JSON_FILE" # well we need something in there
}
check_json() {
local JSON_FILE="$1"
if [ ! -f "$JSON_FILE" ]; then
create_json "$JSON_FILE"
fi
}
check_json "$JSON_FILE"