🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
366 lines
No EOL
14 KiB
Bash
Executable file
366 lines
No EOL
14 KiB
Bash
Executable file
#!/bin/bash
|
|
# ------------------------------------------------------------------
|
|
# [Author] Cortana Rosero One <cortana@rosero.one>
|
|
# [Title] cversion_token.sh - Gestor de Tokens 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 TOKENS DE CONTROL DE VERSIONES"
|
|
apps_title="Configuración de Tokens Git"
|
|
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
|
|
}
|
|
|
|
# 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"
|
|
}
|
|
|
|
# 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 token" \
|
|
"1 GitHub 2 GitLab 3 Forgejo" \
|
|
12 70
|
|
|
|
if [ $codex -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
# Configurar variables según la selección
|
|
case "$value" in
|
|
"1")
|
|
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 personal de GitHub:"
|
|
token_helper="Puede generar un nuevo token en: https://github.com/settings/tokens"
|
|
;;
|
|
"2")
|
|
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 personal de GitLab:"
|
|
token_helper="Puede generar un nuevo token en: https://gitlab.com/-/profile/personal_access_tokens"
|
|
;;
|
|
"3")
|
|
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 personal de Forgejo/Codeberg:"
|
|
token_helper="Puede generar un nuevo token en su perfil de usuario, en la sección 'Aplicaciones'"
|
|
;;
|
|
*)
|
|
return 1
|
|
;;
|
|
esac
|
|
|
|
# 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
|
|
|
|
# Verificar si ya existe un token configurado
|
|
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
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
# Mostrar pantalla de ayuda para tokens
|
|
show_token_help() {
|
|
local vcs_type="$1"
|
|
local help_text=""
|
|
|
|
case "$vcs_type" in
|
|
"GitHub")
|
|
help_text="AYUDA PARA TOKENS DE GITHUB\n\n"
|
|
help_text+="Para generar un token personal en GitHub:\n\n"
|
|
help_text+="1. Inicie sesión en GitHub y vaya a Configuración -> Configuración de desarrollador -> Tokens de acceso personal\n"
|
|
help_text+=" URL: https://github.com/settings/tokens\n\n"
|
|
help_text+="2. Haga clic en 'Generar nuevo token'\n\n"
|
|
help_text+="3. Proporcione una nota descriptiva para el token\n\n"
|
|
help_text+="4. Seleccione los permisos necesarios:\n"
|
|
help_text+=" - Para uso general: repo, workflow, read:org\n"
|
|
help_text+=" - Para acceso más limitado, solo seleccione lo que necesite\n\n"
|
|
help_text+="5. Haga clic en 'Generar token'\n\n"
|
|
help_text+="6. Copie el token inmediatamente (solo se muestra una vez)"
|
|
;;
|
|
"GitLab")
|
|
help_text="AYUDA PARA TOKENS DE GITLAB\n\n"
|
|
help_text+="Para generar un token personal en GitLab:\n\n"
|
|
help_text+="1. Inicie sesión en GitLab y vaya a Preferencias -> Tokens de acceso\n"
|
|
help_text+=" URL: https://gitlab.com/-/profile/personal_access_tokens\n\n"
|
|
help_text+="2. Proporcione un nombre para el token\n\n"
|
|
help_text+="3. Opcionalmente, establezca una fecha de vencimiento\n\n"
|
|
help_text+="4. Seleccione los permisos necesarios:\n"
|
|
help_text+=" - Para uso general: api, read_repository, write_repository\n"
|
|
help_text+=" - Para acceso más limitado, solo seleccione lo que necesite\n\n"
|
|
help_text+="5. Haga clic en 'Crear token de acceso personal'\n\n"
|
|
help_text+="6. Copie el token inmediatamente (solo se muestra una vez)"
|
|
;;
|
|
"Forgejo")
|
|
help_text="AYUDA PARA TOKENS DE FORGEJO/CODEBERG\n\n"
|
|
help_text+="Para generar un token personal en Forgejo o Codeberg:\n\n"
|
|
help_text+="1. Inicie sesión en su instancia de Forgejo o en Codeberg\n\n"
|
|
help_text+="2. Vaya a Configuración -> Aplicaciones\n\n"
|
|
help_text+="3. En la sección 'Generar nuevo token', proporcione un nombre\n\n"
|
|
help_text+="4. Seleccione los permisos necesarios (alcances):\n"
|
|
help_text+=" - Para uso general: repo, repo:status\n"
|
|
help_text+=" - Para acceso más limitado, solo seleccione lo que necesite\n\n"
|
|
help_text+="5. Haga clic en 'Generar token'\n\n"
|
|
help_text+="6. Copie el token inmediatamente (solo se muestra una vez)"
|
|
;;
|
|
esac
|
|
|
|
dialog --backtitle "${title}" --title "Ayuda sobre tokens de $vcs_type" --msgbox "$help_text" 20 75
|
|
}
|
|
|
|
# Función personalizada para solicitar token con botón de ayuda
|
|
token_password_dialog() {
|
|
local prompt="$1"
|
|
local helper="$2"
|
|
local vcs_type="$3"
|
|
local button=0
|
|
local token=""
|
|
|
|
while [ $button -ne 1 ]; do
|
|
# Crear diálogo temporal
|
|
tempfile=$(mktemp)
|
|
|
|
# Mostrar diálogo con 3 botones (Cancelar, OK, Ayuda)
|
|
dialog --backtitle "${title}" --title "${apps_title}" \
|
|
--passwordbox "\n$prompt\n$helper" 12 70 \
|
|
--extra-button --extra-label "Ayuda" 2>"$tempfile"
|
|
|
|
button=$?
|
|
|
|
# Procesar resultado según el botón presionado
|
|
case $button in
|
|
0) # OK
|
|
value=$(cat "$tempfile")
|
|
codex=0
|
|
rm -f "$tempfile"
|
|
return 0
|
|
;;
|
|
1) # Cancelar
|
|
codex=1
|
|
rm -f "$tempfile"
|
|
return 1
|
|
;;
|
|
3) # Botón extra (Ayuda)
|
|
rm -f "$tempfile"
|
|
show_token_help "$vcs_type"
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Solicitar token de acceso con botón de ayuda
|
|
token_password_dialog "$token_prompt" "$token_helper" "$vcs_type"
|
|
if [ $codex -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
local token="$value"
|
|
|
|
# Validar el token
|
|
if [ -z "$token" ]; then
|
|
dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_055}" 7 50
|
|
return 1
|
|
fi
|
|
|
|
if [ ${#token} -lt 20 ]; then
|
|
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"
|
|
sops --encrypt "/tmp/token_temp.yaml" > "$token_file" 2>/dev/null
|
|
|
|
# Eliminar archivo temporal
|
|
rm -f "/tmp/token_temp.yaml"
|
|
|
|
# Verificar resultado de la encriptación
|
|
if [ $? -eq 0 ]; then
|
|
# Establecer permisos adecuados
|
|
chmod 600 "$token_file"
|
|
|
|
dialog --backtitle "${title}" --title "${npm_052}" --msgbox "El token de $vcs_type ha sido encriptado exitosamente en:\n$token_file" 8 70
|
|
|
|
# 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
|
|
|
|
return 0
|
|
}
|
|
|
|
# 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=""
|
|
|
|
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)
|
|
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 "Resultado de la prueba" --msgbox "$connection_message" 15 70
|
|
}
|
|
|
|
# Función principal
|
|
main() {
|
|
# Verificar requisitos
|
|
check_dialog
|
|
check_sops
|
|
check_token_directory
|
|
|
|
# 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
|
|
main
|
|
|
|
exit 0 |