#!/bin/bash # ------------------------------------------------------------------ # [Author] Cortana Rosero One # [Title] version_cli.sh - Gestor de CLIs de Control de Versiones # [Generated] Created by Claude Code (claude-3-7-sonnet-20250219) # # AGPL License # Modified date: 14/03/2025 # ------------------------------------------------------------------ # Directorios base SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BASE_DIR="$(dirname "$SCRIPT_DIR")" CONFIG_DIR="$SCRIPT_DIR/config" LIB_DIR="$SCRIPT_DIR/lib" MSG_DIR="$SCRIPT_DIR/msg" DEVELOPER_DIR="$HOME/.developer" # Cargar libraries source "$LIB_DIR/base.lib" source "$LIB_DIR/console.lib" source "$MSG_DIR/head.es" # Variables globales title="GESTOR DE CLIs DE CONTROL DE VERSIONES" apps_title="Configuración de CLIs Git" SELECTED_CLI="" TOKEN_FILE="" SELECTED_CLI_NAME="" SOPS_CONFIG_DIR="$DEVELOPER_DIR" # Verificar que SOPS esté instalado check_sops() { if ! command -v sops &> /dev/null; then dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_051}" 7 50 exit 1 fi } # Verificar que dialog esté instalado check_dialog() { if ! command -v dialog &> /dev/null; then echo -e "${head_001}" exit 1 fi } # Verificar si un CLI está instalado check_cli_installed() { local cli_name="$1" local command_name="$2" if ! command -v "$command_name" &> /dev/null; then dialog_yesno "El CLI de $cli_name no está instalado.\n¿Desea instalarlo ahora?" 8 60 if [ $? -eq 0 ]; then install_cli "$cli_name" return $? else return 1 fi fi return 0 } # Instalar un CLI específico install_cli() { local cli_name="$1" local install_script="" case "$cli_name" in "GitHub") install_script="$SCRIPT_DIR/ghcli_install.sh" ;; "GitLab") install_script="$SCRIPT_DIR/glcli_install.sh" ;; "Forgejo") install_script="$SCRIPT_DIR/fjcli_install.sh" ;; esac if [ -f "$install_script" ]; then clear echo "Instalando CLI de $cli_name..." bash "$install_script" local result=$? echo "Presione Enter para continuar..." read return $result else dialog --backtitle "${title}" --title "${head_error}" --msgbox "No se encontró el script de instalación para $cli_name" 7 50 return 1 fi } # Crear directorio para tokens si no existe check_token_directory() { if [ ! -d "$SOPS_CONFIG_DIR" ]; then mkdir -p "$SOPS_CONFIG_DIR" fi # Asegurar que el directorio tenga permisos adecuados chmod 700 "$SOPS_CONFIG_DIR" } # Menú principal show_main_menu() { dialog_input_menu "Seleccione la plataforma de control de versiones:" \ "Seleccione la plataforma para la cual desea configurar el CLI" \ "1 GitHub 2 GitLab 3 Forgejo" \ 12 70 if [ $codex -ne 0 ]; then exit 0 fi case "$value" in "1") SELECTED_CLI="gh" SELECTED_CLI_NAME="GitHub" TOKEN_FILE="$SOPS_CONFIG_DIR/github.sops.yaml" ;; "2") SELECTED_CLI="glab" SELECTED_CLI_NAME="GitLab" TOKEN_FILE="$SOPS_CONFIG_DIR/gitlab.sops.yaml" ;; "3") SELECTED_CLI="berg" SELECTED_CLI_NAME="Forgejo" TOKEN_FILE="$SOPS_CONFIG_DIR/forgejo.sops.yaml" ;; *) exit 0 ;; esac handle_cli_selection } # Manejar la selección de CLI handle_cli_selection() { # Verificar si el CLI seleccionado está instalado check_cli_installed "$SELECTED_CLI_NAME" "$SELECTED_CLI" if [ $? -ne 0 ]; then # Si el usuario no quiere instalar el CLI, volver al menú principal show_main_menu return fi # Mostrar menú de acciones para el CLI seleccionado dialog_input_menu "Acciones para $SELECTED_CLI_NAME CLI:" \ "Seleccione la acción que desea realizar" \ "1 'Configurar token de acceso' 2 'Verificar configuración' 3 'Probar conexión' 4 'Volver al menú principal'" \ 12 70 if [ $codex -ne 0 ]; then show_main_menu return fi case "$value" in "1") configure_token ;; "2") verify_configuration ;; "3") test_connection ;; "4") show_main_menu ;; *) exit 0 ;; esac } # Configurar token de acceso configure_token() { # Verificar si ya existe un token configurado if [ -f "$TOKEN_FILE" ]; then dialog_yesno "Ya existe un token configurado para $SELECTED_CLI_NAME.\n¿Desea reemplazarlo?" 8 60 if [ $? -ne 0 ]; then handle_cli_selection return fi fi # Solicitar token de acceso local token_prompt="" local token_helper="" case "$SELECTED_CLI_NAME" in "GitHub") token_prompt="Ingrese su token de acceso personal de GitHub:" token_helper="Puede generar un nuevo token en: https://github.com/settings/tokens\nAsegúrese de incluir los permisos necesarios (repo, workflow, etc.)" ;; "GitLab") token_prompt="Ingrese su token de acceso personal de GitLab:" token_helper="Puede generar un nuevo token en: https://gitlab.com/-/profile/personal_access_tokens\nAsegúrese de incluir los permisos necesarios (api, read_repository, etc.)" ;; "Forgejo") token_prompt="Ingrese su token de acceso personal de Forgejo/Codeberg:" token_helper="Puede generar un nuevo token en su perfil de usuario, en la sección 'Aplicaciones'\nAsegúrese de incluir los permisos necesarios" ;; esac dialog_input_pass "$token_prompt" "$token_helper" if [ $codex -ne 0 ]; then handle_cli_selection return fi local token="$value" # Validar el token if [ -z "$token" ]; then dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_055}" 7 50 configure_token return fi if [ ${#token} -lt 20 ]; then dialog --backtitle "${title}" --title "${head_warning}" --msgbox "${npm_056}" 7 60 fi # Crear archivo YAML y encriptarlo con SOPS echo "creating_token: \"$token\"" > "/tmp/token_temp.yaml" encrypt_token "/tmp/token_temp.yaml" "$TOKEN_FILE" "$SELECTED_CLI_NAME" # Eliminar archivo temporal rm -f "/tmp/token_temp.yaml" handle_cli_selection } # Encriptar token con SOPS encrypt_token() { local temp_file="$1" local target_file="$2" local cli_name="$3" sops --encrypt "$temp_file" > "$target_file" 2>/dev/null if [ $? -eq 0 ]; then dialog --backtitle "${title}" --title "${npm_052}" --msgbox "El token de $cli_name ha sido encriptado exitosamente en\n$target_file" 8 70 # Establecer permisos adecuados chmod 600 "$target_file" else dialog --backtitle "${title}" --title "${head_error}" --msgbox "Falló la encriptación del token de $cli_name." 7 50 fi } # Verificar configuración verify_configuration() { local status_message="" if [ ! -f "$TOKEN_FILE" ]; then status_message="No se encontró un token configurado para $SELECTED_CLI_NAME.\nEl archivo $TOKEN_FILE no existe." else local config_status="" case "$SELECTED_CLI_NAME" in "GitHub") if command -v gh &> /dev/null; then config_status=$(gh auth status 2>&1 || echo "No autenticado") else config_status="CLI de GitHub no está instalado." fi ;; "GitLab") if command -v glab &> /dev/null; then config_status=$(glab auth status 2>&1 || echo "No autenticado") else config_status="CLI de GitLab no está instalado." fi ;; "Forgejo") if command -v berg &> /dev/null; then config_status="Token almacenado en: $TOKEN_FILE\nCifrado con SOPS." else config_status="CLI de Forgejo/Codeberg no está instalado." fi ;; esac status_message="Estado de configuración para $SELECTED_CLI_NAME:\n\n$config_status" fi dialog --backtitle "${title}" --title "Verificación de configuración" --msgbox "$status_message" 15 70 handle_cli_selection } # Probar conexión test_connection() { local connection_status="" local connection_message="" case "$SELECTED_CLI_NAME" in "GitHub") if command -v gh &> /dev/null; then connection_status=$(gh api user 2>&1) if [ $? -eq 0 ]; then local username=$(echo "$connection_status" | grep '"login"' | cut -d'"' -f4) connection_message="Conexión exitosa con GitHub.\nUsuario autenticado: $username" else connection_message="Error al conectar con GitHub:\n$connection_status" fi else connection_message="CLI de GitHub no está instalado." fi ;; "GitLab") if command -v glab &> /dev/null; then connection_status=$(glab api user 2>&1) if [ $? -eq 0 ]; then local username=$(echo "$connection_status" | grep '"username"' | cut -d'"' -f4) connection_message="Conexión exitosa con GitLab.\nUsuario autenticado: $username" else connection_message="Error al conectar con GitLab:\n$connection_status" fi else connection_message="CLI de GitLab no está instalado." fi ;; "Forgejo") if command -v berg &> /dev/null; then # Intentar usar el token de SOPS para la autenticación if [ -f "$TOKEN_FILE" ]; then local token=$(sops --decrypt "$TOKEN_FILE" 2>/dev/null | grep 'creating_token' | cut -d'"' -f2) if [ -n "$token" ]; then connection_status=$(curl -s -H "Authorization: token $token" "https://codeberg.org/api/v1/user" 2>&1) if echo "$connection_status" | grep -q '"username"'; then local username=$(echo "$connection_status" | grep '"username"' | cut -d'"' -f4) connection_message="Conexión exitosa con Forgejo/Codeberg.\nUsuario autenticado: $username" else connection_message="Error al conectar con Forgejo/Codeberg:\n$connection_status" fi else connection_message="No se pudo extraer el token del archivo cifrado." fi else connection_message="No se encontró un token configurado para Forgejo/Codeberg." fi else connection_message="CLI de Forgejo/Codeberg no está instalado." fi ;; esac dialog --backtitle "${title}" --title "Prueba de conexión" --msgbox "$connection_message" 15 70 handle_cli_selection } # Función principal main() { # Verificar requisitos check_dialog check_sops check_token_directory # Mostrar menú principal show_main_menu } # Ejecutar función principal main exit 0