2024-02-27 18:39:09 +01:00
#!/usr/bin/zsh
2024-03-04 22:17:21 +01:00
setopt extendedglob
2024-04-05 00:36:24 +02:00
VERSION="0.0.7" # Fri-2024-04-05
2024-02-27 18:39:09 +01:00
ZREP="Zsh Repository Tool"
2024-03-13 22:52:47 +01:00
# Define the default path to .zreprc
2024-03-10 13:31:19 +01:00
ZREP_CONFIG="${HOME}/.zreprc"
2024-02-27 16:50:07 +01:00
function zrep_fpath() {
2024-03-10 13:31:19 +01:00
local base_dir="${1}"
2024-04-05 00:36:24 +02:00
local mode="${2:-generic}" # Default mode is 'generic'
# Ensure globbing finds dotfiles and nullglob avoids empty directory issues
setopt local_options dotglob nullglob
2024-02-27 16:50:07 +01:00
# Check if the base directory exists
2024-03-10 13:31:19 +01:00
if [[ ! -d "${base_dir}" ]]; then
echo "Error: Base directory '${base_dir}' does not exist."
2024-02-27 16:50:07 +01:00
return 1
fi
2024-04-05 00:36:24 +02:00
if [[ "${mode}" == "zrep_load" ]]; then
# 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
if [[ -n $(ls -A "${dir}/") ]]; then
fpath+=("${dir}")
fi
done
fi
2024-02-27 16:50:07 +01:00
}
2024-03-15 22:35:41 +01:00
if [[ ${1} != "init" ]]; then
zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini
fi
2024-03-13 22:52:47 +01:00
# Define a list of colors available to use in themes
2024-03-12 23:52:10 +01:00
typeset -A base_colors=(
[green]="\033[0;32m"
[yellow]="\033[1;33m"
[red]="\033[0;31m"
[bold_yellow]="\033[1;33m"
[magenta]="\033[0;35m"
[white]="\033[1;37m"
[green_bg]="\033[42m"
[white_on_green]="\033[1;37m\033[42m" # Combined color
[end]="\033[0m"
[black]="\033[0;30m"
[blue]="\033[0;34m"
[cyan]="\033[0;36m"
[bold_black]="\033[1;30m"
[bold_red]="\033[1;31m"
[bold_green]="\033[1;32m"
[bold_blue]="\033[1;34m"
[bold_magenta]="\033[1;35m"
[bold_cyan]="\033[1;36m"
[bold_white]="\033[1;37m"
[black_bg]="\033[40m"
[red_bg]="\033[41m"
[yellow_bg]="\033[43m"
[blue_bg]="\033[44m"
[magenta_bg]="\033[45m"
[cyan_bg]="\033[46m"
[white_bg]="\033[47m"
[underline]="\033[4m"
[italic]="\033[3m"
2024-03-06 18:33:14 +01:00
)
2024-02-28 23:41:09 +01:00
2024-03-12 23:52:10 +01:00
# Define the global associative array to hold the current theme
declare -A current_theme
zrep_load_theme() {
local theme_name="$1"
local theme_file="${config[main_zrep_install_dir]}/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"
fi
# Source the theme file, which should define 'theme_colors'
source "$theme_file"
# Copy 'theme_colors' to 'current_theme'
for key value in ${(kv)theme_colors}; do
current_theme[$key]="$value"
done
}
2024-03-04 22:36:49 +01:00
function zrep_main_version_string() {
2024-03-12 23:52:10 +01:00
echo "${base_colors[bold_black]}${base_colors[white_bg]} ${ZREP} ${base_colors[end]}${base_colors[bold_white]}${base_colors[black_bg]} ${VERSION} ${base_colors[end]}"
2024-03-04 22:36:49 +01:00
}
2024-02-28 23:41:09 +01:00
function zrep_version() {
2024-03-06 18:33:14 +01:00
zrep_msg info "\nCreated by kekePower - 2024"
zrep_msg info "License: MIT"
zrep_msg info "https://git.kekepower.com/kekePower/zrep/"
2024-03-12 23:52:10 +01:00
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]}"
2024-02-28 23:41:09 +01:00
exit
}
2024-03-13 22:52:47 +01:00
# This function is used to display messages and use colors
# from the loaded theme.
2024-02-28 23:41:09 +01:00
function zrep_msg() {
2024-03-12 23:52:10 +01:00
local msg_type="$1"
local message="$2"
2024-03-13 22:52:47 +01:00
local color="${base_colors[end]}" # Default color is NONE
2024-03-12 23:52:10 +01:00
# Retrieve the color key from the current theme
local theme_color_key="${current_theme[$msg_type]}"
# Check if a valid color was found based on the key
if [[ -n "${base_colors[$theme_color_key]}" ]]; then
color="${base_colors[$theme_color_key]}"
else
# Handle invalid theme color key if needed
echo "Warning: Theme color key '$theme_color_key' not found. Using default." >&2
fi
printf "%b\n" "${color}${message}${base_colors[end]}"
2024-02-28 23:41:09 +01:00
}
2024-04-05 00:36:24 +02:00
#################################################################################################
# Function to URL-encode strings in Zsh
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
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 "zrep ${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
}
############################################################################################
2024-03-13 22:52:47 +01:00
# Function to check if a given string exists in ~/.zshrc
function zrep_find_string() {
local searchString="$1"
local found=0
while IFS= read -r line || [[ -n $line ]]; do
if [[ "$line" == *"$searchString"* ]]; then
found=1
break
fi
done < "${HOME}/.zshrc"
echo $found
}
function zrep_check_for_deps() {
# Array of required external programs
2024-03-15 22:10:39 +01:00
local required_programs=('jq' "${config[global_downloader]}")
2024-03-13 22:52:47 +01:00
# Iterate over the array
for program in "${required_programs[@]}"; do
# Check if the program is available in the system's PATH
if ! command -v "$program" &> /dev/null; then
# Program not found, display a message
echo "Required program not found: $program"
fi
done
}
# The first step after downloading zrep.
# Here we create the default file and directory structure
2024-02-28 23:41:09 +01:00
function zrep_init() {
2024-03-10 13:31:19 +01:00
local zshrc_file="${HOME}/.zshrc"
2024-03-13 22:52:47 +01:00
local zrep_addons="${HOME}/.zrep_addons"
2024-02-28 23:41:09 +01:00
local install_dir
# Check if .zreprc exists
2024-03-10 13:31:19 +01:00
if [[ ! -f ${ZREP_CONFIG} ]]; then
echo "${ZREP_CONFIG} not found. Creating it..."
2024-02-28 23:41:09 +01:00
# Prompt user for install directory
2024-03-10 13:31:19 +01:00
read "install_dir?Enter zrep installation directory [${HOME}/.zrep]: "
install_dir=${install_dir:-"${HOME}/.zrep"}
2024-03-15 23:58:20 +01:00
mkdir -p ${install_dir}
2024-03-15 22:10:39 +01:00
read "downloader?Choose command to download packages [curl, wget, wget2]: "
2024-03-15 22:19:23 +01:00
if [[ ${downloader} != curl && ${downloader} != wget && ${downloader} != wget2 ]]; then
2024-03-15 22:10:39 +01:00
echo "Invalid choice: '$downloader'. Try again."
read "downloader?Choose command to download packages [curl, wget, wget2] "
2024-03-15 22:19:23 +01:00
if [[ ${downloader} != curl && ${downloader} != wget && ${downloader} != wget2 ]]; then
2024-03-15 22:10:39 +01:00
echo "Invalid choice: '$downloader'. Exiting."
exit
fi
fi
2024-02-28 23:41:09 +01:00
# Write to .zreprc
2024-03-12 14:58:13 +01:00
cat > "${ZREP_CONFIG}" <<EOF
2024-03-13 22:52:47 +01:00
[main]
zrep_install_dir = ${install_dir}
2024-03-12 14:58:13 +01:00
2024-03-13 22:52:47 +01:00
[global]
2024-04-03 13:57:43 +02:00
repo_url = https://zrep.kekepower.com
2024-03-13 22:52:47 +01:00
theme = classic
2024-03-15 22:10:39 +01:00
downloader = ${downloader}
2024-03-12 14:58:13 +01:00
EOF
2024-03-15 22:10:39 +01:00
echo "The file '${ZREP_CONFIG}' has been created."
2024-02-28 23:41:09 +01:00
else
2024-03-15 22:16:33 +01:00
zrep_fpath ${HOME}/.zrep/functions
autoload -Uz zini
2024-03-10 13:31:19 +01:00
echo "Loading configuration from ${ZREP_CONFIG}"
zini ${ZREP_CONFIG}
2024-03-16 00:06:08 +01:00
# install_dir=${config[main_zrep_install_dir]}
2024-02-28 23:41:09 +01:00
fi
2024-03-13 22:52:47 +01:00
if [[ $(zrep_find_string zini) -eq 0 ]]; then
mkdir -p "${install_dir}/functions/zini"
2024-03-17 09:04:23 +01:00
zrep_global_downloader https://raw.githubusercontent.com/kekePower/zini/main/zini -o "${install_dir}/functions/zini/zini"
2024-03-12 14:58:13 +01:00
echo "Adding 'zini' path to fpath in ${zshrc_file}"
echo "fpath=(${install_dir}/functions/zini \$fpath)" >> ${zshrc_file}
2024-03-15 23:58:20 +01:00
autoload -Uz zini
2024-03-12 14:58:13 +01:00
fi
2024-02-28 23:41:09 +01:00
2024-03-09 00:36:09 +01:00
# Check if .zshrc already sources .zrep_addons, if not, add it
2024-03-13 22:52:47 +01:00
if [[ $(zrep_find_string "source ${zrep_addons}") -eq 0 ]]; then
2024-03-09 00:36:09 +01:00
echo "Adding source command for .zrep_addons to .zshrc..."
2024-03-13 22:52:47 +01:00
echo "source ${zrep_addons}" >> "${zshrc_file}"
2024-03-09 00:36:09 +01:00
fi
# Create or update the .zrep_addons file
2024-03-13 22:52:47 +01:00
if [[ ! -f ${zrep_addons} ]]; then
echo "Creating file ${zrep_addons}..."
2024-03-15 23:58:20 +01:00
touch ${install_dir}/.addons
2024-03-13 22:52:47 +01:00
cat > "${zrep_addons}" <<EOF
2024-03-09 00:36:09 +01:00
# Source the .addons file from the zrep installation directory
source "${install_dir}/.addons"
# If addons array is defined and not empty, add its elements to fpath
if [[ -n \${addons[@]} ]]; then
for addon in "\${addons[@]}"; do
2024-03-10 13:31:19 +01:00
if [[ -d \${addon} ]] && [[ ! " \${fpath[*]} " =~ " \${addon} " ]]; then
fpath=(\${addon} "\${fpath[@]}") # Prepend the new addon to fpath
2024-02-28 23:41:09 +01:00
fi
2024-03-15 23:14:47 +01:00
autoload -Uz \$(basename \${addon})
2024-03-10 10:02:08 +01:00
done
2024-03-09 00:36:09 +01:00
else
echo "zrep: No addons enabled."
fi
EOF
echo "File .zrep_addons created and configured."
2024-02-28 23:41:09 +01:00
else
2024-03-09 00:36:09 +01:00
echo "File .zrep_addons already exists. Review manually if update is needed."
2024-02-28 23:41:09 +01:00
fi
2024-03-15 23:58:20 +01:00
if [[ ! -d ${install_dir}/themes ]]; then
2024-03-16 00:06:08 +01:00
echo "Installing the Classic theme to ${install_dir}/themes"
2024-03-15 23:58:20 +01:00
mkdir -p ${install_dir}/themes
2024-03-17 09:04:23 +01:00
zrep_global_downloader https://git.kekepower.com/kekePower/zrep/raw/branch/main/themes/classic -o ${install_dir}/themes/classic
2024-03-15 23:58:20 +01:00
fi
2024-02-28 23:41:09 +01:00
echo "zrep initialization complete."
2024-04-03 13:57:43 +02:00
echo "You should copy 'zrep' to a path in you 'PATH' so that it's accessible."
echo "For example '${HOME}/bin'"
2024-03-12 14:58:13 +01:00
echo "Remember to 'source ${zshrc_file}' to load the 'zrep' settings."
2024-02-28 23:41:09 +01:00
}
2024-03-12 18:06:06 +01:00
function zrep_installed_json() {
# Check if installed.json exists
if [[ ! -f "${config[main_zrep_install_dir]}/installed.json" ]]; then
2024-03-16 00:18:25 +01:00
touch ${config[main_zrep_install_dir]}/installed.json
2024-03-12 18:06:06 +01:00
zrep_msg debug "\nError: installed.json not found."
return 1
else
installed_json="${config[main_zrep_install_dir]}/installed.json"
2024-03-12 18:07:33 +01:00
export installed_json=${installed_json}
2024-03-12 18:06:06 +01:00
fi
}
2024-02-28 23:41:09 +01:00
# Function to parse remote JSON data and extract author, script, and version
2024-03-11 22:15:43 +01:00
# and return the correct download url
2024-02-28 23:41:09 +01:00
function zrep_parse_remote() {
2024-03-10 13:31:19 +01:00
local url="${1}"
2024-03-11 22:15:43 +01:00
local package="${2}"
local author_name="${package%%/*}"
local script_name="${package#*/}"
2024-02-28 23:41:09 +01:00
local json_data
2024-03-11 22:15:43 +01:00
2024-02-28 23:41:09 +01:00
# Fetch JSON data from the URL
2024-03-17 09:04:23 +01:00
json_data=$(zrep_global_downloader "${url}")
2024-02-28 23:41:09 +01:00
2024-03-11 22:15:43 +01:00
# 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
2024-04-05 00:36:24 +02:00
if [[ -n "$version" ]]; then
2024-03-11 22:15:43 +01:00
# Set the details as global
2024-04-05 00:36:24 +02:00
#export author="$author_name"
#export script="$script_name"
2024-03-11 22:15:43 +01:00
export version
2024-04-05 00:36:24 +02:00
#export dlurl
2024-03-11 22:15:43 +01:00
else
2024-03-16 00:29:32 +01:00
zrep_msg debug "\nPackage ${package} not found.\n"
2024-03-16 00:29:07 +01:00
exit 1
2024-03-11 22:15:43 +01:00
fi
2024-02-28 23:41:09 +01:00
}
# Function to write to installed.json after successful install
function zrep_update_installed_json() {
2024-03-10 13:31:19 +01:00
local author="${1}"
local script="${2}"
local version="${3}"
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-03-04 22:17:21 +01:00
2024-03-15 22:45:37 +01:00
# Ensure the JSON file exists and is not empty, creating an empty object if necessary
if [[ ! -f "${installed_json}" ]] || [[ ! -s "${installed_json}" ]]; then
2024-03-12 18:06:06 +01:00
echo "{}" > "${installed_json}"
2024-03-04 23:40:02 +01:00
fi
2024-03-01 12:22:39 +01:00
2024-03-15 22:45:37 +01:00
# Update or add the script entry
2024-03-11 22:15:43 +01:00
jq --arg author "$author" --arg script "$script" --arg version "$version" \
'if has($author) then
.[$author] |= map(if .script == $script then .version = $version else . end) |
if .[$author] | all(.script != $script) then .[$author] += [{"script": $script, "version": $version}] else . end
else
.[$author] = [{"script": $script, "version": $version}]
2024-03-12 18:06:06 +01:00
end' "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json"
2024-03-04 22:17:21 +01:00
2024-03-11 22:15:43 +01:00
zrep_msg info " - Package '$script' by '$author' version $version installed/updated successfully."
2024-02-28 23:41:09 +01:00
}
# Function to list installed packages from installed.json
function zrep_list_installed_packages() {
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-03-04 20:20:33 +01:00
2024-03-15 22:55:52 +01:00
# Check if installed.json exists and is not empty
if [[ ! -s "${installed_json}" ]]; then
2024-03-15 22:56:38 +01:00
zrep_msg info "\nNo packages installed."
2024-03-15 22:55:52 +01:00
return
fi
2024-02-28 23:41:09 +01:00
# Parse installed.json and list packages
2024-03-04 20:20:33 +01:00
zrep_msg sub "\nInstalled packages:"
2024-03-15 22:55:52 +01:00
# 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
2024-03-15 22:56:38 +01:00
zrep_msg info "\nNo packages installed."
2024-03-15 22:55:52 +01:00
return
fi
2024-03-04 23:40:02 +01:00
# Iterate through each author and their packages
2024-03-11 22:15:43 +01:00
jq -r 'to_entries | .[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "${installed_json}" | while IFS= read -r package_info; do
2024-03-10 13:31:19 +01:00
local package_name=$(echo "${package_info}" | cut -d ' ' -f1) # Extract package name before the version
2024-03-12 23:52:10 +01:00
local is_active="${base_colors[white]}(${base_colors[end]}${base_colors[bold_red]}Inactive${base_colors[end]}${base_colors[white]})${base_colors[end]}" # Set default to Inactive
2024-03-04 20:20:33 +01:00
2024-03-14 16:24:48 +01:00
zmodload zsh/regex
2024-03-04 20:20:33 +01:00
# Check if the package is active (only modify if active)
2024-03-14 16:24:48 +01:00
if [[ $(<${config[main_zrep_install_dir]}/.addons) =~ "\b${package_name}\b" ]]; then
2024-03-12 23:52:10 +01:00
is_active="${base_colors[white]}(${base_colors[end]}${base_colors[bold_green]}Active${base_colors[end]}${base_colors[white]})${base_colors[end]}"
2024-03-04 20:20:33 +01:00
fi
2024-03-14 16:24:48 +01:00
zrep_msg info " |> ${package_info} ${is_active}"
2024-03-04 23:40:02 +01:00
done
2024-02-28 23:41:09 +01:00
}
2024-02-27 16:50:07 +01:00
2024-02-29 16:53:22 +01:00
function zrep_list_package() {
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-02-29 16:53:22 +01:00
local package_names=""
2024-03-04 23:40:02 +01:00
# Parse installed.json and concatenate package names
2024-03-11 22:15:43 +01:00
jq -r 'to_entries[] | .key as $author | .value[] | "\($author)/\(.script) (\(.version))"' "${installed_json}" | while IFS= read -r package_info; do
2024-03-10 13:31:19 +01:00
package_names+="${package_info} "
2024-03-04 23:40:02 +01:00
done
2024-03-01 12:22:39 +01:00
2024-03-04 23:40:02 +01:00
# Assuming you want to print out the concatenated package names
2024-03-10 13:31:19 +01:00
if [[ -n "${package_names}" ]]; then
2024-03-13 07:25:59 +01:00
zrep_msg info "\nInstalled packages: ${package_names}"
2024-03-04 23:40:02 +01:00
else
2024-03-13 07:25:59 +01:00
zrep_msg debug "\nNo packages found."
2024-03-04 23:40:02 +01:00
fi
2024-02-29 16:53:22 +01:00
}
2024-03-09 00:36:09 +01:00
function zrep_load_config() {
2024-02-29 16:53:22 +01:00
2024-04-05 00:36:24 +02:00
if [[ ! -x $(which jq) ]]; then
2024-02-29 16:53:22 +01:00
echo "Error: 'jq' is not installed. Please install jq to continue."
exit 1
fi
2024-03-10 13:31:19 +01:00
if [[ -f "${ZREP_CONFIG}" ]]; then
zini "${ZREP_CONFIG}"
2024-04-05 00:36:24 +02:00
zrep_fpath ${config[main_zrep_install_dir]} zrep_load
2024-02-27 16:50:07 +01:00
else
2024-04-05 00:36:24 +02:00
echo "${ZREP_CONFIG} not found. Run 'zrep init' to set up."
exit 1
2024-02-27 16:50:07 +01:00
fi
2024-03-01 12:22:39 +01:00
2024-02-27 16:50:07 +01:00
}
2024-02-28 23:41:09 +01:00
function zrep_remove_package() {
2024-03-10 13:31:19 +01:00
local package_name="${1}"
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-02-28 23:41:09 +01:00
2024-03-11 22:15:43 +01:00
local author="${package_name%%/*}"
local script="${package_name#*/}"
2024-02-28 23:41:09 +01:00
2024-03-11 22:15:43 +01:00
# Verify if the package is installed and get its version (if any)
local installed_version=$(jq -r --arg author "$author" --arg script "$script" \
'.[$author][] | select(.script == $script) | .version' "$installed_json")
if [[ -z "$installed_version" || "$installed_version" == "null" ]]; then
zrep_msg debug "\nError: Package '${package_name}' is not installed."
zrep_msg info "Please see 'zrep list' for installed packages."
2024-02-28 23:41:09 +01:00
return 1
fi
2024-03-12 18:06:06 +01:00
local first_letter="${author:0:1:l}"
2024-03-10 13:31:19 +01:00
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
2024-02-29 16:53:22 +01:00
2024-04-05 00:36:24 +02:00
zrep_msg std "\nFound installed package: $package_name, version: $installed_version"
2024-02-28 23:41:09 +01:00
2024-02-29 16:53:22 +01:00
# Ask user for confirmation with default response "Y"
2024-04-05 00:36:24 +02:00
zrep_msg info "Are you sure you want to remove this package? (y/n) [Y]: \c"
read REPLY
2024-03-04 22:36:49 +01:00
REPLY=${REPLY:-Y}
2024-02-28 23:41:09 +01:00
echo
2024-02-29 16:53:22 +01:00
2024-03-10 13:31:19 +01:00
if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
2024-02-29 16:53:22 +01:00
# Remove the package directory from disk
2024-03-10 13:31:19 +01:00
if [[ -d "${package_dir}" ]]; then
rm -rf "${package_dir}"
2024-02-29 16:53:22 +01:00
else
2024-03-11 22:15:43 +01:00
zrep_msg debug "Warning: Package directory '${package_dir}' not found."
2024-02-29 16:53:22 +01:00
fi
2024-03-11 22:15:43 +01:00
# Remove the package from installed.json
jq --arg author "$author" --arg script "$script" \
'(.[$author] |= map(select(.script != $script))) |
if .[$author] == [] then del(.[$author]) else . end' \
"$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json"
2024-04-05 00:36:24 +02:00
zrep_msg sub "Package '${package_name}' successfully removed."
2024-03-11 22:15:43 +01:00
else
zrep_msg info "Removal canceled."
fi
}
function zrep_check_if_installed() {
local package="${1}"
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-03-11 22:15:43 +01:00
local author_name="${package%%/*}"
local script_name="${package#*/}"
# Initialize version to an empty string
typeset -g installed_version=""
# Check if the package is already installed and retrieve its version
installed_version=$(jq -r --arg author "$author_name" --arg script "$script_name" \
2024-03-12 18:06:06 +01:00
'.[$author][] | select(.script == $script) | .version' "$installed_json")
2024-03-04 22:36:49 +01:00
2024-03-12 14:58:13 +01:00
if [[ -n "${installed_version}" && "${installed_version}" != "null" ]]; then
2024-03-11 22:15:43 +01:00
# Package is installed, and version is stored in installed_version
return 0 # Package is installed
else
return 1 # Package is not installed
fi
}
2024-03-17 09:04:23 +01:00
function zrep_global_downloader() {
case ${config[global_downloader]} in
curl)
dloader="curl -s -A \"zrep ${VERSION} (curl)\""
;;
wget)
dloader="wget -q -U \"zrep ${VERSION} (wget)\""
;;
wget2)
dloader="wget2 -q -U \"zrep ${VERSION} (wget2)\""
;;
*)
echo "Invalid Downloader."
exit
;;
esac
2024-04-05 00:36:24 +02:00
2024-03-31 15:14:43 +02:00
eval ${dloader} "${1}"
2024-03-17 09:04:23 +01:00
}
2024-03-11 22:15:43 +01:00
typeset -A updatesAvailable
function zrep_check_for_updates() {
2024-03-17 09:04:23 +01:00
2024-04-05 00:36:24 +02:00
# Now using a zrep API to fetch the JSON for checking for updates. This _may_ change in the
# future if the load on the DB gets too high.
local remoteFile="${config[global_repo_url]}/getver.php"
2024-03-12 18:06:06 +01:00
# local localFile="${config[main_zrep_install_dir]}/installed.json"
zrep_installed_json
2024-03-17 09:04:23 +01:00
local remotePackages=$(zrep_global_downloader "${remoteFile}")
2024-03-11 22:15:43 +01:00
# Reset global variables
updatesAvailable=()
typeset -g updates=false # Global declaration, initializes to false
# Process updates
2024-03-12 18:06:06 +01:00
local authorsScripts=$(jq -r '. | to_entries[] | .key as $author | .value[] | "\($author)/\(.script):\(.version)"' "$installed_json")
2024-03-11 22:15:43 +01:00
for entry in ${(f)authorsScripts}; do
local author="${entry%%/*}"
local rest="${entry#*/}"
2024-03-12 14:58:13 +01:00
local script="${rest%%:*}"
2024-03-11 22:15:43 +01:00
local installed_version="${rest##*:}"
2024-03-12 14:58:13 +01:00
local remote_version=$(jq -r --arg author "$author" --arg script "$script" \
2024-03-11 22:15:43 +01:00
'.authors[] | select(.name==$author) | .scripts[] | select(.name==$script) | .version' <<<"$remotePackages")
2024-03-12 14:58:13 +01:00
if [[ "${remote_version}" > "${installed_version}" ]]; then
updatesAvailable[${author}/${script}]="${remote_version}"
zrep_msg info "\n${author}/${script} can be updated from ${installed_version} to ${remote_version}"
2024-03-11 22:15:43 +01:00
updates=true # Mark that updates are available
2024-03-04 22:36:49 +01:00
fi
2024-03-11 22:15:43 +01:00
done
2024-03-04 22:36:49 +01:00
2024-03-13 22:52:47 +01:00
if [[ ${updates} == "false" ]]; then
zrep_msg info "\nNo updates found."
fi
2024-03-11 22:15:43 +01:00
}
function zrep_update_package() {
local specificPackage=${1}
zrep_check_for_updates
2024-03-12 14:58:13 +01:00
if [[ -n "${specificPackage}" ]]; then
2024-03-11 22:15:43 +01:00
# Logic for updating a specific package
# Assuming specificPackage format is "author/script"
2024-03-12 14:58:13 +01:00
local version=${updatesAvailable[${specificPackage}]}
2024-03-11 22:15:43 +01:00
if [[ -n "$version" ]]; then
local author="${specificPackage%%/*}"
local script="${specificPackage#*/}"
2024-03-12 14:58:13 +01:00
local install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg}
2024-03-11 22:15:43 +01:00
else
2024-03-12 23:52:10 +01:00
zrep_msg info "\nNo update available for ${specificPackage}."
2024-03-11 22:15:43 +01:00
fi
2024-02-28 23:41:09 +01:00
else
2024-03-12 14:58:13 +01:00
if [[ ${updates} == "true" ]]; then
2024-03-31 15:14:43 +02:00
# Prompt the user only if updates are available
echo "New updates are available. Do you want to proceed with updating? (Y/n): "
read -q "response?"
echo # Move to a new line
# Set the default response to 'Y' if no input is entered
response=${response:-Y}
# 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 install_pkg="${author}/${script}"
zrep_install_package u ${install_pkg}
done
else
zrep_msg info "Update canceled."
fi
2024-03-12 14:58:13 +01:00
fi
2024-02-28 23:41:09 +01:00
fi
}
2024-03-15 22:10:39 +01:00
# 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.
2024-03-12 23:52:10 +01:00
function zrep_download_package() {
2024-03-15 22:10:39 +01:00
local ZipFile="${1}"
local DownloadURL="${2}"
local retries=5
local delay=5
local attempt=1
local downloader=""
local http_status
local cmd
2024-03-31 15:14:43 +02:00
local exit_status
case "${config[global_downloader]}" in
curl)
downloader="curl"
cmd="curl -L -A \"zrep ${VERSION} (curl)\" -s -o \"$ZipFile\" \"$DownloadURL\" -w \"%{http_code}\""
;;
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
;;
esac
2024-03-15 22:10:39 +01:00
while (( attempt <= retries )); do
2024-03-31 15:14:43 +02:00
# zrep_msg sub "Attempt $attempt of $retries: Downloading using $downloader..."
2024-03-15 22:10:39 +01:00
if [[ $downloader == "curl" ]]; then
http_status=$(eval $cmd)
2024-03-31 15:14:43 +02:00
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
2024-03-15 22:10:39 +01:00
fi
2024-03-31 15:14:43 +02:00
sleep $delay
((attempt++))
2024-03-15 22:10:39 +01:00
done
zrep_msg debug "Error: The download failed after $retries attempts."
return 1
2024-03-12 23:52:10 +01:00
}
2024-03-04 20:20:33 +01:00
# Function to install a package by unzipping it to ${config[main_zrep_install_dir]}
function zrep_install_package() {
2024-03-11 22:15:43 +01:00
if [[ ${1} == "u" ]]; then
updates=true
local package=${2}
2024-03-31 15:14:43 +02:00
#echo "zrep_install_package: package=$package"
2024-03-11 22:15:43 +01:00
else
updates=false
local package="${1}"
2024-04-02 21:57:39 +02:00
# Ensure package name is in 'author/package' format
if [[ ! "${package}" =~ ^.+/[^/]+$ ]]; then
zrep_msg debug "\nError: Package name must be in 'author/package' format."
return 1
fi
2024-03-11 22:15:43 +01:00
# Call zrep_check_if_installed to check if the package is already installed
2024-03-12 14:58:13 +01:00
if zrep_check_if_installed "${package}"; then
2024-03-31 15:14:43 +02:00
zrep_msg debug "\nPackage ${package} is already installed."
2024-03-11 22:15:43 +01:00
zrep_msg info "Use 'zrep list' to see installed packages."
return 0
fi
fi
2024-03-04 20:20:33 +01:00
2024-03-11 22:15:43 +01:00
# If not installed, proceed with fetching the package information
2024-04-05 00:36:24 +02:00
# Using the new zrep API to get the package version
zrep_parse_remote "${config[global_repo_url]}/getver.php\?p\=${package}" ${package}
2024-03-04 20:20:33 +01:00
2024-03-11 22:15:43 +01:00
local tmpDir="${config[main_zrep_install_dir]}/tmp"
2024-03-04 20:20:33 +01:00
2024-03-10 13:31:19 +01:00
mkdir -p "${tmpDir}"
2024-03-04 20:20:33 +01:00
2024-03-12 23:52:10 +01:00
local zipFile="${tmpDir}/${author}-${script}-${version}.zip"
2024-04-05 00:36:24 +02:00
dlurl="${config[global_repo_url]}/download/${package}/${version}"
2024-03-12 23:52:10 +01:00
zrep_download_package "${zipFile}" "${dlurl}"
2024-02-29 16:53:22 +01:00
2024-03-31 15:14:43 +02:00
unzip -q -o "${zipFile}" -d "${config[main_zrep_install_dir]}"
2024-02-29 16:53:22 +01:00
2024-03-04 20:20:33 +01:00
if [[ $? -ne 0 ]]; then
2024-03-13 07:25:59 +01:00
zrep_msg debug "\nError: Failed to unzip the package."
2024-03-04 20:20:33 +01:00
return 1
else
2024-03-10 13:31:19 +01:00
zrep_update_installed_json "${author}" "${script}" "${version}"
2024-03-04 20:20:33 +01:00
fi
2024-03-12 18:06:06 +01:00
rm "${zipFile}"
2024-03-04 20:20:33 +01:00
}
2024-03-04 23:40:02 +01:00
2024-02-28 23:41:09 +01:00
# Function to parse installed.json
function zrep_parse_installed_json() {
2024-03-01 12:22:39 +01:00
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-03-10 13:31:19 +01:00
jq -c '.' "${installed_json}"
2024-03-01 12:22:39 +01:00
2024-02-28 23:41:09 +01:00
}
2024-03-04 23:40:02 +01:00
function zrep_parse_package_name() {
2024-03-10 13:31:19 +01:00
package_name="${1}"
2024-03-12 18:06:06 +01:00
zrep_installed_json
2024-03-04 23:40:02 +01:00
author="${package_name%/*}"
script="${package_name#*/}"
2024-03-12 18:06:06 +01:00
local first_letter="${author:0:1:l}"
2024-03-10 13:31:19 +01:00
addon_path="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
2024-03-04 22:17:21 +01:00
# Check if the package is installed
2024-03-12 18:06:06 +01:00
if ! jq -e --arg author "$author" --arg script "$script" '.[$author] | any(.script == $script)' "${installed_json}" &>/dev/null; then
2024-03-13 07:25:59 +01:00
zrep_msg debug "\nError: Package '${package_name}' is not installed."
2024-03-04 22:17:21 +01:00
return 1
fi
2024-03-04 23:40:02 +01:00
}
function zrep_enable() {
2024-03-10 13:31:19 +01:00
local package_name="${1}"
zrep_parse_package_name "${package_name}"
2024-03-04 23:40:02 +01:00
2024-03-09 00:36:09 +01:00
# Initialize addons array if .zrep_addons does not exist
if [ ! -f "${config[main_zrep_install_dir]}/.addons" ]; then
addons=()
else
# Load existing addons from ${config[main_zrep_install_dir]}/.addons
source "${config[main_zrep_install_dir]}/.addons"
fi
2024-03-04 22:17:21 +01:00
# Check if the addon is already enabled
local addon_exists=0
for addon in "${addons[@]}"; do
2024-03-10 13:31:19 +01:00
if [[ "${addon}" == "${addon_path}" ]]; then
2024-03-04 22:17:21 +01:00
addon_exists=1
break
fi
done
if ((addon_exists)); then
2024-03-10 13:31:19 +01:00
echo "Package '${package_name}' is already enabled."
2024-03-04 22:17:21 +01:00
return 0
fi
# Add addon path to the array
2024-03-10 13:31:19 +01:00
addons+=("${addon_path}")
2024-03-04 22:17:21 +01:00
2024-03-04 23:40:02 +01:00
# Reconstruct .zrep_addons file with the updated addons array
2024-03-04 22:17:21 +01:00
{
echo "addons=("
for addon in "${addons[@]}"; do
2024-03-10 13:31:19 +01:00
echo " '${addon}'"
2024-03-04 22:17:21 +01:00
done
echo ")"
2024-03-09 00:36:09 +01:00
} > "${config[main_zrep_install_dir]}/.addons"
2024-03-04 22:17:21 +01:00
2024-03-10 13:31:19 +01:00
zrep_msg info "\nPackage '${package_name}' has been enabled and added to fpath."
2024-03-09 00:36:09 +01:00
zrep_msg info "You may have to run 'source ~/.zrep_addons' to get access to it."
2024-03-04 22:17:21 +01:00
}
function zrep_disable() {
2024-03-10 13:31:19 +01:00
local package_name="${1}"
zrep_parse_package_name "${package_name}"
2024-03-04 23:40:02 +01:00
2024-03-09 00:36:09 +01:00
# Initialize addons array if .zrep_addons does not exist
if [ ! -f "${config[main_zrep_install_dir]}/.addons" ]; then
addons=()
else
# Load existing addons from ${config[main_zrep_install_dir]}/.addons
source "${config[main_zrep_install_dir]}/.addons"
fi
2024-03-04 22:17:21 +01:00
# Initialize a new array for addons
local new_addons=()
# Flag to check if addon was found and removed
local found=0
# Iterate through existing addons
for addon in "${addons[@]}"; do
2024-03-10 13:31:19 +01:00
if [[ "${addon}" == "${addon_path}" ]]; then
2024-03-04 22:17:21 +01:00
found=1
else
2024-03-10 13:31:19 +01:00
new_addons+=("${addon}")
2024-03-04 22:17:21 +01:00
fi
done
if ((found == 0)); then
2024-03-10 13:31:19 +01:00
zrep_msg debug "\nPackage '${package_name}' is not currently enabled."
2024-03-04 22:17:21 +01:00
return 0
fi
2024-03-04 23:40:02 +01:00
# Reconstruct .zrep_addons file with the new addons array
2024-03-04 22:17:21 +01:00
{
echo "addons=("
2024-03-04 23:40:02 +01:00
for addon in "${new_addons[@]}"; do
2024-03-10 13:31:19 +01:00
echo " '${addon}'"
2024-03-04 22:17:21 +01:00
done
echo ")"
2024-03-09 00:36:09 +01:00
} > ${config[main_zrep_install_dir]}/.addons
2024-03-04 22:17:21 +01:00
2024-03-10 13:31:19 +01:00
zrep_msg info "\nPackage '${package_name} (${script})' has been disabled and removed from fpath."
2024-03-09 00:36:09 +01:00
zrep_msg info "You may have to run 'source ~/.zrep_addons' to remove it from your shell."
2024-03-04 22:17:21 +01:00
}
2024-02-28 23:41:09 +01:00
# Help function to display available options
function zrep_help() {
2024-03-01 12:22:39 +01:00
2024-03-06 18:33:14 +01:00
zrep_msg sub "\nUsage: zrep <command> [arguments]"
2024-03-10 13:31:19 +01:00
zrep_msg info "Available commands:"
2024-03-12 14:58:13 +01:00
if [[ ! -f ${ZREP_CONFIG} ]]; then
zrep_msg info " init: Initialize zrep"
fi
2024-03-15 22:10:39 +01:00
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 " update (u) <author/package>:\t\t\tUpdate zrep package"
zrep_msg info " enable <author/package>:\t\t\tEnable zrep package"
zrep_msg info " disable <author/package>:\t\t\tDisable zrep package"
2024-04-05 00:36:24 +02:00
zrep_msg info " search 'search term':\t\t\t\tSearch for authors, packages or description"
2024-03-15 22:10:39 +01:00
zrep_msg info " version:\t\t\t\t\tDisplay zrep version"
zrep_msg info " list:\t\t\t\t\t\tList installed packages"
2024-04-05 00:36:24 +02:00
zrep_msg info " <author/package> help:\t\t\tDisplay help for package (if available)"
2024-03-15 22:10:39 +01:00
}
function zrep_package_info() {
local package_name="${1}"
zrep_installed_json # Ensure installed.json is loaded and available
2024-03-06 18:33:14 +01:00
2024-03-15 22:10:39 +01:00
# Parse the package name to extract author and script
local author="${package_name%/*}"
local script="${package_name#*/}"
local first_letter="${author:0:1:l}" # Get the first letter of the author's name to construct the URL
# Check if the package is installed and get its version
if zrep_check_if_installed "${package_name}"; then
local installed_version="${installed_version}" # This variable is set by zrep_check_if_installed
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}"
local zrep_url="${config[global_repo_url]}/${first_letter}/${author}/${script}"
# Display package information
zrep_msg info "\nAuthor/Package:\t\t${author}/${script}"
zrep_msg info "Version installed:\t${installed_version}"
zrep_msg info "Location on disk:\t${package_dir}"
zrep_msg info "zrep URL:\t\t${zrep_url}"
else
zrep_msg debug "\nPackage '${package_name}' is not installed."
fi
2024-03-06 18:33:14 +01:00
}
function zrep_read_usage() {
2024-03-10 13:31:19 +01:00
local package_name="${1}"
2024-03-06 18:33:14 +01:00
# Parse the package name to extract author and script
local author="${package_name%/*}"
local script="${package_name#*/}"
2024-03-12 18:06:06 +01:00
local first_letter="${author:0:1:l}"
2024-03-01 12:22:39 +01:00
2024-03-06 18:33:14 +01:00
# Construct the path to the USAGE file
2024-03-10 13:31:19 +01:00
local usage_file="${config[main_zrep_install_dir]}/${first_letter}/${author}/${script}/USAGE"
2024-03-06 18:33:14 +01:00
# Check if the USAGE file exists
2024-03-10 13:31:19 +01:00
if [[ -f "${usage_file}" ]]; then
2024-03-06 18:33:14 +01:00
# Display the content of the USAGE file
2024-03-10 13:31:19 +01:00
zrep_msg sub "\n${package_name}:"
2024-03-06 18:33:14 +01:00
local usage_buffer=$(<${usage_file})
2024-03-10 13:31:19 +01:00
zrep_msg info "${usage_buffer}"
2024-03-06 18:33:14 +01:00
else
2024-03-10 13:31:19 +01:00
zrep_msg debug "No USAGE file found for package '${package_name}'."
2024-03-06 18:33:14 +01:00
fi
2024-02-28 23:41:09 +01:00
}
function main() {
2024-03-12 23:52:10 +01:00
zrep_main_version_string
2024-04-05 00:36:24 +02:00
2024-03-16 00:14:36 +01:00
if [[ ${1} != "init" ]]; then
zrep_load_config ${1}
zrep_load_theme ${config[global_theme]}
fi
2024-02-27 16:50:07 +01:00
2024-03-06 18:33:14 +01:00
# Check if the second argument is "help" and the first argument is not empty
if [[ "${2}" == "help" && -n "${1}" ]]; then
zrep_read_usage "${1}"
exit
fi
2024-02-27 16:50:07 +01:00
# Example command handling structure
2024-02-29 16:53:22 +01:00
case "${1}" in
2024-02-27 16:50:07 +01:00
init)
2024-02-28 23:41:09 +01:00
zrep_init
2024-03-15 22:10:39 +01:00
zrep_check_for_deps
2024-02-28 23:41:09 +01:00
exit
2024-02-27 16:50:07 +01:00
;;
2024-04-05 00:36:24 +02:00
search | s | find)
zrep_search "${@:2}"
;;
2024-02-29 16:53:22 +01:00
install | i)
2024-03-11 22:15:43 +01:00
zrep_install_package ${2}
2024-02-28 23:41:09 +01:00
;;
2024-03-13 07:25:59 +01:00
remove | delete | rm | del)
2024-02-28 23:41:09 +01:00
# Parse the command argument to extract the package name
2024-02-29 16:53:22 +01:00
zrep_remove_package_name="${2:-}"
2024-03-10 13:31:19 +01:00
if [[ -z "${zrep_remove_package_name}" ]]; then
2024-03-13 07:25:59 +01:00
zrep_msg info "\nUsage: zrep ${1} author/package"
2024-02-28 23:41:09 +01:00
else
2024-03-10 13:31:19 +01:00
zrep_remove_package "${zrep_remove_package_name}"
2024-02-28 23:41:09 +01:00
fi
2024-02-27 16:50:07 +01:00
;;
2024-02-29 16:53:22 +01:00
update | u)
2024-03-11 22:15:43 +01:00
zrep_update_package ${2}
2024-02-28 23:41:09 +01:00
;;
2024-02-29 16:53:22 +01:00
version | -v | --version)
2024-02-28 23:41:09 +01:00
zrep_version
;;
list)
zrep_list_installed_packages
;;
2024-02-29 16:53:22 +01:00
help | -h | --help)
2024-02-28 23:41:09 +01:00
zrep_help
2024-02-27 16:50:07 +01:00
;;
2024-03-04 22:17:21 +01:00
enable)
zrep_enable ${2}
;;
disable)
zrep_disable ${2}
;;
2024-03-15 22:10:39 +01:00
info)
zrep_package_info ${2}
;;
2024-02-27 16:50:07 +01:00
*)
2024-02-28 23:41:09 +01:00
zrep_help
2024-02-27 16:50:07 +01:00
;;
esac
}
# Call main with all passed arguments
main "$@"