🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
277 lines
No EOL
10 KiB
Bash
Executable file
277 lines
No EOL
10 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 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")
|
|
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")
|
|
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"
|
|
;;
|
|
*)
|
|
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
|
|
|
|
# Solicitar token de acceso
|
|
dialog_input_pass "$token_prompt" "$token_helper"
|
|
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 |