devs/bin/gladmin_install.sh

503 lines
14 KiB
Bash
Executable file

#!/bin/bash
#Script : gladmin_install.sh
#Apps : MRDEVS TOOLS
#Description : Instala Gitlab Manager CLI
#Author : Cortana Rosero One
#Company Email : cortana@rosero.one
#Generated by : Claude Code (claude-3-7-sonnet-20250219)
#Created : 2025/03/19 11:57:08
#Modified : 2025/03/19 11:57:08
#Version : 1.2.0
#Use Notes :
#==============================================================================
# Derechos de Autor [2025] [Mauro Rosero P. <mauro@rosero.one>]
#==============================================================================
# Este programa es software libre: usted puede redistribuirlo y/o modificarlo
# bajo los términos de la Licencia Pública Affero General de GNU tal como
# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia,
# o (a su elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil,
# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la
# Licencia Pública Affero General de GNU para obtener más detalles.
#
# Debería haber recibido una copia de la Licencia Pública Affero General
# junto con este programa. Si no la recibió, consulte <https://www.gnu.org/licenses/>.
# Configuración inicial
# Usar DEVELOPER_DIR de base.lib
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_BASE="bin"
BIN_LIBS="lib"
BIN_MESG="msg"
BIN_CFGS="config"
# Leer DEVSPATH desde el archivo de configuración o usar "devs" por defecto
if [ -f "$SCRIPT_DIR/config/devspath.dat" ]; then
DEVSPATH=$(cat "$SCRIPT_DIR/$BIN_CFGS/devspath.dat")
else
DEVSPATH="devs"
fi
BIN_HOME="$HOME/$DEVSPATH"
VERSION=$(cat "$BIN_HOME/$BIN_BASE/$BIN_CFGS/version")
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# Importar bibliotecas necesarias
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib"
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/developers.lib"
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/console.lib"
# Cargar mensajes en el idioma del sistema o español por defecto
load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head"
load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "developers"
title="${head_000} ${head_002}"
apps_title="${glmsg_000}"
# Función para verificar si se está ejecutando como root
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "${glmsg_001}"
echo "${glmsg_002}"
# Verificar si sudo está disponible
if command -v sudo &> /dev/null; then
echo "${glmsg_003}"
sudo "$0" "$@"
exit $?
elif command -v doas &> /dev/null; then
echo "${glmsg_004}"
doas "$0" "$@"
exit $?
else
echo "${glmsg_005}"
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 "${glmsg_006} $DISTRO"
}
# Función para instalar glab mediante Homebrew en macOS y Linux
install_glab_homebrew() {
echo "${glmsg_007}"
# Verificar si Homebrew está instalado
if ! command -v brew &> /dev/null; then
echo "${glmsg_008}"
/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 "${glmsg_009}"
return 1
fi
fi
# Instalar GitLab CLI
brew install gitlab-glab/tap/glab
if [ $? -eq 0 ]; then
echo "${glmsg_010}"
return 0
else
echo "${glmsg_011}"
return 1
fi
}
# Función para instalar glab en sistemas basados en Debian/Ubuntu
install_glab_debian() {
echo "${glmsg_012}"
# 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 "${glmsg_013}"
apt-cache search --names-only "^glab$" 2>/dev/null | grep -q "^glab"
if [ $? -eq 0 ]; then
echo "${glmsg_014}"
apt update && apt install -y glab
if [ $? -eq 0 ]; then
echo "${glmsg_015}"
return 0
fi
fi
fi
# Si no está en los repositorios, usar el script oficial
if command -v gpg &> /dev/null; then
echo "${glmsg_016}"
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "${glmsg_017}"
return 0
fi
fi
# Si el método anterior falla, probar con Homebrew
echo "${glmsg_018}"
install_glab_homebrew
return $?
}
# Función para instalar glab en sistemas basados en Red Hat/Fedora
install_glab_redhat() {
echo "${glmsg_019}"
# Verificar si dnf está instalado
if command -v dnf &> /dev/null; then
# Verificar si glab está disponible en los repositorios
echo "${glmsg_013}"
dnf list glab &>/dev/null
if [ $? -eq 0 ]; then
echo "${glmsg_014}"
dnf install -y glab
if [ $? -eq 0 ]; then
echo "${glmsg_015}"
return 0
fi
fi
# Si no está en los repositorios, usar el script oficial
if command -v gpg &> /dev/null; then
echo "${glmsg_020}"
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "${glmsg_017}"
return 0
fi
fi
# Si el método anterior falla, probar con snap
if command -v snap &> /dev/null; then
echo "${glmsg_021}"
snap install glab
if [ $? -eq 0 ]; then
echo "${glmsg_022}"
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 "${glmsg_013}"
yum list glab &>/dev/null
if [ $? -eq 0 ]; then
echo "${glmsg_014}"
yum install -y glab
if [ $? -eq 0 ]; then
echo "${glmsg_015}"
return 0
fi
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "${glmsg_018}"
install_glab_homebrew
return $?
}
# Función para instalar glab en Arch Linux
install_glab_arch() {
echo "${glmsg_023}"
# 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 "${glmsg_015}"
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 "${glmsg_024}"
return 0
fi
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "${glmsg_018}"
install_glab_homebrew
return $?
}
# Función para instalar glab en sistemas basados en SUSE
install_glab_suse() {
echo "${glmsg_025}"
# Intentar primero usando el script oficial
if command -v gpg &> /dev/null; then
echo "${glmsg_020}"
curl -s https://gitlab.com/gitlab-org/cli/-/raw/main/scripts/install.sh | bash
if [ $? -eq 0 ]; then
echo "${glmsg_017}"
return 0
fi
fi
# Si el método anterior falla, probar con snap
if command -v snap &> /dev/null; then
echo "${glmsg_021}"
snap install glab
if [ $? -eq 0 ]; then
echo "${glmsg_022}"
return 0
fi
fi
# Si los métodos anteriores fallan, probar con Homebrew
echo "${glmsg_018}"
install_glab_homebrew
return $?
}
# Función para instalar directamente usando Go
install_glab_go() {
echo "${glmsg_026}"
# Verificar si Go está instalado
if ! command -v go &> /dev/null; then
echo "${glmsg_027}"
return 1
fi
# Instalar GitLab CLI mediante Go
go install gitlab.com/gitlab-org/cli/cmd/glab@main
if [ $? -eq 0 ]; then
echo "${glmsg_028}"
echo "${glmsg_029}"
echo "${glmsg_030}"
echo "${glmsg_031}"
return 0
else
echo "${glmsg_032}"
return 1
fi
}
# Función para instalar usando método binario genérico
install_glab_binary() {
echo "${glmsg_033}"
# 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 "${glmsg_034} $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 "${glmsg_035} $DOWNLOAD_URL"
# Descargar y extraer
curl -L "$DOWNLOAD_URL" -o glab.tar.gz
if [ $? -ne 0 ]; then
echo "${glmsg_036}"
cd - > /dev/null
rm -rf "$TMP_DIR"
return 1
fi
tar xzf glab.tar.gz
if [ $? -ne 0 ]; then
echo "${glmsg_037}"
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 "${glmsg_038}"
return 0
else
echo "${glmsg_039}"
return 1
fi
}
# Función para verificar si glab está instalado
check_glab() {
if command -v glab &> /dev/null; then
# Mostrar versión actual
local current_version=$(glab --version | head -n 1)
# Preguntar si se desea reinstalar o actualizar usando dialog
dialog_yesno "${glmsg_040}\n${glmsg_041}\n${current_version}\n\n${glmsg_042}"
if [ $? -ne 0 ]; then
dialog_error_box "${head_info}" "${glmsg_043}"
exit 0
fi
fi
}
# Función principal
main() {
# Verificar que 'dialog' esté instalado
if ! command -v dialog &>/dev/null; then
echo "${head_001}"
exit 1
fi
# 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
;;
*)
dialog_error_box "${head_warning}" "${glmsg_044} $DISTRO\n${glmsg_045}"
;;
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
dialog_error_box "${head_info}" "${glmsg_046}"
# 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
dialog_error_box "${head_error}" "${glmsg_047}\n\n${glmsg_048}"
exit 1
fi
fi
# Verificar la instalación
if command -v glab &> /dev/null; then
local current_version=$(glab --version | head -n 1)
# Mostrar mensaje de éxito y sugerir autenticación usando dialog
dialog_error_box "${head_success}" "${glmsg_049}\n\n${current_version}\n\n${glmsg_050}\nglab auth login\n\n${glmsg_051}"
else
dialog_error_box "${head_error}" "${glmsg_052}"
exit 1
fi
}
# Ejecutar función principal
main "$@"
# Limpiar la pantalla al finalizar
clear
exit 0