Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
36b81d6657 | |||
0372eba263 | |||
d0136a9b19 | |||
933b27b167 |
160
zpi
160
zpi
@ -84,13 +84,13 @@ typeset -A base_colors=(
|
||||
# Define the global associative array to hold the current theme
|
||||
declare -A current_theme
|
||||
|
||||
zrep_load_theme() {
|
||||
function zrep_load_theme() {
|
||||
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
|
||||
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
|
||||
|
||||
# Source the theme file, which should define 'theme_colors'
|
||||
@ -137,7 +137,7 @@ function zrep_msg() {
|
||||
|
||||
#################################################################################################
|
||||
# Function to URL-encode strings in Zsh
|
||||
zrep_search_url_encode() {
|
||||
function zrep_search_url_encode() {
|
||||
local string="${1}"
|
||||
local strlen=${#string}
|
||||
local encoded=""
|
||||
@ -156,7 +156,7 @@ zrep_search_url_encode() {
|
||||
}
|
||||
|
||||
# Function to perform a search query and process JSON response
|
||||
zrep_search() {
|
||||
function zrep_search() {
|
||||
local searchTerm="${@}"
|
||||
|
||||
local encodedSearch=$(zrep_search_url_encode "${searchTerm}")
|
||||
@ -332,6 +332,7 @@ function zrep_installed_json() {
|
||||
|
||||
# Function to parse remote JSON data and extract author, script, and version
|
||||
# and return the correct download url
|
||||
|
||||
function zrep_parse_remote() {
|
||||
local url="${1}"
|
||||
local package="${2}"
|
||||
@ -339,19 +340,21 @@ function zrep_parse_remote() {
|
||||
local script_name="${package#*/}"
|
||||
local json_data
|
||||
|
||||
# Print the URL being used for debugging
|
||||
echo "Fetching URL: ${url}"
|
||||
|
||||
# Fetch JSON data from the 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
|
||||
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
|
||||
if [[ -n "$version" ]]; then
|
||||
# Set the details as global
|
||||
#export author="$author_name"
|
||||
#export script="$script_name"
|
||||
export version
|
||||
#export dlurl
|
||||
else
|
||||
zrep_msg debug "\nPackage ${package} not found.\n"
|
||||
exit 1
|
||||
@ -521,23 +524,59 @@ function zrep_check_if_installed() {
|
||||
}
|
||||
|
||||
function zrep_global_downloader() {
|
||||
local downloadURL="${1}"
|
||||
local outputFile="${2:-}" # Optional, used for downloading files
|
||||
local cmd
|
||||
local retries=5
|
||||
local delay=5
|
||||
local attempt=0
|
||||
|
||||
case ${config[global_downloader]} in
|
||||
curl)
|
||||
dloader="curl -s -A \"${ZSH_SCRIPT} ${VERSION} (curl)\""
|
||||
if [[ -n $outputFile ]]; then
|
||||
cmd="curl -s -L -A \"${ZSH_SCRIPT:t} ${VERSION} (curl)\" -w \"%{http_code}\" -o \"$outputFile\" \"$downloadURL\""
|
||||
else
|
||||
cmd="curl -s -L -A \"${ZSH_SCRIPT:t} ${VERSION} (curl)\" -w \"%{http_code}\" \"$downloadURL\""
|
||||
fi
|
||||
;;
|
||||
wget)
|
||||
dloader="wget -q -U \"${ZSH_SCRIPT} ${VERSION} (wget)\""
|
||||
;;
|
||||
wget2)
|
||||
dloader="wget2 -q -U \"${ZSH_SCRIPT} ${VERSION} (wget2)\""
|
||||
wget|wget2)
|
||||
if [[ -n $outputFile ]]; then
|
||||
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" -O \"$outputFile\" \"$downloadURL\""
|
||||
else
|
||||
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" \"$downloadURL\""
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Invalid Downloader."
|
||||
exit
|
||||
echo "Unsupported downloader."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
eval ${dloader} "${1}"
|
||||
while (( ++attempt <= retries )); do
|
||||
if [[ ${config[global_downloader]} == "curl" ]]; then
|
||||
local response
|
||||
response=$(eval $cmd)
|
||||
local http_status="${response: -3}"
|
||||
local json_data="${response:0: -3}"
|
||||
if [[ $http_status -eq 200 ]]; then
|
||||
[[ -z $outputFile ]] && echo "$json_data"
|
||||
return 0
|
||||
else
|
||||
echo "HTTP Status: $http_status"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
eval $cmd
|
||||
exit_status=$?
|
||||
if [[ $exit_status -eq 0 ]]; then
|
||||
[[ -n $outputFile ]] && cat "$outputFile"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
sleep $delay
|
||||
done
|
||||
echo "Download failed after $retries attempts."
|
||||
return 1
|
||||
}
|
||||
|
||||
function zrep_process_updates() {
|
||||
@ -629,75 +668,21 @@ function zrep_update_package() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Enhanced zrep_downloader function with error handling and retry logic.
|
||||
# It attempts to download a file from a given URL to a specified zip file using curl, wget, or wget2 based on the global configuration.
|
||||
# This function supports retries and delays between attempts for robust error handling.
|
||||
#
|
||||
# Usage: zrep_download_package ZipFile DownloadURL
|
||||
#
|
||||
# Parameters:
|
||||
# DownloadURL: The URL from which to download the file.
|
||||
# ZipFile: The name of the file to save the downloaded content to.
|
||||
|
||||
function zrep_download_package() {
|
||||
local ZipFile="${1}"
|
||||
local DownloadURL="${2}"
|
||||
local retries=5
|
||||
local delay=5
|
||||
local attempt=1
|
||||
local downloader=""
|
||||
local http_status
|
||||
local cmd
|
||||
local exit_status
|
||||
local ZipFile="${1}"
|
||||
local DownloadURL="${2}"
|
||||
|
||||
case "${config[global_downloader]}" in
|
||||
curl)
|
||||
downloader="curl"
|
||||
cmd="curl -L -A \"${ZSH_SCRIPT} ${VERSION} (curl)\" -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\""
|
||||
;;
|
||||
wget)
|
||||
downloader="wget"
|
||||
cmd="wget -L -U \"${ZSH_SCRIPT} ${VERSION} (wget)\" -q -O \"$ZipFile\" \"$DownloadURL\""
|
||||
;;
|
||||
wget2)
|
||||
downloader="wget2"
|
||||
cmd="wget2 -L -U \"${ZSH_SCRIPT} ${VERSION} (wget2)\" -q -O \"$ZipFile\" \"$DownloadURL\""
|
||||
;;
|
||||
*)
|
||||
zrep_msg debug "Unsupported or unspecified downloader: '${config[global_downloader]}'."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
while (( attempt <= retries )); do
|
||||
# zrep_msg sub "Attempt $attempt of $retries: Downloading using $downloader..."
|
||||
|
||||
if [[ $downloader == "curl" ]]; then
|
||||
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
|
||||
# Now simply call the unified downloader function
|
||||
if zrep_global_downloader "$DownloadURL" "$ZipFile"; then
|
||||
zrep_msg std "\nDownload successful."
|
||||
else
|
||||
zrep_msg debug "\nDownload failed."
|
||||
return 1
|
||||
fi
|
||||
sleep $delay
|
||||
((attempt++))
|
||||
done
|
||||
|
||||
zrep_msg debug "Error: The download failed after $retries attempts."
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to install a package by unzipping it to ${config[main_zrep_install_dir]}
|
||||
|
||||
function zrep_install_package() {
|
||||
|
||||
if [[ ${1} == "u" ]]; then
|
||||
@ -719,23 +704,24 @@ function zrep_install_package() {
|
||||
return 0
|
||||
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
|
||||
|
||||
|
||||
local tmpDir="${config[main_zrep_install_dir]}/tmp"
|
||||
|
||||
mkdir -p "${tmpDir}"
|
||||
|
||||
author="${package%/*}"
|
||||
script="${package#*/}"
|
||||
|
||||
local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
|
||||
dlurl="${config[global_repo_url]}/download/${package}/${version}"
|
||||
zrep_download_package "${zipFile}" "${dlurl}"
|
||||
local dlurl="${config[global_repo_url]}/download/${package}/${version}"
|
||||
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]}"
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
zrep_msg debug "\nError: Failed to unzip the package."
|
||||
return 1
|
||||
|
Loading…
Reference in New Issue
Block a user