From b1601208936d1e6bb915f9fba09f519b8904ba37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Fri, 15 Mar 2024 22:55:52 +0100 Subject: [PATCH] Update zrep script to handle cases where installed.json is empty or contains no packages. Add checks to handle empty JSON objects or arrays. --- zrep | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/zrep b/zrep index a582388..da9fe70 100755 --- a/zrep +++ b/zrep @@ -299,9 +299,21 @@ end' "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$ins function zrep_list_installed_packages() { zrep_installed_json + # Check if installed.json exists and is not empty + if [[ ! -s "${installed_json}" ]]; then + zrep_msg info "No packages installed." + return + fi + # Parse installed.json and list packages zrep_msg sub "\nInstalled packages:" + # Check if the JSON file is effectively empty ({} or []) + if jq -e 'if type == "object" then . == {} elif type == "array" then . == [] else false end' "${installed_json}" >/dev/null; then + zrep_msg info "No packages installed." + return + fi + # Iterate through each author and their packages jq -r 'to_entries | .[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "${installed_json}" | while IFS= read -r package_info; do local package_name=$(echo "${package_info}" | cut -d ' ' -f1) # Extract package name before the version