[IMPROVED] Actualizar cversion_token.sh para usar funciones de console.lib
- Reemplazar llamadas directas a dialog por funciones de console.lib - Simplificar el flujo de diálogo para solicitar tokens - Mejorar manejo de errores utilizando métodos estándar - Mostrar mensajes i18n adecuados de head.es y developers.es 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
64c32003fe
commit
d5b3b1bc31
1 changed files with 245 additions and 0 deletions
245
bin/cversion_token.sh
Executable file
245
bin/cversion_token.sh
Executable file
|
@ -0,0 +1,245 @@
|
||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# Script: cversion_token.sh
|
||||||
|
# Description: Administrador de tokens para plataformas de control de versiones
|
||||||
|
# Created: 2025/03/15 10:07:08
|
||||||
|
# Modified: 2025/03/19 11:57:08
|
||||||
|
# [Author] Cortana Rosero One <cortana@rosero.one>
|
||||||
|
# [Generated] Created by Claude Code (claude-3-7-sonnet-20250219)
|
||||||
|
#
|
||||||
|
# Derechos de Autor (C) [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}/console.lib"
|
||||||
|
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/developers.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}" "console"
|
||||||
|
load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "developers"
|
||||||
|
title="${head_000} ${head_002}"
|
||||||
|
apps_title="${cvmsg_000}"
|
||||||
|
|
||||||
|
# Función para verificar si SOPS está instalado
|
||||||
|
check_sops_installed() {
|
||||||
|
if ! command -v sops &> /dev/null; then
|
||||||
|
# Mostrar el error usando dialog con mensaje desde developers.es
|
||||||
|
dialog_error_box "${head_error}" "${cvmsg_013}. ${cvmsg_014}"
|
||||||
|
clear
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Función para asegurar que el directorio .developer existe
|
||||||
|
ensure_developers_dir() {
|
||||||
|
if [ ! -d "$DEVELOPER_DIR" ]; then
|
||||||
|
mkdir -p "$DEVELOPER_DIR"
|
||||||
|
chmod 700 "$DEVELOPER_DIR"
|
||||||
|
echo "${cvmsg_008} $DEVELOPER_DIR"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Función para seleccionar la plataforma de control de versiones usando menu_actions
|
||||||
|
select_platform() {
|
||||||
|
# Preparar opciones para menu_actions
|
||||||
|
local menu_options="1:${cvmsg_010} 2:${cvmsg_011} 3:${cvmsg_012}"
|
||||||
|
|
||||||
|
# Usar menu_actions en lugar de dialog_input_menu
|
||||||
|
local selection=$(menu_actions "${cvmsg_002}" "$menu_options" 9)
|
||||||
|
|
||||||
|
# Verificar si hubo cancelación o error
|
||||||
|
if [ "$selection" = "${head_key_end}" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Interpretar la selección
|
||||||
|
case $selection in
|
||||||
|
1) echo "github" ;;
|
||||||
|
2) echo "gitlab" ;;
|
||||||
|
3) echo "forgejo" ;;
|
||||||
|
*) return 1 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Función para solicitar y validar el token
|
||||||
|
request_token() {
|
||||||
|
local platform=$1
|
||||||
|
local platform_name=""
|
||||||
|
local token=""
|
||||||
|
|
||||||
|
case $platform in
|
||||||
|
"github") platform_name="${cvmsg_010}" ;;
|
||||||
|
"gitlab") platform_name="${cvmsg_011}" ;;
|
||||||
|
"forgejo") platform_name="${cvmsg_012}" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Bucle para validación de token usando funciones de console.lib
|
||||||
|
while true; do
|
||||||
|
# Usar dialog_input_pass de console.lib
|
||||||
|
dialog_input_pass "${cvmsg_004} $platform_name" "${cvmsg_005}"
|
||||||
|
|
||||||
|
# Verificar si el usuario canceló
|
||||||
|
if [ $codex -ne 0 ]; then
|
||||||
|
clear
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Obtener token de la variable value establecida por dialog_input_pass
|
||||||
|
token="$value"
|
||||||
|
|
||||||
|
# Validar token
|
||||||
|
if [ -z "$token" ]; then
|
||||||
|
dialog_error_box "${head_error}" "${cvmsg_015}"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Verificar longitud (debería tener al menos 30 caracteres)
|
||||||
|
if [ ${#token} -lt 30 ]; then
|
||||||
|
dialog_error_box "${head_error}" "${cvmsg_016}"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Verificar formato (debería contener caracteres alfanuméricos y algunos especiales)
|
||||||
|
if ! [[ "$token" =~ ^[A-Za-z0-9\#\-\_\.]+$ ]]; then
|
||||||
|
dialog_error_box "${head_warning}" "${cvmsg_017}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Token válido
|
||||||
|
break
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "$token"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Función para guardar el token encriptado con SOPS
|
||||||
|
save_token() {
|
||||||
|
local platform=$1
|
||||||
|
local token=$2
|
||||||
|
local filename="${DEVELOPER_DIR}/${platform}.sops.yaml"
|
||||||
|
local token_file="/tmp/vcs_token_$$"
|
||||||
|
|
||||||
|
# Convertir token a base64 para mayor seguridad
|
||||||
|
local token_base64=$(echo -n "$token" | base64)
|
||||||
|
|
||||||
|
# Crear archivo temporal con el token en base64
|
||||||
|
echo "token: $token_base64" > "$token_file"
|
||||||
|
|
||||||
|
# Encriptar el archivo con SOPS
|
||||||
|
sops --encrypt "$token_file" > "$filename"
|
||||||
|
local result=$?
|
||||||
|
|
||||||
|
# Eliminar archivo temporal
|
||||||
|
rm -f "$token_file"
|
||||||
|
|
||||||
|
# Establecer permisos adecuados (solo lectura para el propietario)
|
||||||
|
if [ $result -eq 0 ]; then
|
||||||
|
chmod 600 "$filename"
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $result
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verificar si dialog está instalado
|
||||||
|
check_dialog_installed() {
|
||||||
|
if ! command -v dialog &> /dev/null; then
|
||||||
|
echo "${head_001}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Función principal
|
||||||
|
main() {
|
||||||
|
# Verificar requisitos
|
||||||
|
check_dialog_installed
|
||||||
|
check_sops_installed
|
||||||
|
ensure_developers_dir
|
||||||
|
|
||||||
|
# Asegurar que head_000 y head_002 estén definidos para el título
|
||||||
|
if [ -z "$head_000" ] || [ -z "$head_002" ]; then
|
||||||
|
head_000="MRDevs"
|
||||||
|
head_002="Tools"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Mostrar título inicial usando funciones de console.lib
|
||||||
|
# Reemplazamos display_text_header por un diálogo de inicio
|
||||||
|
dialog_error_box "$title" "$apps_title - ${head_version} $VERSION"
|
||||||
|
|
||||||
|
# Seleccionar plataforma
|
||||||
|
platform=$(select_platform)
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
clear
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Solicitar token
|
||||||
|
token=$(request_token "$platform")
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
clear
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Guardar token
|
||||||
|
save_token "$platform" "$token"
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
local platform_name=""
|
||||||
|
case $platform in
|
||||||
|
"github") platform_name="${cvmsg_010}" ;;
|
||||||
|
"gitlab") platform_name="${cvmsg_011}" ;;
|
||||||
|
"forgejo") platform_name="${cvmsg_012}" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
dialog_error_box "${head_info}" "${cvmsg_006} $platform_name"
|
||||||
|
else
|
||||||
|
local platform_name=""
|
||||||
|
case $platform in
|
||||||
|
"github") platform_name="${cvmsg_010}" ;;
|
||||||
|
"gitlab") platform_name="${cvmsg_011}" ;;
|
||||||
|
"forgejo") platform_name="${cvmsg_012}" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
dialog_error_box "${head_error}" "${cvmsg_007} $platform_name"
|
||||||
|
fi
|
||||||
|
|
||||||
|
clear
|
||||||
|
}
|
||||||
|
|
||||||
|
# Ejecutar función principal
|
||||||
|
main
|
Loading…
Reference in a new issue