Compare commits

..

No commits in common. "main" and "v0.0.6" have entirely different histories.
main ... v0.0.6

2 changed files with 176 additions and 238 deletions

View File

@ -1,39 +1,31 @@
<img src="zrep-logo.png" width="150" align="left"> <img src="zrep-logo.png" width="150" align="left">
# Zrep Package Installer (zpi) # Zsh Repository Tool (zrep)
zpi is a powerful and user-friendly package manager for the Zsh shell. It allows you to easily install, manage, and share Zsh packages, such as plugins, themes, and scripts, directly from a central repository. zrep is a powerful and user-friendly package manager for the Zsh shell. It allows you to easily install, manage, and share Zsh packages, such as plugins, themes, and scripts, directly from a central repository.
## What is it?
`zpi` in and of itself is the command line tool that you use to install and manage scripts that are placed in `$fpath` used by Zsh. The packages are available on [the zrep website](https://zrep.kekepower.com) where you can, if you want to share your own scripts, register for an account. By sharing your scripts with the community, you are probably making somebody's day easier.
You can read more in the Wiki.
## Key Features ## Key Features
- **Easy Installation**: zpi simplifies the process of installing Zsh packages. With a single command, you can install packages from the zrep repository. - **Easy Installation**: zrep simplifies the process of installing Zsh packages. With a single command, you can install packages from the zrep repository.
- **Package Management**: zpi provides a set of commands to manage your installed packages. You can list, enable, disable, update, and remove packages effortlessly. - **Package Management**: zrep provides a set of commands to manage your installed packages. You can list, enable, disable, update, and remove packages effortlessly.
- **Centralized Repository**: All packages are stored in a central repository, making it convenient to discover and share Zsh packages with the community. - **Centralized Repository**: All packages are stored in a central repository, making it convenient to discover and share Zsh packages with the community.
- **Automatic Setup**: zpi automatically sets up the necessary configuration files and directories, ensuring a smooth integration with your Zsh environment. - **Automatic Setup**: zrep automatically sets up the necessary configuration files and directories, ensuring a smooth integration with your Zsh environment.
- **Customizable**: zpi allows you to customize the installation directory and other settings through a configuration file. - **Customizable**: zrep allows you to customize the installation directory and other settings through a configuration file.
## Getting Started ## Getting Started
To start using zpi, simply run the `zpi init` command to initialize the tool. zpi will guide you through the setup process and create the required configuration files. To start using zrep, simply run the `zrep init` command to initialize the tool. zrep will guide you through the setup process and create the required configuration files.
Use the search function on **[the zrep website](https://zrep.kekepower.com)** to find useful scripts until a search function is added to the `zpi` script some time in the future. Once initialized, you can explore and install packages using commands like `zrep install <author/package>`, `zrep list`, and `zrep enable <author/package>`.
Once initialized, you can explore and install packages using commands like `zpi install <author/package>`, `zpi list`, and `zpi enable <author/package>`.
## Contributing ## Contributing
zpi is an open-source project, and contributions are welcome! If you have any ideas, bug reports, or feature requests, please open an issue on the GitHub repository. zrep is an open-source project, and contributions are welcome! If you have any ideas, bug reports, or feature requests, please open an issue on the GitHub repository.
## License ## License
zpi is released under the MIT License. zrep is released under the MIT License.

View File

@ -2,17 +2,13 @@
setopt extendedglob setopt extendedglob
VERSION="0.0.9" # Sat-2024-04-06 VERSION="0.0.6" # Wed-2024-04-03
ZREP="Zrep Package Installer" ZREP="Zsh Repository Tool"
# Define the default path to .zreprc # Define the default path to .zreprc
ZREP_CONFIG="${HOME}/.zreprc" ZREP_CONFIG="${HOME}/.zreprc"
function zrep_fpath() { function zrep_fpath() {
local base_dir="${1}" local base_dir="${1}"
local mode="${2:-generic}" # Default mode is 'generic'
# Ensure globbing finds dotfiles and nullglob avoids empty directory issues
setopt local_options dotglob nullglob
# Check if the base directory exists # Check if the base directory exists
if [[ ! -d "${base_dir}" ]]; then if [[ ! -d "${base_dir}" ]]; then
@ -20,28 +16,12 @@ function zrep_fpath() {
return 1 return 1
fi fi
if [[ "${mode}" == "zrep_load" ]]; then # Add directories containing at least one file to fpath
# Specific mode for first_letter/author/script structure
for one_char_dir in ${base_dir}/?; do
[[ -d "${one_char_dir}" ]] || continue
for script_dir in ${one_char_dir}/*/*(/); do
local script_name=$(basename "${script_dir}")
local matching_files=("${script_dir}/${script_name}".*)
if (( ${#matching_files} )); then
fpath+=("${script_dir}")
fi
done
done
else
# Generic mode for any directory containing at least one file
for dir in ${base_dir}/**/*(/N); do for dir in ${base_dir}/**/*(/N); do
if [[ -n $(ls -A "${dir}/") ]]; then if [[ -n $(ls -A "${dir}/") ]]; then
fpath+=("${dir}") fpath=(${dir} $fpath)
fi fi
done done
fi
} }
if [[ ${1} != "init" ]]; then if [[ ${1} != "init" ]]; then
@ -84,13 +64,13 @@ typeset -A base_colors=(
# Define the global associative array to hold the current theme # Define the global associative array to hold the current theme
declare -A current_theme declare -A current_theme
function zrep_load_theme() { zrep_load_theme() {
local theme_name="$1" local theme_name="$1"
local theme_file="/home/stig/.zrep/themes/${theme_name}" local theme_file="${config[main_zrep_install_dir]}/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="/home/stig/.zrep/themes/classic" theme_file="${config[main_zrep_install_dir]}/themes/classic"
fi fi
# Source the theme file, which should define 'theme_colors' # Source the theme file, which should define 'theme_colors'
@ -109,7 +89,7 @@ function zrep_main_version_string() {
function zrep_version() { function zrep_version() {
zrep_msg info "\nCreated by kekePower - 2024" zrep_msg info "\nCreated by kekePower - 2024"
zrep_msg info "License: MIT" zrep_msg info "License: MIT"
zrep_msg info "https://git.kekepower.com/kekePower/zpi/" zrep_msg info "https://git.kekepower.com/kekePower/zrep/"
zrep_msg info "Please see '${base_colors[${current_theme[help]}]}${ZSH_SCRIPT:t} help${base_colors[end]}${base_colors[${current_theme[info]}]}' for more info${base_colors[end]}" zrep_msg info "Please see '${base_colors[${current_theme[help]}]}${ZSH_SCRIPT:t} help${base_colors[end]}${base_colors[${current_theme[info]}]}' for more info${base_colors[end]}"
exit exit
} }
@ -135,65 +115,6 @@ function zrep_msg() {
printf "%b\n" "${color}${message}${base_colors[end]}" printf "%b\n" "${color}${message}${base_colors[end]}"
} }
#################################################################################################
# Function to URL-encode strings in Zsh
function zrep_search_url_encode() {
local string="${1}"
local strlen=${#string}
local encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
# This will output the URL-encoded string
}
# Function to perform a search query and process JSON response
function zrep_search() {
local searchTerm="${@}"
local encodedSearch=$(zrep_search_url_encode "${searchTerm}")
# Use the global_repo_url from the config associative array
local response=$(curl -s -A "${ZSH_SCRIPT:t} ${VERSION} (curl)" "${config[global_repo_url]}/find.php?s=${encodedSearch}")
# Determine if the JSON response is an object indicating "No scripts found"
local jsonType=$(echo "$response" | jq -r 'type')
zrep_msg std "\nSearch results:"
if [[ "$jsonType" == "object" ]]; then
# Assuming an object type indicates a message field exists
local message=$(echo "$response" | jq -r '.message')
echo "$message"
return 0
elif [[ "$jsonType" == "array" ]]; then
# It's an array, process each item
echo "$response" | jq -c '.[]' | while IFS= read -r line; do
local script=$(echo "$line" | jq -r '.script')
local description=$(echo "$line" | jq -r '.description')
local url=$(echo "$line" | jq -r '.url')
# You can process these variables further as needed
zrep_msg other " * $script"
zrep_msg sub " - Description: $description"
zrep_msg sub " - $url"
# echo "-------------------------------------"
done
else
echo "Unexpected JSON format."
return 1
fi
}
############################################################################################
# Function to check if a given string exists in ~/.zshrc # Function to check if a given string exists in ~/.zshrc
function zrep_find_string() { function zrep_find_string() {
local searchString="$1" local searchString="$1"
@ -234,7 +155,7 @@ function zrep_init() {
if [[ ! -f ${ZREP_CONFIG} ]]; then if [[ ! -f ${ZREP_CONFIG} ]]; then
echo "${ZREP_CONFIG} not found. Creating it..." echo "${ZREP_CONFIG} not found. Creating it..."
# Prompt user for install directory # Prompt user for install directory
read "install_dir?Enter zpi installation directory [${HOME}/.zrep]: " read "install_dir?Enter zrep installation directory [${HOME}/.zrep]: "
install_dir=${install_dir:-"${HOME}/.zrep"} install_dir=${install_dir:-"${HOME}/.zrep"}
mkdir -p ${install_dir} mkdir -p ${install_dir}
read "downloader?Choose command to download packages [curl, wget, wget2]: " read "downloader?Choose command to download packages [curl, wget, wget2]: "
@ -297,7 +218,7 @@ if [[ -n \${addons[@]} ]]; then
autoload -Uz \$(basename \${addon}) autoload -Uz \$(basename \${addon})
done done
else else
echo "zpi: No addons enabled." echo "zrep: No addons enabled."
fi fi
EOF EOF
echo "File .zrep_addons created and configured." echo "File .zrep_addons created and configured."
@ -308,12 +229,12 @@ EOF
if [[ ! -d ${install_dir}/themes ]]; then if [[ ! -d ${install_dir}/themes ]]; then
echo "Installing the Classic theme to ${install_dir}/themes" echo "Installing the Classic theme to ${install_dir}/themes"
mkdir -p ${install_dir}/themes mkdir -p ${install_dir}/themes
zrep_global_downloader https://git.kekepower.com/kekePower/zpi/raw/branch/main/themes/classic -o ${install_dir}/themes/classic zrep_global_downloader https://git.kekepower.com/kekePower/zrep/raw/branch/main/themes/classic -o ${install_dir}/themes/classic
fi fi
echo "zpi initialization complete." echo "zrep initialization complete."
echo "You should copy 'zpi' to a path in you 'PATH' so that it's accessible." echo "You should copy 'zrep' to a path in you 'PATH' so that it's accessible."
echo "For example '${HOME}/bin'" echo "For example '${HOME}/bin'"
echo "Remember to 'source ${zshrc_file}' to load the 'zpi' settings." echo "Remember to 'source ${zshrc_file}' to load the 'zrep' settings."
} }
function zrep_installed_json() { function zrep_installed_json() {
@ -332,7 +253,6 @@ 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}"
@ -340,21 +260,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
dlurl=$(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) | .dlurl')
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 "$dlurl" && -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
@ -382,7 +302,7 @@ 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 "\n - Package '$author/$script' version $version installed/updated successfully." zrep_msg info " - Package '$script' by '$author' version $version installed/updated successfully."
} }
# Function to list installed packages from installed.json # Function to list installed packages from installed.json
@ -436,20 +356,41 @@ function zrep_list_package() {
fi fi
} }
# Function to load configuration
function zrep_load_config() { function zrep_load_config() {
if [[ ! -x $(which jq) ]]; then # Check if jq is available
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' is not installed. Please install jq to continue." echo "Error: 'jq' is not installed. Please install jq to continue."
exit 1 exit 1
fi fi
if [[ -f "${ZREP_CONFIG}" ]]; then if [[ -f "${ZREP_CONFIG}" ]]; then
zini "${ZREP_CONFIG}" zini "${ZREP_CONFIG}"
zrep_fpath ${config[main_zrep_install_dir]} zrep_load zrep_fpath ${config[main_zrep_install_dir]}
else else
echo "${ZREP_CONFIG} not found. Run 'zpi init' to set up." if [[ "${1}" == "init" ]]; then
echo "\nWelcome to zrep. Looks like this is the first time you are"
echo "running me."
read "response?Are you ready to set up zrep? (y/n): "
if [[ "${response}" =~ ^[Yy]$ ]]; then
zrep_init
else
echo "Setup aborted. Try again later."
fi
else
# echo "${ZREP_CONFIG} not found."
# Ask the user if they want to run 'zrep init'
echo "\nWelcome to zrep. I was unable to find the config file."
read "response?Would you like to run 'zrep init' to set up? (y/n): "
if [[ "${response}" =~ ^[Yy]$ ]]; then
zrep_init
else
echo "Initialization canceled. Please run 'zrep init' manually to set up."
exit 1 exit 1
fi fi
fi
fi
} }
@ -466,18 +407,17 @@ function zrep_remove_package() {
if [[ -z "$installed_version" || "$installed_version" == "null" ]]; then if [[ -z "$installed_version" || "$installed_version" == "null" ]]; then
zrep_msg debug "\nError: Package '${package_name}' is not installed." zrep_msg debug "\nError: Package '${package_name}' is not installed."
zrep_msg info "Please see 'zpi list' for installed packages." zrep_msg info "Please see 'zrep list' for installed packages."
return 1 return 1
fi fi
local first_letter="${author:0:1:l}" local first_letter="${author:0:1:l}"
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}" local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
zrep_msg std "\nFound installed package: $package_name, version: $installed_version" zrep_msg info "\nFound installed package: $package_name, version: $installed_version"
# Ask user for confirmation with default response "Y" # Ask user for confirmation with default response "Y"
zrep_msg info "Are you sure you want to remove this package? (y/n) [Y]: \c" read "REPLY?Are you sure you want to remove this package? (y/n) [Y]: "
read REPLY
REPLY=${REPLY:-Y} REPLY=${REPLY:-Y}
echo echo
@ -485,6 +425,7 @@ function zrep_remove_package() {
# Remove the package directory from disk # Remove the package directory from disk
if [[ -d "${package_dir}" ]]; then if [[ -d "${package_dir}" ]]; then
rm -rf "${package_dir}" rm -rf "${package_dir}"
zrep_msg sub "Package directory '${package_dir}' removed successfully."
else else
zrep_msg debug "Warning: Package directory '${package_dir}' not found." zrep_msg debug "Warning: Package directory '${package_dir}' not found."
fi fi
@ -495,7 +436,7 @@ function zrep_remove_package() {
if .[$author] == [] then del(.[$author]) else . end' \ if .[$author] == [] then del(.[$author]) else . end' \
"$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json" "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json"
zrep_msg sub "Package '${package_name}' successfully removed." zrep_msg sub "Package '${package_name}' removed successfully from installed.json."
else else
zrep_msg info "Removal canceled." zrep_msg info "Removal canceled."
fi fi
@ -517,85 +458,37 @@ function zrep_check_if_installed() {
if [[ -n "${installed_version}" && "${installed_version}" != "null" ]]; then if [[ -n "${installed_version}" && "${installed_version}" != "null" ]]; then
# Package is installed, and version is stored in installed_version # Package is installed, and version is stored in installed_version
# echo "Package $package is installed with version $installed_version."
return 0 # Package is installed return 0 # Package is installed
else else
# echo "Package $package is not installed."
return 1 # Package is not installed return 1 # Package is not installed
fi fi
} }
function zrep_global_downloader() { 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 case ${config[global_downloader]} in
curl) curl)
if [[ -n $outputFile ]]; then dloader="curl -s -A \"zrep ${VERSION} (curl)\""
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|wget2) wget)
if [[ -n $outputFile ]]; then dloader="wget -q -U \"zrep ${VERSION} (wget)\""
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" -O \"$outputFile\" \"$downloadURL\"" ;;
else wget2)
cmd="${config[global_downloader]} -q -L -U \"${ZSH_SCRIPT:t} ${VERSION} (${config[global_downloader]})\" \"$downloadURL\"" dloader="wget2 -q -U \"zrep ${VERSION} (wget2)\""
fi
;; ;;
*) *)
echo "Unsupported downloader." echo "Invalid Downloader."
return 1 exit
;; ;;
esac 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() {
for package in ${(k)updatesAvailable}; do
local updateDetails=(${(s/|/)updatesAvailable[${package}]})
local author=${updateDetails[1]}
local script=${updateDetails[2]}
local version=${updateDetails[3]}
echo "Updating ${author}/${script} to version ${version}..."
zrep_install_package u "${author}/${script}" "${version}"
done
} }
typeset -A updatesAvailable typeset -A updatesAvailable
function zrep_check_for_updates() { function zrep_check_for_updates() {
# Now using a zrep API to fetch the JSON for checking for updates. This _may_ change in the local remoteFile="${config[global_repo_url]}/packages.json\?$(date +%s)"
# future if the load on the DB gets too high.
local remoteFile="${config[global_repo_url]}/getver.php"
# 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}")
@ -617,9 +510,7 @@ function zrep_check_for_updates() {
'.authors[] | select(.name==$author) | .scripts[] | select(.name==$script) | .version' <<<"$remotePackages") '.authors[] | select(.name==$author) | .scripts[] | select(.name==$script) | .version' <<<"$remotePackages")
if [[ "${remote_version}" > "${installed_version}" ]]; then if [[ "${remote_version}" > "${installed_version}" ]]; then
# updatesAvailable[${author}/${script}]="${remote_version}" updatesAvailable[${author}/${script}]="${remote_version}"
# Store author, script, and version in a single string, separated by "|"
updatesAvailable[${author}/${script}]="${author}|${script}|${remote_version}"
zrep_msg info "\n${author}/${script} can be updated from ${installed_version} to ${remote_version}" zrep_msg info "\n${author}/${script} can be updated from ${installed_version} to ${remote_version}"
updates=true # Mark that updates are available updates=true # Mark that updates are available
fi fi
@ -642,6 +533,8 @@ function zrep_update_package() {
if [[ -n "$version" ]]; then if [[ -n "$version" ]]; then
local author="${specificPackage%%/*}" local author="${specificPackage%%/*}"
local script="${specificPackage#*/}" local script="${specificPackage#*/}"
# local dlurl="${config[global_repo_url]}/download.php?a=${author}&s=${script}&v=${version}"
# echo "Updating $specificPackage to version $version..."
local install_pkg="${author}/${script}" local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg} zrep_install_package u ${install_pkg}
else else
@ -650,8 +543,8 @@ function zrep_update_package() {
else else
if [[ ${updates} == "true" ]]; then if [[ ${updates} == "true" ]]; then
# Prompt the user only if updates are available # Prompt the user only if updates are available
zrep_msg sub "New updates are available. Do you want to proceed with updating? (Y/n): \c" echo "New updates are available. Do you want to proceed with updating? (Y/n): "
read "response" read -q "response?"
echo # Move to a new line echo # Move to a new line
# Set the default response to 'Y' if no input is entered # Set the default response to 'Y' if no input is entered
@ -659,8 +552,17 @@ function zrep_update_package() {
# Proceed with update if the response is 'Y' or 'y' # Proceed with update if the response is 'Y' or 'y'
if [[ $response =~ ^[Yy]$ ]]; then if [[ $response =~ ^[Yy]$ ]]; then
# Call zrep_process_updates to handle all updates. # General update mode: update all packages listed in updatesAvailable
zrep_process_updates 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 else
zrep_msg info "Update canceled." zrep_msg info "Update canceled."
fi fi
@ -668,21 +570,75 @@ function zrep_update_package() {
fi 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() { function zrep_download_package() {
local ZipFile="${1}" local ZipFile="${1}"
local DownloadURL="${2}" local DownloadURL="${2}"
local retries=5
local delay=5
local attempt=1
local downloader=""
local http_status
local cmd
local exit_status
# Now simply call the unified downloader function case "${config[global_downloader]}" in
if zrep_global_downloader "$DownloadURL" "$ZipFile"; then curl)
zrep_msg std "\nDownload successful." downloader="curl"
else cmd="curl -L -A \"zrep ${VERSION} (curl)\" -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\""
zrep_msg debug "\nDownload failed." ;;
wget)
downloader="wget"
cmd="wget -L -U \"zrep ${VERSION} (wget)\" -q -O \"$ZipFile\" \"$DownloadURL\""
;;
wget2)
downloader="wget2"
cmd="wget2 -L -U \"zrep ${VERSION} (wget2)\" -q -O \"$ZipFile\" \"$DownloadURL\""
;;
*)
zrep_msg debug "Unsupported or unspecified downloader: '${config[global_downloader]}'."
return 1 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 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
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 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
@ -700,28 +656,23 @@ function zrep_install_package() {
# 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 debug "\nPackage ${package} is already installed." zrep_msg debug "\nPackage ${package} is already installed."
zrep_msg info "Use 'zpi list' to see installed packages." zrep_msg info "Use 'zrep list' to see installed packages."
return 0 return 0
fi fi
zrep_parse_remote "${config[global_repo_url]}/getver.php?p=${package}" ${package}
fi fi
# If not installed, proceed with fetching the package information
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"
mkdir -p "${tmpDir}" mkdir -p "${tmpDir}"
author="${package%/*}"
script="${package#*/}"
local zipFile="${tmpDir}/${author}-${script}-${version}.zip" local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
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
@ -838,27 +789,26 @@ function zrep_disable() {
echo ")" echo ")"
} > ${config[main_zrep_install_dir]}/.addons } > ${config[main_zrep_install_dir]}/.addons
zrep_msg info "\nPackage '${package_name}' has been disabled and removed from fpath." zrep_msg info "\nPackage '${package_name} (${script})' has been disabled and removed from fpath."
zrep_msg info "You may have to run 'source ~/.zrep_addons' to remove it from your shell." zrep_msg info "You may have to run 'source ~/.zrep_addons' to remove it from your shell."
} }
# Help function to display available options # Help function to display available options
function zrep_help() { function zrep_help() {
zrep_msg sub "\nUsage: ${ZSH_SCRIPT:t} <command> [arguments]" zrep_msg sub "\nUsage: zrep <command> [arguments]"
zrep_msg info "Available commands:" zrep_msg info "Available commands:"
if [[ ! -f ${ZREP_CONFIG} ]]; then if [[ ! -f ${ZREP_CONFIG} ]]; then
zrep_msg info " init: Initialize zpi" zrep_msg info " init: Initialize zrep"
fi fi
zrep_msg info " install (i) <author/package>:\t\t\tInstall a package" zrep_msg info " install (i) <author/package>:\t\t\tInstall a package"
zrep_msg info " remove (rm, delete, del) <author/package>:\tRemove a package" zrep_msg info " remove (rm, delete, del) <author/package>:\tRemove a package"
zrep_msg info " update (u) <author/package>:\t\t\tUpdate zpi package" zrep_msg info " update (u) <author/package>:\t\t\tUpdate zrep package"
zrep_msg info " enable <author/package>:\t\t\tEnable zpi package" zrep_msg info " enable <author/package>:\t\t\tEnable zrep package"
zrep_msg info " disable <author/package>:\t\t\tDisable zpi package" zrep_msg info " disable <author/package>:\t\t\tDisable zrep package"
zrep_msg info " search 'search term':\t\t\t\tSearch for authors, packages or description" zrep_msg info " version:\t\t\t\t\tDisplay zrep version"
zrep_msg info " version:\t\t\t\t\tDisplay zpi version"
zrep_msg info " list:\t\t\t\t\t\tList installed packages" zrep_msg info " list:\t\t\t\t\t\tList installed packages"
zrep_msg info " <author/package> help:\t\t\tDisplay help for package (if available)" zrep_msg info " <author/package> help:\t\t\tDisplay help for pacakage (if available)"
} }
@ -911,7 +861,6 @@ function zrep_read_usage() {
function main() { function main() {
zrep_main_version_string zrep_main_version_string
if [[ ${1} != "init" ]]; then if [[ ${1} != "init" ]]; then
zrep_load_config ${1} zrep_load_config ${1}
zrep_load_theme ${config[global_theme]} zrep_load_theme ${config[global_theme]}
@ -930,9 +879,6 @@ function main() {
zrep_check_for_deps zrep_check_for_deps
exit exit
;; ;;
search | s | find)
zrep_search "${@:2}"
;;
install | i) install | i)
zrep_install_package ${2} zrep_install_package ${2}
;; ;;
@ -940,7 +886,7 @@ function main() {
# Parse the command argument to extract the package name # Parse the command argument to extract the package name
zrep_remove_package_name="${2:-}" zrep_remove_package_name="${2:-}"
if [[ -z "${zrep_remove_package_name}" ]]; then if [[ -z "${zrep_remove_package_name}" ]]; then
zrep_msg info "\nUsage: zpi ${1} author/package" zrep_msg info "\nUsage: zrep ${1} author/package"
else else
zrep_remove_package "${zrep_remove_package_name}" zrep_remove_package "${zrep_remove_package_name}"
fi fi