#!/usr/bin/env bash
set -euo >/dev/null 2>&1
info() { echo -e "\033[36m=== $@ ===\033[0m"; }
success() { echo -e "\033[32m✅ $@\033[0m"; }
warning() { echo -e "\033[33m⚠️ $@\033[0m"; }
error() { echo -e "\033[31m❌ $@\033[0m"; exit 1; }
detect_os() {
local os_type=$(uname -s)
case "${os_type}" in
Darwin)
DEPLOY_VARS["OS_TYPE"]="macos"
;;
Linux)
DEPLOY_VARS["OS_TYPE"]="linux"
;;
MINGW*|MSYS*|CYGWIN*)
DEPLOY_VARS["OS_TYPE"]="windows"
;;
*)
error "Unsupported OS: ${os_type}"
;;
esac
info "Operating System: ${DEPLOY_VARS["OS_TYPE"]}"
}
generate_random_chars() {
local chars="abcdefghijklmnopqrstuvwxyz0123456789"
local char_count=${#chars}
local random_str=""
local i=0
local os_type=${DEPLOY_VARS["OS_TYPE"]}
while [ $i -lt 5 ]; do
local random_idx=0
case "${os_type}" in
macos)
random_idx=$(( $(jot -r 1 0 32767) % 36 ))
;;
linux)
random_idx=$(head -c 2 /dev/urandom | od -An -tu2 | awk '{print $1 % 36}')
;;
windows)
random_idx=$((RANDOM % 36))
;;
esac
random_str+=${chars:$random_idx:1}
i=$((i + 1))
done
echo "$random_str"
}
get_public_ip() {
local local_ip=""
local os_type=${DEPLOY_VARS["OS_TYPE"]}
local cmd=${ARGS["CMD"]}
if [ -n "${DEPLOY_VARS["IP"]:-}" ]; then
info "Predefined IP address detected: ${DEPLOY_VARS["IP"]}"
return
fi
if [ "${cmd}" == "down" ]; then
return
fi
case "${os_type}" in
macos)
local_ip=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | head -n 1 | awk '{print $2}')
;;
linux)
local default_interface=$(ip route show default | awk '/default/ {print $5; exit}')
if [ -n "${default_interface}" ]; then
local_ip=$(ip addr show "${default_interface}" | awk '/inet / {gsub(/\/.*/, "", $2); print $2; exit}')
fi
if [ -z "${local_ip}" ]; then
local_ip=$(hostname -I | awk '{print $1}')
fi
;;
windows)
local_ip=$(powershell -Command "& { \
Get-NetIPAddress | \
Where-Object { \
\$_.AddressFamily -eq 'IPv4' -and \
!\$_.IPAddress.StartsWith('127.') -and \
!\$_.IPAddress.StartsWith('169.254.') -and \
\$_.InterfaceAlias -match 'WLAN|Ethernet' -and \
\$_.InterfaceAlias -notmatch 'Bluetooth|Hyper-V|WSL|Virtual|vEthernet' \
} | \
Select-Object -ExpandProperty IPAddress -First 1
}" 2>/dev/null)
local_ip=$(echo "${local_ip}" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1)
;;
esac
DEPLOY_VARS["IP"]=${local_ip}
}
print_array() {
local array_name="$1"
local -n arr_ref="$1"
echo -e "\033[33m$ ${array_name}\033[0m"
if [[ ! "$(declare -p ${array_name})" =~ "declare -a" && ! "$(declare -p ${array_name})" =~ "declare -A" ]]; then
echo -e "\033[31m[ERROR] ${array_name} is not a bash array variable!\033[0m"
return
fi
for key in "${!arr_ref[@]}"; do
echo -e "\033[36m ├─ ${array_name}[${key}] = ${arr_ref[${key}]}\033[0m"
done
echo -e "\033[33m └─ Total elements count: ${#arr_ref[@]}\033[0m\n"
}
format_dir_str() {
local original_str="$1"
local lower_str="${original_str,,}"
local final_str="${lower_str//_/-}"
echo "${final_str}"
}
set_if_empty() {
local -n vars=$1
local key=$2
local value=$3
if [ -z "${vars["${key}"]:-}" ]; then
vars["${key}"]="${value}"
fi
}