diff --git a/changelog.md b/changelog.md index aa6d438..c97190c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-03-25 - 2.3.0 - feat(installer/cli) +Add OS detection and git auto-installation support to install.sh and improve service setup prompt in CLI + +- Implemented helper functions in install.sh to detect OS type and automatically install git if missing +- Prompt user for git installation if not present before cloning the repository +- Enhanced CLI service setup flow to offer starting the NUPST service immediately after installation + ## 2025-03-25 - 2.2.0 - feat(cli) Add 'config' command to display current configuration and update CLI help diff --git a/install.sh b/install.sh index a7a47d3..00cf6be 100644 --- a/install.sh +++ b/install.sh @@ -17,6 +17,78 @@ if [ ! -t 0 ]; then PIPED=1 fi +# Helper function to detect OS type +detect_os() { + if [ -f /etc/os-release ]; then + . /etc/os-release + OS=$ID + elif type lsb_release >/dev/null 2>&1; then + OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]') + elif [ -f /etc/lsb-release ]; then + . /etc/lsb-release + OS=$DISTRIB_ID + elif [ -f /etc/debian_version ]; then + OS="debian" + elif [ -f /etc/redhat-release ]; then + if grep -q "CentOS" /etc/redhat-release; then + OS="centos" + elif grep -q "Fedora" /etc/redhat-release; then + OS="fedora" + else + OS="rhel" + fi + else + OS=$(uname -s) + fi + echo $OS +} + +# Helper function to install git +install_git() { + OS=$(detect_os) + echo "Detected OS: $OS" + + case "$OS" in + ubuntu|debian|pop|mint|elementary|kali|zorin) + echo "Installing git using apt..." + apt-get update && apt-get install -y git + ;; + fedora|rhel|centos|almalinux|rocky) + echo "Installing git using dnf/yum..." + if command -v dnf &> /dev/null; then + dnf install -y git + else + yum install -y git + fi + ;; + arch|manjaro|endeavouros|garuda) + echo "Installing git using pacman..." + pacman -Sy --noconfirm git + ;; + opensuse*|suse|sles) + echo "Installing git using zypper..." + zypper install -y git + ;; + alpine) + echo "Installing git using apk..." + apk add git + ;; + *) + echo "Unsupported OS: $OS" + echo "Please install git manually and run the installer again." + exit 1 + ;; + esac + + # Check if git was installed successfully + if ! command -v git &> /dev/null; then + echo "Failed to install git. Please install git manually and run the installer again." + exit 1 + fi + + echo "Git installed successfully." +} + # Define installation directory INSTALL_DIR="/opt/nupst" REPO_URL="https://code.foss.global/serve.zone/nupst.git" @@ -26,8 +98,15 @@ if [ $PIPED -eq 1 ]; then # Check if git is installed if ! command -v git &> /dev/null; then - echo "Git is required but not installed. Please install git first." - exit 1 + echo "Git is required but not installed." + read -p "Would you like to install git now? (Y/n): " install_git_prompt + + if [[ "$install_git_prompt" =~ ^[Nn]$ ]]; then + echo "Git installation skipped. Please install git manually and run the installer again." + exit 1 + else + install_git + fi fi # Check if installation directory exists diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f66b923..8dca2b3 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@serve.zone/nupst', - version: '2.2.0', + version: '2.3.0', description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' } diff --git a/ts/cli.ts b/ts/cli.ts index d9094d8..11633ca 100644 --- a/ts/cli.ts +++ b/ts/cli.ts @@ -834,7 +834,7 @@ Options: } /** - * Optionally enable systemd service + * Optionally enable and start systemd service * @param prompt Function to prompt for user input */ private async optionallyEnableService(prompt: (question: string) => Promise): Promise { @@ -843,8 +843,21 @@ Options: } else { const setupService = await prompt('Would you like to enable NUPST as a system service? (y/N): '); if (setupService.toLowerCase() === 'y') { - await this.nupst.getSystemd().install(); - console.log('Service installed. Use "nupst start" to start the service.'); + try { + await this.nupst.getSystemd().install(); + console.log('Service installed and enabled to start on boot.'); + + // Ask if the user wants to start the service now + const startService = await prompt('Would you like to start the NUPST service now? (Y/n): '); + if (startService.toLowerCase() !== 'n') { + await this.nupst.getSystemd().start(); + console.log('NUPST service started successfully.'); + } else { + console.log('Service not started. Use "nupst start" to start the service manually.'); + } + } catch (error) { + console.error(`Failed to setup service: ${error.message}`); + } } } }