From 92dd54059f9b7b9e4bd909ad5acd82beb9ce0be0 Mon Sep 17 00:00:00 2001 From: "Mauro Rosero P." Date: Sat, 15 Mar 2025 12:25:19 -0500 Subject: [PATCH] [IMPROVED] Refactorizado script como cversion_token.sh con enfoque simplificado MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 馃 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- README.md | 18 +- bin/{version_cli.sh => cversion_token.sh} | 304 ++++++++-------------- 2 files changed, 113 insertions(+), 209 deletions(-) rename bin/{version_cli.sh => cversion_token.sh} (54%) diff --git a/README.md b/README.md index b991d16..cd5e6ca 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ bin/update.sh | `bin/ghcli_install.sh` | Instala GitHub CLI (gh) con soporte multiplataforma | | `bin/glcli_install.sh` | Instala GitLab CLI (glab) con m煤ltiples m茅todos de instalaci贸n | | `bin/fjcli_install.sh` | Instala Forgejo/Codeberg CLI v铆a Cargo | -| `bin/version_cli.sh` | Men煤 para gestionar herramientas de control de versiones y sus tokens | +| `bin/cversion_token.sh` | Men煤 para gestionar tokens de control de versiones cifrados con SOPS | ### Seguridad y Acceso @@ -185,19 +185,19 @@ El script instala la CLI mediante Cargo (el gestor de paquetes de Rust): - Verifica si Cargo est谩 instalado y lo instala si es necesario - Instala codeberg-cli que es compatible con servidores Forgejo -#### Gestor unificado de CLIs de control de versiones +#### Gestor de tokens de control de versiones ```bash -# Men煤 interactivo para gesti贸n de CLIs -bin/version_cli.sh +# Men煤 interactivo para gesti贸n de tokens +bin/cversion_token.sh ``` -Este script proporciona un men煤 interactivo para gestionar todas las CLIs de control de versiones: +Este script proporciona un men煤 interactivo para gestionar los tokens de acceso para plataformas de control de versiones: +- Selecci贸n sencilla del tipo de plataforma (GitHub, GitLab o Forgejo/Codeberg) - Verifica si las CLIs est谩n instaladas y ofrece instalarlas si faltan -- Configura y almacena tokens de acceso cifrados con SOPS -- Verifica el estado de configuraci贸n de las herramientas -- Prueba la conectividad con los servicios respectivos -- Compatible con GitHub, GitLab y Forgejo/Codeberg +- Configura y almacena tokens de acceso cifrados con SOPS en ~/.developer +- Ofrece la opci贸n de probar la conectividad con cada servicio +- Permite configurar m煤ltiples tokens secuencialmente Para aprender a usar Codeberg CLI (compatible con Forgejo), consulta la [documentaci贸n en el repositorio de Codeberg CLI](https://codeberg.org/RobWalt/codeberg-cli). diff --git a/bin/version_cli.sh b/bin/cversion_token.sh similarity index 54% rename from bin/version_cli.sh rename to bin/cversion_token.sh index 6672913..db32638 100755 --- a/bin/version_cli.sh +++ b/bin/cversion_token.sh @@ -1,7 +1,7 @@ #!/bin/bash # ------------------------------------------------------------------ # [Author] Cortana Rosero One -# [Title] version_cli.sh - Gestor de CLIs de Control de Versiones +# [Title] cversion_token.sh - Gestor de Tokens de Control de Versiones # [Generated] Created by Claude Code (claude-3-7-sonnet-20250219) # # AGPL License @@ -22,11 +22,8 @@ source "$LIB_DIR/console.lib" source "$MSG_DIR/head.es" # Variables globales -title="GESTOR DE CLIs DE CONTROL DE VERSIONES" -apps_title="Gesti贸n de CLIs Git" -SELECTED_CLI="" -TOKEN_FILE="" -SELECTED_CLI_NAME="" +title="GESTOR DE TOKENS DE CONTROL DE VERSIONES" +apps_title="Configuraci贸n de Tokens Git" SOPS_CONFIG_DIR="$DEVELOPER_DIR" # Verificar que SOPS est茅 instalado @@ -45,55 +42,6 @@ check_dialog() { 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 @@ -104,115 +52,94 @@ check_token_directory() { chmod 700 "$SOPS_CONFIG_DIR" } -# Men煤 principal -show_main_menu() { +# Verificar e instalar CLI seg煤n tipo seleccionado +manage_version_token() { + local vcs_type="" + local cli_command="" + local token_file="" + local install_script="" + local token_prompt="" + local token_helper="" + local token_stored=0 + + # Mostrar men煤 para seleccionar tipo de control de versiones dialog_input_menu "Seleccione la plataforma de control de versiones:" \ - "Seleccione la plataforma para la cual desea configurar el CLI" \ + "Seleccione la plataforma para la cual desea configurar el token" \ "1 GitHub 2 GitLab 3 Forgejo" \ 12 70 if [ $codex -ne 0 ]; then - exit 0 + return 1 fi + # Configurar variables seg煤n la selecci贸n case "$value" in "1") - SELECTED_CLI="gh" - SELECTED_CLI_NAME="GitHub" - TOKEN_FILE="$SOPS_CONFIG_DIR/github.sops.yaml" + vcs_type="GitHub" + cli_command="gh" + token_file="$SOPS_CONFIG_DIR/github.sops.yaml" + install_script="$SCRIPT_DIR/ghcli_install.sh" + 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.)" ;; "2") - SELECTED_CLI="glab" - SELECTED_CLI_NAME="GitLab" - TOKEN_FILE="$SOPS_CONFIG_DIR/gitlab.sops.yaml" + vcs_type="GitLab" + cli_command="glab" + token_file="$SOPS_CONFIG_DIR/gitlab.sops.yaml" + install_script="$SCRIPT_DIR/glcli_install.sh" + 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.)" ;; "3") - SELECTED_CLI="berg" - SELECTED_CLI_NAME="Forgejo" - TOKEN_FILE="$SOPS_CONFIG_DIR/forgejo.sops.yaml" + vcs_type="Forgejo" + cli_command="berg" + token_file="$SOPS_CONFIG_DIR/forgejo.sops.yaml" + install_script="$SCRIPT_DIR/fjcli_install.sh" + 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" ;; *) - exit 0 + return 1 ;; 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 + # Verificar si el CLI est谩 instalado + if ! command -v "$cli_command" &> /dev/null; then + dialog_yesno "El CLI de $vcs_type ($cli_command) no est谩 instalado.\n驴Desea instalarlo ahora?" 8 60 + if [ $? -eq 0 ]; then + # Instalar el CLI si el usuario lo confirma + if [ -f "$install_script" ]; then + clear + echo "Instalando CLI de $vcs_type..." + bash "$install_script" + if [ $? -ne 0 ]; then + dialog --backtitle "${title}" --title "${head_error}" --msgbox "No se pudo instalar el CLI de $vcs_type. Verifique los mensajes de error e intente nuevamente." 8 70 + return 1 + fi + echo "Presione Enter para continuar..." + read + else + dialog --backtitle "${title}" --title "${head_error}" --msgbox "No se encontr贸 el script de instalaci贸n para $vcs_type en: $install_script" 8 70 + return 1 + fi + else + dialog --backtitle "${title}" --title "${head_info}" --msgbox "El CLI es necesario para interactuar con $vcs_type.\nEl token se guardar谩 de todas formas, pero no podr谩 verificar la conexi贸n." 8 70 + fi 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 [ -f "$token_file" ]; then + token_stored=1 + dialog_yesno "Ya existe un token configurado para $vcs_type.\n驴Desea reemplazarlo?" 8 60 if [ $? -ne 0 ]; then - handle_cli_selection - return + return 0 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 + return 1 fi local token="$value" @@ -220,87 +147,56 @@ configure_token() { # Validar el token if [ -z "$token" ]; then dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_055}" 7 50 - configure_token - return + return 1 fi if [ ${#token} -lt 20 ]; then - dialog --backtitle "${title}" --title "${head_warning}" --msgbox "${npm_056}" 7 60 + dialog_yesno "${npm_056}\n\n驴Desea continuar de todas formas?" 8 70 + if [ $? -ne 0 ]; then + return 1 + fi 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" + sops --encrypt "/tmp/token_temp.yaml" > "$token_file" 2>/dev/null # 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 + # Verificar resultado de la encriptaci贸n 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="" + chmod 600 "$token_file" - 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 + dialog --backtitle "${title}" --title "${npm_052}" --msgbox "El token de $vcs_type ha sido encriptado exitosamente en:\n$token_file" 8 70 - status_message="Estado de configuraci贸n para $SELECTED_CLI_NAME:\n\n$config_status" + # Probar conexi贸n si el CLI est谩 instalado + if command -v "$cli_command" &> /dev/null; then + dialog_yesno "驴Desea probar la conexi贸n con $vcs_type usando el token configurado?" 8 60 + if [ $? -eq 0 ]; then + test_connection "$vcs_type" "$cli_command" "$token_file" + fi + fi + else + dialog --backtitle "${title}" --title "${head_error}" --msgbox "Fall贸 la encriptaci贸n del token de $vcs_type." 7 50 + return 1 fi - dialog --backtitle "${title}" --title "Verificaci贸n de configuraci贸n" --msgbox "$status_message" 15 70 - handle_cli_selection + return 0 } -# Probar conexi贸n +# Probar conexi贸n con el servicio test_connection() { + local vcs_type="$1" + local cli_command="$2" + local token_file="$3" local connection_status="" local connection_message="" - case "$SELECTED_CLI_NAME" in + dialog --backtitle "${title}" --title "Prueba de conexi贸n" --infobox "Probando conexi贸n con $vcs_type..." 5 60 + + case "$vcs_type" in "GitHub") if command -v gh &> /dev/null; then connection_status=$(gh api user 2>&1) @@ -330,8 +226,8 @@ test_connection() { "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 [ -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 @@ -352,8 +248,7 @@ test_connection() { ;; esac - dialog --backtitle "${title}" --title "Prueba de conexi贸n" --msgbox "$connection_message" 15 70 - handle_cli_selection + dialog --backtitle "${title}" --title "Resultado de la prueba" --msgbox "$connection_message" 15 70 } # Funci贸n principal @@ -363,8 +258,17 @@ main() { check_sops check_token_directory - # Mostrar men煤 principal - show_main_menu + # Ejecutar la funci贸n principal + manage_version_token + + # Preguntar si desea configurar otro token + while true; do + dialog_yesno "驴Desea configurar otro token de control de versiones?" 7 60 + if [ $? -ne 0 ]; then + break + fi + manage_version_token + done } # Ejecutar funci贸n principal