Fix a few bugs preventing the download of scripts

This commit is contained in:
Stig-Ørjan Smelror 2024-07-01 09:42:36 +02:00
parent 0372eba263
commit 36b81d6657

60
zpi
View File

@ -86,11 +86,11 @@ declare -A current_theme
function zrep_load_theme() { function zrep_load_theme() {
local theme_name="$1" local theme_name="$1"
local theme_file="${config[main_zrep_install_dir]}/themes/${theme_name}" local theme_file="/home/stig/.zrep/themes/${theme_name}"
if [[ ! -f "$theme_file" ]]; then if [[ ! -f "$theme_file" ]]; then
echo "Error: Theme file for '${theme_name}' not found. Falling back to the 'classic' theme." echo "Error: Theme file for '${theme_name}' not found. Falling back to the 'classic' theme."
theme_file="${config[main_zrep_install_dir]}/themes/classic" theme_file="/home/stig/.zrep/themes/classic"
fi fi
# Source the theme file, which should define 'theme_colors' # Source the theme file, which should define 'theme_colors'
@ -332,6 +332,7 @@ function zrep_installed_json() {
# Function to parse remote JSON data and extract author, script, and version # Function to parse remote JSON data and extract author, script, and version
# and return the correct download url # and return the correct download url
function zrep_parse_remote() { function zrep_parse_remote() {
local url="${1}" local url="${1}"
local package="${2}" local package="${2}"
@ -339,19 +340,21 @@ function zrep_parse_remote() {
local script_name="${package#*/}" local script_name="${package#*/}"
local json_data local json_data
# Print the URL being used for debugging
echo "Fetching URL: ${url}"
# Fetch JSON data from the URL # Fetch JSON data from the URL
json_data=$(zrep_global_downloader "${url}") json_data=$(zrep_global_downloader "${url}")
# Print the fetched JSON data for debugging
echo "Fetched JSON data: ${json_data}"
# Directly extract the details based on author_name and script_name # Directly extract the details based on author_name and script_name
version=$(echo "${json_data}" | jq -r --arg author_name "$author_name" --arg script_name "$script_name" '.authors[] | select(.name==$author_name) | .scripts[] | select(.name==$script_name) | .version') version=$(echo "${json_data}" | jq -r --arg author_name "$author_name" --arg script_name "$script_name" '.authors[] | select(.name==$author_name) | .scripts[] | select(.name==$script_name) | .version')
# Check if the dlurl and version are found # Check if the dlurl and version are found
if [[ -n "$version" ]]; then if [[ -n "$version" ]]; then
# Set the details as global
#export author="$author_name"
#export script="$script_name"
export version export version
#export dlurl
else else
zrep_msg debug "\nPackage ${package} not found.\n" zrep_msg debug "\nPackage ${package} not found.\n"
exit 1 exit 1
@ -524,22 +527,24 @@ function zrep_global_downloader() {
local downloadURL="${1}" local downloadURL="${1}"
local outputFile="${2:-}" # Optional, used for downloading files local outputFile="${2:-}" # Optional, used for downloading files
local cmd local cmd
local http_status
local exit_status
local retries=5 local retries=5
local delay=5 local delay=5
local attempt=0 local attempt=0
case ${config[global_downloader]} in case ${config[global_downloader]} in
curl) curl)
cmd="curl -s -L -A \"${ZSH_SCRIPT:t} ${VERSION} (curl)\"" if [[ -n $outputFile ]]; then
[[ -n $outputFile ]] && cmd+=" -o \"$outputFile\"" cmd="curl -s -L -A \"${ZSH_SCRIPT:t} ${VERSION} (curl)\" -w \"%{http_code}\" -o \"$outputFile\" \"$downloadURL\""
cmd+=" -w \"%{http_code}\" \"$downloadURL\"" else
cmd="curl -s -L -A \"${ZSH_SCRIPT:t} ${VERSION} (curl)\" -w \"%{http_code}\" \"$downloadURL\""
fi
;; ;;
wget|wget2) wget|wget2)
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\"" if [[ -n $outputFile ]]; then
[[ -n $outputFile ]] && cmd+=" -O \"$outputFile\"" cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" -O \"$outputFile\" \"$downloadURL\""
cmd+=" \"$downloadURL\"" else
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" \"$downloadURL\""
fi
;; ;;
*) *)
echo "Unsupported downloader." echo "Unsupported downloader."
@ -549,15 +554,22 @@ function zrep_global_downloader() {
while (( ++attempt <= retries )); do while (( ++attempt <= retries )); do
if [[ ${config[global_downloader]} == "curl" ]]; then if [[ ${config[global_downloader]} == "curl" ]]; then
http_status=$(eval $cmd) local response
exit_status=$? response=$(eval $cmd)
if [[ $exit_status -eq 0 && ( -z $outputFile || $http_status -eq 200 ) ]]; then local http_status="${response: -3}"
local json_data="${response:0: -3}"
if [[ $http_status -eq 200 ]]; then
[[ -z $outputFile ]] && echo "$json_data"
return 0 return 0
else
echo "HTTP Status: $http_status"
return 1
fi fi
else else
eval $cmd eval $cmd
exit_status=$? exit_status=$?
if [[ $exit_status -eq 0 ]]; then if [[ $exit_status -eq 0 ]]; then
[[ -n $outputFile ]] && cat "$outputFile"
return 0 return 0
fi fi
fi fi
@ -670,6 +682,7 @@ function zrep_download_package() {
} }
# Function to install a package by unzipping it to ${config[main_zrep_install_dir]} # Function to install a package by unzipping it to ${config[main_zrep_install_dir]}
function zrep_install_package() { function zrep_install_package() {
if [[ ${1} == "u" ]]; then if [[ ${1} == "u" ]]; then
@ -691,23 +704,24 @@ function zrep_install_package() {
return 0 return 0
fi fi
zrep_parse_remote "${config[global_repo_url]}/getver.php\?p\=${package}" ${package} zrep_parse_remote "${config[global_repo_url]}/getver.php?p=${package}" ${package}
fi fi
local tmpDir="${config[main_zrep_install_dir]}/tmp" local tmpDir="${config[main_zrep_install_dir]}/tmp"
mkdir -p "${tmpDir}" mkdir -p "${tmpDir}"
author="${package%/*}" author="${package%/*}"
script="${package#*/}" script="${package#*/}"
local zipFile="${tmpDir}/${author}-${script}-${version}.zip" local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
dlurl="${config[global_repo_url]}/download/${package}/${version}" local dlurl="${config[global_repo_url]}/download/${package}/${version}"
zrep_download_package "${zipFile}" "${dlurl}" echo "Download URL: ${dlurl}" # Print the download URL for debugging
if ! zrep_download_package "${zipFile}" "${dlurl}"; then
zrep_msg debug "\nError: Failed to download the package."
return 1
fi
unzip -q -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."
return 1 return 1