#!/usr/bin/env bash # ShFlow Environment Checker # License: GPLv3 # Author: Luis GuLo # Version: 1.5.1 set -e PROJECT_ROOT="${SHFLOW_HOME:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}" MODULE_PATHS=( "$PROJECT_ROOT/core/modules" "$PROJECT_ROOT/user_modules" "$PROJECT_ROOT/community_modules" ) # 🧩 Cargar funciones comunes si no están disponibles COMMON_LIB="$PROJECT_ROOT/core/lib/translate_msg.sh" if ! declare -f render_msg &>/dev/null; then [[ -f "$COMMON_LIB" ]] && source "$COMMON_LIB" fi # 🔧 yq segun arquitectura ARCH=$(uname -m) case "$ARCH" in x86_64) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_amd64" ;; i686|i386) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_386" ;; aarch64) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_arm64" ;; armv7l|armv6l) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_arm" ;; *) echo "❌ Arquitectura no soportada: $ARCH"; exit 1 ;; esac GLOBAL_TOOLS=("bash" "ssh" "scp" "git" "curl" "jq" "$YQ_BIN" "gpg") REQUIRED_PATHS=( "$PROJECT_ROOT/core/modules" "$PROJECT_ROOT/core/utils" "$PROJECT_ROOT/core/inventory" "$PROJECT_ROOT/examples" "$PROJECT_ROOT/user_modules" "$PROJECT_ROOT/community_modules" "$PROJECT_ROOT/shflow.sh" "$PROJECT_ROOT/vault.sh" ) # 📦 Variables Globales declare -A shflow_vars GLOBAL_VARS="$PROJECT_ROOT/core/inventory/vars/all.yaml" # 🌐 Cargar traducciones lang="${shflow_vars[language]:-es}" trfile="$PROJECT_ROOT/core/utils/shflow-check.tr.${lang}" declare -A tr if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi check_global_tools() { echo "${tr[tools_header]:-🔍 Verificando herramientas globales...}" local missing=0 for tool in "${GLOBAL_TOOLS[@]}"; do if ! command -v "$tool" &> /dev/null; then echo "$(render_msg "${tr[tool_missing]}" "tool=$tool")" missing=1 else tool=$(basename $tool) echo "$(render_msg "${tr[tool_ok]}" "tool=$tool")" fi done return $missing } check_structure() { echo "" echo "${tr[structure_header]:-📁 Verificando estructura de ShFlow...}" local missing=0 for path in "${REQUIRED_PATHS[@]}"; do if [ ! -e "$path" ]; then echo "$(render_msg "${tr[path_missing]}" "path=$path")" missing=1 else echo "$(render_msg "${tr[path_ok]}" "path=$path")" fi done return $missing } load_and_check_modules() { echo "" echo "${tr[modules_header]:-🔍 Verificando módulos ShFlow...}" for dir in "${MODULE_PATHS[@]}"; do [ -d "$dir" ] || continue while IFS= read -r -d '' mod; do source "$mod" done < <(find "$dir" -type f -name "*.sh" -print0) done for func in $(declare -F | awk '{print $3}' | grep '^check_dependencies_'); do echo "" echo "$(render_msg "${tr[checking_func]}" "func=$func")" $func || echo "$(render_msg "${tr[func_warn]}" "func=$func")" done } # 📦 Carga las variables globales de all.yaml load_global_vars() { if [[ -f "$GLOBAL_VARS" ]]; then echo "${tr[vars_loading]:-📦 Cargando variables globales de all.yaml...}" # Carga todas las claves del YAML GLOBAL_KEYS=$("$YQ_BIN" eval 'keys[]' "$GLOBAL_VARS" -r 2>/dev/null || true) for key in $GLOBAL_KEYS; do # Carga el valor en modo raw para Bash raw_value=$("$YQ_BIN" eval ".\"$key\"" "$GLOBAL_VARS" -r 2>/dev/null || true) # Si el valor es una lista o multilínea, yq lo habrá cargado con saltos de línea, lo almacenamos tal cual. # Si es un valor simple, lo almacenamos. shflow_vars["$key"]="$raw_value" done echo "$(render_msg "${tr[vars_loaded]:-✅ Variables cargadas: {count}}" "count=${#shflow_vars[@]}")" # Si la variable 'language' se ha cargado, actualizamos las traducciones del check. local new_lang="${shflow_vars[language]:-es}" if [[ "$new_lang" != "$lang" ]]; then lang="$new_lang" trfile="$PROJECT_ROOT/core/utils/shflow-check.tr.${lang}" if [[ -f "$trfile" ]]; then declare -A tr # Reset array while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile" fi fi else echo "$(render_msg "${tr[vars_missing]:-⚠️ all.yaml no encontrado en {path}}" "path=$GLOBAL_VARS")" fi } # 🧠 Mostrar variables cargadas show_global_vars() { echo "" echo "${tr[debug_vars_header]:-🌐 VARIABLES GLOBALES CARGADAS:}" echo "=====================================" local count=0 for key in "${!shflow_vars[@]}"; do local display_value="${shflow_vars[$key]}" # Formateo básico para listas para una mejor lectura en la consola if [[ "$key" == "ntp_servers" || "$key" == "default_packages" ]]; then # Reemplaza los saltos de línea por comas y espacio display_value=$(echo "$display_value" | tr '\n' ' ' | xargs | sed 's/ /\//g') fi echo "- $key: $display_value" count=$((count + 1)) done echo "-------------------------------------" echo "$(render_msg "${tr[debug_vars_count]:-Total de variables: {count}" "count=$count")" } main() { echo "${tr[title]:-🧪 ShFlow Environment Check}" echo "${tr[separator]:-=============================}" load_global_vars check_global_tools check_structure load_and_check_modules show_global_vars echo "" echo "${tr[done]:-✅ Verificación completada.}" } main "$@"