#!/usr/bin/env bash # ============================================ # Detection # ============================================ function platform::detect() { case "$(uname -s)" in Linux*) PLATFORM="linux" ;; Darwin*) PLATFORM="macos" ;; CYGWIN*|MINGW*|MSYS*) PLATFORM="windows" ;; *) PLATFORM="unknown" ;; esac platform::_apply_platform_guards } function platform::is_macos() { [[ "$PLATFORM" == "macos" ]]; } function platform::is_linux() { [[ "$PLATFORM" == "linux" ]]; } function platform::is_windows() { [[ "$PLATFORM" == "windows" ]]; } # ============================================ function platform::_apply_platform_guards() { if platform::is_windows; then export MSYS_NO_PATHCONV=1 export MSYS2_ARG_CONV_EXCL="*" fi } # ============================================ function platform::elevation_fail() { if platform::is_windows; then echo "Action must be run as Administrator" else echo "Action must be run with sudo or as root" fi } function platform::hosts_file() { local hostsPath='/etc/hosts' if platform::is_windows; then hostsPath='/c/Windows/System32/drivers/etc/hosts' fi echo $hostsPath } function platform::sudo() { if platform::is_windows; then "$@" else [[ $EUID -ne 0 ]] && sudo "$1" "$@" fi } function platform::require_privileges() { if platform::is_windows; then if ! net session >/dev/null 2>&1; then return 1 fi else platform::sudo -n true 2>/dev/null || platform::sudo true || return 1 fi } # ============================================ # Docker # ============================================ function platform::docker_path() { local path="$1" if platform::is_windows; then cygpath -m "$path" else echo "$path" fi } # ============================================ # Platform functions # ============================================ # Detect GNU realpath (supports --relative-to) function platform::has_gnu_realpath() { realpath --help 2>&1 | grep -q -- '--relative-to' } # Detect grealpath (Homebrew GNU coreutils) function platform::has_grealpath() { command -v grealpath >/dev/null 2>&1 } # Detect python3 function platform::has_python() { command -v python3 >/dev/null 2>&1 }