devs/bin/glcli_install.sh
Mauro Rosero P. cc5ae62213
[IMPROVED] Priorizar instalación de GitLab CLI desde repositorios del sistema
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-15 10:30:29 -05:00

450 lines
No EOL
14 KiB
Bash
Executable file

#!/bin/bash
# ------------------------------------------------------------------
# [Author] Cortana Rosero One <cortana@rosero.one>
# [Title] glcli_install.sh - Instalador de GitLab 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 glab mediante Homebrew en macOS y Linux
install_glab_homebrew() {
echo "Instalando GitLab CLI mediante Homebrew..."
# 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)"
# Verificar si la instalación fue exitosa
if ! command -v brew &> /dev/null; then
echo "ERROR: No se pudo instalar Homebrew."
return 1
fi
fi
# Instalar GitLab CLI
brew install gitlab-glab/tap/glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante Homebrew."
return 0
else
echo "ERROR: No se pudo instalar GitLab CLI mediante Homebrew."
return 1
fi
}
# Función para instalar glab en sistemas basados en Debian/Ubuntu
install_glab_debian() {
echo "Instalando GitLab CLI en sistema basado en Debian/Ubuntu..."
# Verificar si curl está instalado
if ! command -v curl &> /dev/null; then
apt update && apt install -y curl
fi
# Intentar primero usando los paquetes del sistema si están disponibles
if command -v apt &> /dev/null; then
echo "Verificando si glab está disponible en los repositorios..."
apt-cache search --names-only "^glab$" 2>/dev/null | grep -q "^glab"
if [ $? -eq 0 ]; then
echo "GitLab CLI encontrado en los repositorios. Instalando..."
apt update && apt install -y glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente desde los repositorios oficiales."
return 0
fi
fi
fi
# Si no está en los repositorios, usar el script oficial
if command -v gpg &> /dev/null; then
echo "Agregando repositorio oficial de GitLab..."
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante script oficial."
return 0
fi
fi
# Si el método anterior falla, probar con Homebrew
echo "Intentando instalar mediante Homebrew..."
install_glab_homebrew
return $?
}
# Función para instalar glab en sistemas basados en Red Hat/Fedora
install_glab_redhat() {
echo "Instalando GitLab CLI en sistema basado en Red Hat/Fedora..."
# Verificar si dnf está instalado
if command -v dnf &> /dev/null; then
# Verificar si glab está disponible en los repositorios
echo "Verificando si glab está disponible en los repositorios..."
dnf list glab &>/dev/null
if [ $? -eq 0 ]; then
echo "GitLab CLI encontrado en los repositorios. Instalando..."
dnf install -y glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente desde los repositorios oficiales."
return 0
fi
fi
# Si no está en los repositorios, usar el script oficial
if command -v gpg &> /dev/null; then
echo "Usando script oficial de instalación..."
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante script oficial."
return 0
fi
fi
# Si el método anterior falla, probar con snap
if command -v snap &> /dev/null; then
echo "Intentando instalar mediante snap..."
snap install glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante snap."
return 0
fi
fi
# Si dnf no está disponible, pero yum sí
elif command -v yum &> /dev/null; then
# Verificar si glab está disponible en los repositorios
echo "Verificando si glab está disponible en los repositorios..."
yum list glab &>/dev/null
if [ $? -eq 0 ]; then
echo "GitLab CLI encontrado en los repositorios. Instalando..."
yum install -y glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente desde los repositorios oficiales."
return 0
fi
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "Intentando instalar mediante Homebrew..."
install_glab_homebrew
return $?
}
# Función para instalar glab en Arch Linux
install_glab_arch() {
echo "Instalando GitLab CLI en Arch Linux..."
# Intentar primero usando el repositorio oficial
if command -v pacman &> /dev/null; then
# Verificar si glab está en los repositorios
if pacman -Ss glab | grep -q "^community/glab"; then
pacman -Sy --noconfirm glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente desde los repositorios oficiales."
return 0
fi
fi
# Si no está en los repositorios, intentar con yay (AUR)
if command -v yay &> /dev/null; then
yay -S --noconfirm glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente desde AUR."
return 0
fi
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "Intentando instalar mediante Homebrew..."
install_glab_homebrew
return $?
}
# Función para instalar glab en sistemas basados en SUSE
install_glab_suse() {
echo "Instalando GitLab CLI en sistema basado en SUSE..."
# Intentar primero usando el script oficial
if command -v gpg &> /dev/null; then
echo "Usando script oficial de instalación..."
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante script oficial."
return 0
fi
fi
# Si el método anterior falla, probar con snap
if command -v snap &> /dev/null; then
echo "Intentando instalar mediante snap..."
snap install glab
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante snap."
return 0
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "Intentando instalar mediante Homebrew..."
install_glab_homebrew
return $?
}
# Función para instalar directamente usando Go
install_glab_go() {
echo "Intentando instalar GitLab CLI mediante Go..."
# Verificar si Go está instalado
if ! command -v go &> /dev/null; then
echo "Go no está instalado. Se requiere Go para este método de instalación."
return 1
fi
# Instalar GitLab CLI mediante Go
go install gitlab.com/gitlab-org/cli/cmd/glab@main
if [ $? -eq 0 ]; then
echo "GitLab CLI instalado correctamente mediante Go."
echo "Asegúrese de que \$GOPATH/bin esté en su PATH."
echo "Por ejemplo, puede agregar lo siguiente a su ~/.bashrc o ~/.zshrc:"
echo "export PATH=\$PATH:\$HOME/go/bin"
return 0
else
echo "ERROR: No se pudo instalar GitLab CLI mediante Go."
return 1
fi
}
# Función para instalar usando método binario genérico
install_glab_binary() {
echo "Instalando GitLab CLI usando binarios precompilados..."
# Crear directorio temporal
TMP_DIR=$(mktemp -d)
cd "$TMP_DIR" || return 1
# Detectar arquitectura
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo "Arquitectura no soportada: $ARCH"
return 1
;;
esac
# Detectar sistema operativo
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
# Obtener la última versión
LATEST_VERSION=$(curl -s https://api.github.com/repos/profclems/glab/releases/latest | grep "tag_name" | cut -d'"' -f4)
if [ -z "$LATEST_VERSION" ]; then
LATEST_VERSION="v1.25.0" # Versión de respaldo si no se puede obtener la última
fi
# Construir URL de descarga
DOWNLOAD_URL="https://github.com/profclems/glab/releases/download/${LATEST_VERSION}/glab_${LATEST_VERSION:1}_${OS}_${ARCH}.tar.gz"
echo "Descargando GitLab CLI desde: $DOWNLOAD_URL"
# Descargar y extraer
curl -L "$DOWNLOAD_URL" -o glab.tar.gz
if [ $? -ne 0 ]; then
echo "ERROR: No se pudo descargar GitLab CLI."
cd - > /dev/null
rm -rf "$TMP_DIR"
return 1
fi
tar xzf glab.tar.gz
if [ $? -ne 0 ]; then
echo "ERROR: No se pudo extraer el archivo de GitLab CLI."
cd - > /dev/null
rm -rf "$TMP_DIR"
return 1
fi
# Instalar binario
mv bin/glab /usr/local/bin/glab
chmod +x /usr/local/bin/glab
# Limpiar
cd - > /dev/null
rm -rf "$TMP_DIR"
# Verificar instalación
if command -v glab &> /dev/null; then
echo "GitLab CLI instalado correctamente mediante binarios precompilados."
return 0
else
echo "ERROR: No se pudo instalar GitLab CLI mediante binarios precompilados."
return 1
fi
}
# Función para verificar si glab está instalado
check_glab() {
if command -v glab &> /dev/null; then
echo "GitLab CLI ya está instalado."
echo "Versión actual:"
glab --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_glab
# Verificar privilegios de root (excepto en macOS)
if [ "$(uname -s)" != "Darwin" ]; then
check_root "$@"
fi
# Detectar la distribución
detect_distro
# Intentar instalar según la distribución
install_success=false
case $DISTRO in
ubuntu|debian|linuxmint|pop|elementary|raspbian)
install_glab_debian
if [ $? -eq 0 ]; then
install_success=true
fi
;;
fedora|rhel|centos|rocky|almalinux)
install_glab_redhat
if [ $? -eq 0 ]; then
install_success=true
fi
;;
arch|manjaro|endeavouros)
install_glab_arch
if [ $? -eq 0 ]; then
install_success=true
fi
;;
opensuse*|suse|sles)
install_glab_suse
if [ $? -eq 0 ]; then
install_success=true
fi
;;
darwin)
install_glab_homebrew
if [ $? -eq 0 ]; then
install_success=true
fi
;;
*)
echo "Distribución no soportada directamente: $DISTRO"
echo "Intentando métodos alternativos..."
;;
esac
# Si no se pudo instalar con el método específico para la distribución, intentar métodos alternativos
if [ "$install_success" = false ]; then
echo "Intentando métodos alternativos de instalación..."
# Intentar instalar con Go
if command -v go &> /dev/null; then
install_glab_go
if [ $? -eq 0 ]; then
install_success=true
fi
fi
# Si aún no se ha instalado, probar con binarios precompilados
if [ "$install_success" = false ]; then
install_glab_binary
if [ $? -eq 0 ]; then
install_success=true
fi
fi
# Si todos los métodos fallan
if [ "$install_success" = false ]; then
echo "No se pudo instalar GitLab CLI con ninguno de los métodos disponibles."
echo "Por favor, visite https://gitlab.com/gitlab-org/cli para instrucciones de instalación manual."
exit 1
fi
fi
# Verificar la instalación
if command -v glab &> /dev/null; then
echo "GitLab CLI instalado correctamente."
glab --version
# Sugerir autenticación
echo ""
echo "Para autenticarse con GitLab, ejecute:"
echo "glab auth login"
echo ""
echo "Para más información, visite: https://gitlab.com/gitlab-org/cli/blob/main/docs/index.md"
else
echo "ERROR: La instalación de GitLab CLI falló."
exit 1
fi
}
# Ejecutar función principal
main "$@"
exit 0