Update zrep script:

- Added `zrep_main_version_string` function to display version string.
- Modified `zrep_remove_package` function to safely remove directories and update `installed.json`.
- Updated `zrep_help` function to include `enable` and `disable` commands.
- Minor formatting improvements and code cleanup. - Mon, 04 Mar 2024 22:36:49 +0100
This commit is contained in:
Stig-Ørjan Smelror 2024-03-04 22:36:49 +01:00
parent 469ff49206
commit d1f2b04d5b

59
zrep
View File

@ -56,6 +56,9 @@ cyan_bg="\033[46m"
white_bg="\033[47m" white_bg="\033[47m"
end="\033[0m" end="\033[0m"
function zrep_main_version_string() {
echo "${magenta}${blue_bg} ${ZREP} ${end}${bold_white}${blue_bg}${VERSION} ${end}"
}
function zrep_version() { function zrep_version() {
zrep_msg info "Created by kekePower - 2024" zrep_msg info "Created by kekePower - 2024"
@ -255,7 +258,6 @@ function load_config() {
} }
function zrep_remove_package() { function zrep_remove_package() {
local package_name="$1" local package_name="$1"
local installed_json="${config[main_zrep_install_dir]}/installed.json" local installed_json="${config[main_zrep_install_dir]}/installed.json"
@ -275,55 +277,46 @@ function zrep_remove_package() {
return 1 return 1
fi fi
# Extract author and script from package_name local author=$(echo "$package_info" | jq -r '.author')
local author script local script=$(echo "$package_info" | jq -r '.script')
author=$(echo "$package_name" | cut -d '/' -f1) local first_letter=$(echo "$author" | cut -c 1 | tr '[:upper:]' '[:lower:]')
script=$(echo "$package_name" | cut -d '/' -f2) local package_dir="${config[main_zrep_install_dir]}/$first_letter/$author/$script"
# Display package information
echo "Package information:" echo "Package information:"
echo "$package_info" | jq . echo "$package_info" | jq .
# Ask user for confirmation with default response "Y" # Ask user for confirmation with default response "Y"
read -q -r "REPLY?Are you sure you want to remove this package? (y/n) " && REPLY="${REPLY:-Y}" read "REPLY?Are you sure you want to remove this package? (y/n) [Y]: "
REPLY=${REPLY:-Y}
echo echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then if [[ "$REPLY" =~ ^[Yy]$ ]]; then
# Extract first letter of package_name
local first_letter=${package_name:0:1}
# Remove the package directory from disk # Remove the package directory from disk
local package_dir="${config[main_zrep_install_dir]}/${first_letter}/${package_name}"
if [[ -d "$package_dir" ]]; then if [[ -d "$package_dir" ]]; then
rm -rf "$package_dir" rm -rf "$package_dir"
echo "Package directory '$package_dir' removed successfully." echo "Package directory '$package_dir' removed successfully."
# Check if the author directory is empty and delete it if so
local author_dir="${config[main_zrep_install_dir]}/${first_letter}/$author"
if [[ -d "$author_dir" && ! -f "$author_dir/*" ]]; then
rm -rf "$author_dir"
echo "Author directory '$author_dir' removed successfully."
# Check if the first letter directory is empty and delete it if so
if [[ -d "${config[main_zrep_install_dir]}/$first_letter" && -f "${config[main_zrep_install_dir]}/$first_letter/.[!.]*" ]]; then
rm -rf "${config[main_zrep_install_dir]}/$first_letter"
echo "First letter directory '${config[main_zrep_install_dir]}/$first_letter' removed successfully."
fi
fi
# Remove the package from installed.json (if everything else was successful)
jq "map(select(.author + \"/\" + .script != \"$package_name\"))" "$installed_json" > "$installed_json.tmp"
mv "$installed_json.tmp" "$installed_json"
echo "Package '$package_name' removed successfully."
else else
echo "Warning: Package directory '$package_dir' not found." echo "Warning: Package directory '$package_dir' not found."
fi fi
echo "Package '$package_name' removed successfully." # Safely check and remove author and first letter directories if empty
local author_dir="${config[main_zrep_install_dir]}/$first_letter/$author"
if [[ -d "$author_dir" && ! "$(ls -A "$author_dir")" ]]; then
rmdir "$author_dir"
echo "Author directory '$author_dir' removed successfully."
fi
if [[ -d "${config[main_zrep_install_dir]}/$first_letter" && ! "$(ls -A "${config[main_zrep_install_dir]}/$first_letter")" ]]; then
rmdir "${config[main_zrep_install_dir]}/$first_letter"
echo "First letter directory '${config[main_zrep_install_dir]}/$first_letter' removed successfully."
fi
# Remove the package from installed.json
jq "del(.[] | select(.author == \"$author\" and .script == \"$script\"))" "$installed_json" > "$installed_json.tmp" && mv "$installed_json.tmp" "$installed_json"
echo "Package '$package_name' removed successfully from installed.json."
else else
echo "Removal canceled." echo "Removal canceled."
fi fi
} }
# 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]}
@ -508,6 +501,8 @@ function zrep_help() {
zrep_msg other " install (i) <author/package>: Install a package" zrep_msg other " install (i) <author/package>: Install a package"
zrep_msg other " remove (rm, delete) <author/package>: Remove a package" zrep_msg other " remove (rm, delete) <author/package>: Remove a package"
zrep_msg other " update (u) <author/package>: Update zrep package" zrep_msg other " update (u) <author/package>: Update zrep package"
zrep_msg other " enable <author/package>: Enable zrep package"
zrep_msg other " disable <author/package>: Disable zrep package"
zrep_msg other " version: Display zrep version" zrep_msg other " version: Display zrep version"
zrep_msg other " list: List installed packages" zrep_msg other " list: List installed packages"
@ -515,7 +510,7 @@ function zrep_help() {
function main() { function main() {
echo "${magenta}${blue_bg} ${ZREP} ${end}${bold_white}${blue_bg}${VERSION} ${end}" zrep_main_version_string
load_config load_config