21 lines
574 B
Bash
21 lines
574 B
Bash
#!/bin/bash
|
|
#
|
|
# Purpose : Edit JSON settings files for configure.d items. Source this file.
|
|
|
|
# Edit a JSON file in place with jq, creating it from $2 if it's missing.
|
|
# Leaves the file alone if it isn't valid JSON or jq fails.
|
|
edit_json() {
|
|
local file="$1" empty="$2" tmp
|
|
shift 2
|
|
mkdir -p "$(dirname "$file")" || return 1
|
|
[ -f "$file" ] || echo "$empty" > "$file"
|
|
tmp="$(mktemp "$file.XXXXXX")" || return 1
|
|
if jq "$@" "$file" > "$tmp"; then
|
|
mv -f "$tmp" "$file"
|
|
else
|
|
echo "Couldn't change $file; is it valid JSON?"
|
|
rm -f "$tmp"
|
|
return 1
|
|
fi
|
|
}
|