Update zrep script with improvements in file existence check and script entry handling. Fix JSON file creation and ensure it's not empty. Refactor script entry update/addition logic for better clarity. Rearrange curl command options for zrep_download_package function.

This commit is contained in:
Stig-Ørjan Smelror 2024-03-15 22:45:37 +01:00
parent c297c2a551
commit d4e355cf32

9
zrep
View File

@ -278,12 +278,12 @@ function zrep_update_installed_json() {
local version="${3}" local version="${3}"
zrep_installed_json zrep_installed_json
# Ensure the JSON file exists, creating an empty object if not # Ensure the JSON file exists and is not empty, creating an empty object if necessary
if [[ ! -f "${installed_json}" ]]; then if [[ ! -f "${installed_json}" ]] || [[ ! -s "${installed_json}" ]]; then
echo "{}" > "${installed_json}" echo "{}" > "${installed_json}"
fi fi
# Correctly handle updating or adding script entries within the nested array # Update or add the script entry
jq --arg author "$author" --arg script "$script" --arg version "$version" \ jq --arg author "$author" --arg script "$script" --arg version "$version" \
'if has($author) then 'if has($author) then
.[$author] |= map(if .script == $script then .version = $version else . end) | .[$author] |= map(if .script == $script then .version = $version else . end) |
@ -292,7 +292,6 @@ else
.[$author] = [{"script": $script, "version": $version}] .[$author] = [{"script": $script, "version": $version}]
end' "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json" end' "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json"
zrep_msg info " - Package '$script' by '$author' version $version installed/updated successfully." zrep_msg info " - Package '$script' by '$author' version $version installed/updated successfully."
} }
@ -533,7 +532,7 @@ function zrep_download_package() {
case "${config[global_downloader]}" in case "${config[global_downloader]}" in
curl) curl)
downloader="curl" downloader="curl"
cmd="curl -s -w \"%{http_code}\" -o \"$ZipFile\" \"$DownloadURL\"" cmd="curl -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\""
;; ;;
wget) wget)
downloader="wget" downloader="wget"