From 30d97a3568054dad9a6422f25fda9e17a241315b Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Mon, 30 Dec 2024 08:53:44 -0300 Subject: [PATCH] Refactor dependency checks to auto-install missing packages --- docs.org | 34 ++++++++++++++++++++++++++++------ install.sh | 32 +++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/docs.org b/docs.org index c813126..c5e8436 100644 --- a/docs.org +++ b/docs.org @@ -72,7 +72,7 @@ readonly YELLOW='\033[1;33m' readonly NC='\033[0m' # No Color # Dependencies -readonly REQUIRED_COMMANDS=("curl" "docker" "docker" "sed" "awk") +readonly REQUIRED_COMMANDS=("curl" "sed" "awk") #+end_src * Functions @@ -159,14 +159,36 @@ verify_user_permissions() { #+begin_src bash check_dependencies() { - # Check for required commands - for cmd in "${REQUIRED_COMMANDS[@]}"; do - if ! command -v "$cmd" &> /dev/null; then - log_error "Required command '$cmd' not found!" + local missing_packages=() + local install_cmd="" + + # Check for required commands and collect missing ones + for pkg in "${REQUIRED_PACKAGES[@]}"; do + if ! command -v "$pkg" &> /dev/null; then + missing_packages+=("$pkg") + else + log_success "$pkg exists ✅" fi done - # Check Docker + # If there are missing packages, offer to install them + if [ ${#missing_packages[@]} -gt 0 ]; then + log_warning "Missing required packages: ${missing_packages[*]}" + read -p "Would you like to install the missing packages? (y/N) [Default = n]: " install_deps + install_deps=${install_deps:-"n"} + + if [ "${install_deps,,}" = "y" ]; then + echo "Installing missing packages..." + if ! sudo apt install -y "${missing_packages[@]}"; then + log_error "Failed to install missing packages. Please install them manually: ${missing_packages[*]}" + fi + log_success "Successfully installed missing packages ✅" + else + log_error "Please install the required packages manually: ${missing_packages[*]}" + fi + fi + + # Check Docker and Docker Compose if command -v docker &> /dev/null; then log_success "docker exists ✅" if docker compose version &> /dev/null; then diff --git a/install.sh b/install.sh index a20c791..0e88721 100644 --- a/install.sh +++ b/install.sh @@ -99,14 +99,36 @@ verify_user_permissions() { } check_dependencies() { - # Check for required commands - for cmd in "${REQUIRED_COMMANDS[@]}"; do - if ! command -v "$cmd" &> /dev/null; then - log_error "Required command '$cmd' not found!" + local missing_packages=() + local install_cmd="" + + # Check for required commands and collect missing ones + for pkg in "${REQUIRED_PACKAGES[@]}"; do + if ! command -v "$pkg" &> /dev/null; then + missing_packages+=("$pkg") + else + log_success "$pkg exists ✅" fi done - # Check Docker + # If there are missing packages, offer to install them + if [ ${#missing_packages[@]} -gt 0 ]; then + log_warning "Missing required packages: ${missing_packages[*]}" + read -p "Would you like to install the missing packages? (y/N) [Default = n]: " install_deps + install_deps=${install_deps:-"n"} + + if [ "${install_deps,,}" = "y" ]; then + echo "Installing missing packages..." + if ! sudo apt install -y "${missing_packages[@]}"; then + log_error "Failed to install missing packages. Please install them manually: ${missing_packages[*]}" + fi + log_success "Successfully installed missing packages ✅" + else + log_error "Please install the required packages manually: ${missing_packages[*]}" + fi + fi + + # Check Docker and Docker Compose if command -v docker &> /dev/null; then log_success "docker exists ✅" if docker compose version &> /dev/null; then