Files
fn_registry/bash/functions/infra/detect_wsl.sh
T
egutierrez 194663a379 feat: add bash infra installer and diagnostic functions
10 funciones Bash del dominio infra: instaladores de herramientas de desarrollo
(install_go, install_nodejs, install_pnpm, install_python312, install_uv,
install_volta, install_wails), diagnostico del sistema (analyze_disk_space,
detect_wsl, list_listening_ports). Automatizan la configuracion del entorno
de desarrollo en Linux/WSL.
2026-04-12 13:54:21 +02:00

98 lines
2.6 KiB
Bash

#!/usr/bin/env bash
# detect_wsl
# ----------
# Detecta si el sistema actual es WSL (Windows Subsystem for Linux).
# Con --check solo retorna exit code (0=WSL, 1=no WSL) sin output.
# Sin argumentos, imprime información completa del entorno WSL.
#
# USO:
# source detect_wsl.sh
# detect_wsl [--check]
detect_wsl() {
local check_only=false
[[ "${1:-}" == "--check" ]] && check_only=true
# Detección interna de WSL
_is_wsl() {
if [[ -f /proc/version ]] && grep -qi "microsoft\|wsl" /proc/version; then
return 0
fi
if [[ -f /proc/sys/kernel/osrelease ]] && grep -qi "microsoft\|wsl" /proc/sys/kernel/osrelease; then
return 0
fi
if [[ -d /mnt/c ]] && [[ -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then
return 0
fi
return 1
}
_get_wsl_version() {
if [[ -f /proc/version ]]; then
if grep -qi "WSL2" /proc/version; then
echo "WSL2"
elif grep -qi "microsoft" /proc/version; then
echo "WSL1"
else
echo "Unknown"
fi
else
echo "Unknown"
fi
}
_get_windows_username() {
if [[ -n "${WSLENV:-}" ]]; then
cmd.exe /c "echo %USERNAME%" 2>/dev/null | tr -d '\r\n' || echo "Unknown"
else
echo "Unknown"
fi
}
# Modo --check: solo exit code
if [[ "$check_only" == true ]]; then
_is_wsl && return 0 || return 1
fi
# Modo informativo
if ! _is_wsl; then
echo "detect_wsl: este sistema NO es WSL" >&2
return 1
fi
local wsl_version
wsl_version="$(_get_wsl_version)"
local win_user
win_user="$(_get_windows_username)"
local distro="Unknown"
if [[ -f /etc/os-release ]]; then
distro="$(. /etc/os-release && echo "${PRETTY_NAME:-${ID:-Unknown}}")"
fi
echo "=== Entorno WSL ==="
echo " Versión de WSL: ${wsl_version}"
echo " Usuario Windows: ${win_user}"
echo " Distribución: ${distro}"
echo " Hostname: $(hostname)"
echo ""
echo "=== Unidades de Windows montadas ==="
ls /mnt/ 2>/dev/null | grep -E "^[a-z]$" | while IFS= read -r drive; do
echo " ${drive}: → /mnt/${drive}"
done
echo ""
local current_win_path
current_win_path="$(wslpath -w "$(pwd)" 2>/dev/null || echo "N/A")"
echo "=== Directorio actual en Windows ==="
echo " ${current_win_path}"
echo ""
}
# Ejecutar si se invoca directamente
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
detect_wsl "$@"
fi