# os.sh — OS/distro detection, command_exists, version_ge # Depends-On: colors.sh # Requires-Vars: (none) # Sets-Vars: $OS $DISTRO # Include via: @include lib/os.sh # Sets OS (macOS | Linux | Windows | Unknown) and DISTRO (ubuntu | debian | …) detect_os() { case "$(uname -s)" in Linux*) OS=Linux ;; Darwin*) OS=macOS ;; CYGWIN*) OS=Windows ;; MINGW*) OS=Windows ;; *) OS=Unknown ;; esac print_info "Detected OS: $OS" if [ "$OS" = "Linux" ] && [ -f /etc/os-release ]; then # shellcheck source=/dev/null . /etc/os-release DISTRO=$ID print_info "Detected Linux distribution: $DISTRO" else DISTRO=unknown fi } # Returns 0 if the given command is on PATH command_exists() { command -v "$1" >/dev/null 2>&1; } # Boolean helpers — use these in business logic instead of inline string comparisons is_macos() { [ "$OS" = "macOS" ]; } is_linux_apt() { [ "$OS" = "Linux" ] && { [ "$DISTRO" = "ubuntu" ] || [ "$DISTRO" = "debian" ]; }; } # Returns 0 (true) if $1 >= $2 (semantic version comparison) version_ge() { printf '%s\n%s\n' "$2" "$1" | sort -V -C; } # Assert that the current OS/distro is supported (macOS or Ubuntu/Debian). # Optional $1: hint message printed on failure (e.g. manual install instructions). # Exits with code 1 on unsupported OS or distro. assert_supported_os() { local hint="${1:-}" if [ "$OS" = "Linux" ]; then if [ "$DISTRO" = "ubuntu" ] || [ "$DISTRO" = "debian" ]; then return 0 fi print_error "Unsupported Linux distribution: $DISTRO" [ -n "$hint" ] && print_info "$hint" exit 1 elif [ "$OS" = "macOS" ]; then return 0 else print_error "Unsupported OS: $OS" [ -n "$hint" ] && print_info "$hint" exit 1 fi }