#!/bin/bash # ------------------------------------------------------------------ # [Author] Cortana Rosero One # [Title] ghcli_install.sh - Instalador de GitHub CLI # [Generated] Created by Claude Code (claude-3-7-sonnet-20250219) # # AGPL License # Modified date: 14/03/2025 # ------------------------------------------------------------------ # Función para verificar si se está ejecutando como root check_root() { if [ "$(id -u)" -ne 0 ]; then echo "Este script requiere privilegios de administrador." echo "Intentando escalar privilegios..." # Verificar si sudo está disponible if command -v sudo &> /dev/null; then echo "Usando sudo para escalar privilegios..." sudo "$0" "$@" exit $? elif command -v doas &> /dev/null; then echo "Usando doas para escalar privilegios..." doas "$0" "$@" exit $? else echo "ERROR: No se encontró sudo ni doas. Por favor ejecute este script como root." exit 1 fi fi } # Función para detectar la distribución detect_distro() { if [ -f /etc/os-release ]; then . /etc/os-release DISTRO=$ID elif type lsb_release >/dev/null 2>&1; then DISTRO=$(lsb_release -si | tr '[:upper:]' '[:lower:]') elif [ -f /etc/lsb-release ]; then . /etc/lsb-release DISTRO=$DISTRIB_ID elif [ -f /etc/debian_version ]; then DISTRO="debian" else DISTRO=$(uname -s) fi echo "Distribución detectada: $DISTRO" } # Función para instalar GitHub CLI en sistemas basados en Debian/Ubuntu install_gh_debian() { echo "Instalando GitHub CLI en sistema basado en Debian/Ubuntu..." # Añadir repositorio de GitHub CLI type -p curl >/dev/null || apt update && apt install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ && apt update \ && apt install gh -y if [ $? -eq 0 ]; then echo "GitHub CLI instalado correctamente." else echo "ERROR: No se pudo instalar GitHub CLI." exit 1 fi } # Función para instalar GitHub CLI en sistemas basados en Red Hat/Fedora install_gh_redhat() { echo "Instalando GitHub CLI en sistema basado en Red Hat/Fedora..." # Añadir repositorio de GitHub CLI dnf install -y dnf-plugins-core dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo dnf install -y gh if [ $? -eq 0 ]; then echo "GitHub CLI instalado correctamente." else echo "ERROR: No se pudo instalar GitHub CLI." exit 1 fi } # Función para instalar GitHub CLI en Arch Linux install_gh_arch() { echo "Instalando GitHub CLI en Arch Linux..." # Instalar GitHub CLI desde los repositorios oficiales pacman -Sy --noconfirm gh if [ $? -eq 0 ]; then echo "GitHub CLI instalado correctamente." else echo "ERROR: No se pudo instalar GitHub CLI." exit 1 fi } # Función para instalar GitHub CLI en sistemas basados en SUSE install_gh_suse() { echo "Instalando GitHub CLI en sistema basado en SUSE..." # Añadir repositorio de GitHub CLI zypper addrepo --refresh https://cli.github.com/packages/rpm/gh-cli.repo zypper install -y gh if [ $? -eq 0 ]; then echo "GitHub CLI instalado correctamente." else echo "ERROR: No se pudo instalar GitHub CLI." exit 1 fi } # Función para instalar GitHub CLI en macOS install_gh_macos() { echo "Instalando GitHub CLI en macOS..." # Verificar si Homebrew está instalado if ! command -v brew &> /dev/null; then echo "Homebrew no está instalado. Instalando Homebrew..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" fi # Instalar GitHub CLI brew install gh if [ $? -eq 0 ]; then echo "GitHub CLI instalado correctamente." else echo "ERROR: No se pudo instalar GitHub CLI." exit 1 fi } # Función para verificar si gh está instalado check_gh() { if command -v gh &> /dev/null; then echo "GitHub CLI ya está instalado." echo "Versión actual:" gh --version read -p "¿Desea reinstalar o actualizar? (s/n): " REINSTALL if [[ ! "$REINSTALL" =~ ^[Ss]$ ]]; then echo "Operación cancelada." exit 0 fi fi } # Función principal main() { # Verificar si ya está instalado check_gh # Verificar privilegios de root (excepto en macOS) if [ "$(uname -s)" != "Darwin" ]; then check_root "$@" fi # Detectar la distribución detect_distro # Instalar según la distribución case $DISTRO in ubuntu|debian|linuxmint|pop|elementary) install_gh_debian ;; fedora|rhel|centos|rocky|almalinux) install_gh_redhat ;; arch|manjaro|endeavouros) install_gh_arch ;; opensuse*|suse|sles) install_gh_suse ;; darwin) install_gh_macos ;; *) echo "Distribución no soportada: $DISTRO" echo "Por favor, visite https://github.com/cli/cli#installation para instrucciones de instalación manual." exit 1 ;; esac # Verificar la instalación if command -v gh &> /dev/null; then echo "GitHub CLI instalado correctamente." gh --version # Sugerir autenticación echo "" echo "Para autenticarse con GitHub, ejecute:" echo "gh auth login" echo "" echo "Para más información, visite: https://cli.github.com/manual/" else echo "ERROR: La instalación de GitHub CLI falló." exit 1 fi } # Ejecutar función principal main "$@" exit 0