Update zrep script to version 0.0.4

- Updated version number to 0.0.4
- Added timestamp to remoteFile URL
- Improved update_package function logic
- Added prompt for user confirmation before updating
- Updated downloader commands for curl, wget, and wget2
- Improved error handling in download_package function
- Improved installation process in install_package function
This commit is contained in:
Stig-Ørjan Smelror 2024-03-31 15:14:43 +02:00
parent c4c384c799
commit 8f34150a78

117
zrep
View File

@ -2,7 +2,7 @@
setopt extendedglob setopt extendedglob
VERSION="0.0.3" # Sun-2024-03-17 VERSION="0.0.4" # Sun-2024-03-31
ZREP="Zsh Repository Tool" ZREP="Zsh Repository Tool"
# Define the default path to .zreprc # Define the default path to .zreprc
ZREP_CONFIG="${HOME}/.zreprc" ZREP_CONFIG="${HOME}/.zreprc"
@ -480,13 +480,13 @@ function zrep_global_downloader() {
exit exit
;; ;;
esac esac
eval ${dloader} ${1} eval ${dloader} "${1}"
} }
typeset -A updatesAvailable typeset -A updatesAvailable
function zrep_check_for_updates() { function zrep_check_for_updates() {
local remoteFile="${config[global_repo_url]}/packages.json" local remoteFile="${config[global_repo_url]}/packages.json\?$(date +%s)"
# local localFile="${config[main_zrep_install_dir]}/installed.json" # local localFile="${config[main_zrep_install_dir]}/installed.json"
zrep_installed_json zrep_installed_json
local remotePackages=$(zrep_global_downloader "${remoteFile}") local remotePackages=$(zrep_global_downloader "${remoteFile}")
@ -524,7 +524,6 @@ function zrep_update_package() {
local specificPackage=${1} local specificPackage=${1}
zrep_check_for_updates zrep_check_for_updates
# Check if in update mode or specific package update
if [[ -n "${specificPackage}" ]]; then if [[ -n "${specificPackage}" ]]; then
# Logic for updating a specific package # Logic for updating a specific package
# Assuming specificPackage format is "author/script" # Assuming specificPackage format is "author/script"
@ -541,17 +540,30 @@ function zrep_update_package() {
fi fi
else else
if [[ ${updates} == "true" ]]; then if [[ ${updates} == "true" ]]; then
# General update mode: update all packages listed in updatesAvailable # Prompt the user only if updates are available
for package in ${(k)updatesAvailable}; do echo "New updates are available. Do you want to proceed with updating? (Y/n): "
local author=${package%%/*} read -q "response?"
local script=${package#*/} echo # Move to a new line
local version=${updatesAvailable[${package}]}
# local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
# echo "Preparing to update $package to version $version..." # Set the default response to 'Y' if no input is entered
local install_pkg="${author}/${script}" response=${response:-Y}
zrep_install_package u ${install_pkg}
done # Proceed with update if the response is 'Y' or 'y'
if [[ $response =~ ^[Yy]$ ]]; then
# General update mode: update all packages listed in updatesAvailable
for package in ${(k)updatesAvailable}; do
local author=${package%%/*}
local script=${package#*/}
local version=${updatesAvailable[${package}]}
# local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
# echo "Preparing to update $package to version $version..."
local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg}
done
else
zrep_msg info "Update canceled."
fi
fi fi
fi fi
} }
@ -575,46 +587,49 @@ function zrep_download_package() {
local downloader="" local downloader=""
local http_status local http_status
local cmd local cmd
local exit_status
case "${config[global_downloader]}" in case "${config[global_downloader]}" in
curl) curl)
downloader="curl" downloader="curl"
cmd="curl -A \"zrep ${VERSION} (curl)\" -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\"" cmd="curl -L -A \"zrep ${VERSION} (curl)\" -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\""
;; ;;
wget) wget)
downloader="wget" downloader="wget"
cmd="wget -U \"zrep ${VERSION} (wget)\" -q -O \"$ZipFile\" \"$DownloadURL\"; echo $?" cmd="wget -L -U \"zrep ${VERSION} (wget)\" -q -O \"$ZipFile\" \"$DownloadURL\""
;; ;;
wget2) wget2)
downloader="wget2" downloader="wget2"
cmd="wget2 -U \"zrep ${VERSION} (wget2)\" -q -O \"$ZipFile\" \"$DownloadURL\"; echo $?" cmd="wget2 -L -U \"zrep ${VERSION} (wget2)\" -q -O \"$ZipFile\" \"$DownloadURL\""
;; ;;
*) *)
zrep_msg debug "Unsupported or unspecified downloader: '${config[global_downloader]}'." zrep_msg debug "Unsupported or unspecified downloader: '${config[global_downloader]}'."
return 1 return 1
;; ;;
esac esac
while (( attempt <= retries )); do while (( attempt <= retries )); do
zrep_msg sub "Attempt $attempt of $retries: Downloading using $downloader..." # zrep_msg sub "Attempt $attempt of $retries: Downloading using $downloader..."
if [[ $downloader == "curl" ]]; then if [[ $downloader == "curl" ]]; then
eval $cmd
http_status="${PIPESTATUS[0]}"
else # wget or wget2
http_status=$(eval $cmd) http_status=$(eval $cmd)
exit_status=$?
# For curl, check HTTP status is 200 and exit status is 0
if [[ $exit_status -eq 0 && $http_status -eq 200 ]]; then
# zrep_msg sub "a.Download successful."
return 0
fi
else # wget or wget2
eval $cmd
exit_status=$?
# For wget/wget2, just check exit status is 0
if [[ $exit_status -eq 0 ]]; then
# zrep_msg sub "b.Download successful."
return 0
fi
fi fi
sleep $delay
# Check if download command was successful (0 exit status) and, for curl, HTTP status is 200 (OK) ((attempt++))
if [[ $down_status -eq 0 && ($downloader != "curl" || $http_status -eq 200) ]]; then
zrep_msg sub "Download successful."
return 0
else
zrep_msg debug "Error: Failed to download the package. HTTP status (curl only): ${http_status}."
((attempt++))
zrep_msg info "Waiting ${delay} seconds before retrying..."
sleep $delay
fi
done done
zrep_msg debug "Error: The download failed after $retries attempts." zrep_msg debug "Error: The download failed after $retries attempts."
@ -627,20 +642,20 @@ function zrep_install_package() {
if [[ ${1} == "u" ]]; then if [[ ${1} == "u" ]]; then
updates=true updates=true
local package=${2} local package=${2}
echo "zrep_install_package: package=$package" #echo "zrep_install_package: package=$package"
else else
updates=false updates=false
local package="${1}" local package="${1}"
# Call zrep_check_if_installed to check if the package is already installed # Call zrep_check_if_installed to check if the package is already installed
if zrep_check_if_installed "${package}"; then if zrep_check_if_installed "${package}"; then
zrep_msg info "Package ${package} is already installed." zrep_msg debug "\nPackage ${package} is already installed."
zrep_msg info "Use 'zrep list' to see installed packages." zrep_msg info "Use 'zrep list' to see installed packages."
return 0 return 0
fi fi
fi fi
# If not installed, proceed with fetching the package information # If not installed, proceed with fetching the package information
zrep_parse_remote "${config[global_repo_url]}/packages.json" "${package}" zrep_parse_remote "${config[global_repo_url]}/packages.json\?$(date +%s)" "${package}"
local tmpDir="${config[main_zrep_install_dir]}/tmp" local tmpDir="${config[main_zrep_install_dir]}/tmp"
@ -649,7 +664,7 @@ function zrep_install_package() {
local zipFile="${tmpDir}/${author}-${script}-${version}.zip" local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
zrep_download_package "${zipFile}" "${dlurl}" zrep_download_package "${zipFile}" "${dlurl}"
unzip -q -aa -o "${zipFile}" -d "${config[main_zrep_install_dir]}" unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
zrep_msg debug "\nError: Failed to unzip the package." zrep_msg debug "\nError: Failed to unzip the package."