wgctl/core/framework/utils.sh
Nuno Duque Nunes 290ac24d88 refactor: core directory restructure
- core/framework/: flag.sh, hook.sh, help.sh, command.sh, mixin.sh
- core/app/: wgctl-specific context.sh, json.sh
- core/framework/mixins/: json_output, no_color mixins
- core/core.sh: sources framework/core.sh + app/core.sh
- PYTHONPATH exported in app/core.sh for lib/ module resolution
- command::_load_mixins: uses _FRAMEWORK_DIR for mixin path
2026-05-30 02:50:43 +00:00

68 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# ============================================
# Core — shell introspection
# ============================================
function core::function_exists() { declare -F "$1" >/dev/null 2>&1; }
function core::variable_exists() { declare -p "$1" &>/dev/null; }
function core::array_exists() { [[ "$(declare -p "$1" 2>/dev/null)" == "declare -a"* ]]; }
function core::call_if_exists() {
local fn="$1"
shift
if declare -F "$fn" >/dev/null 2>&1; then
"$fn" "$@"
fi
return 0
}
WGCTL_QUIET=false
function core::set_quiet() { WGCTL_QUIET=true; }
function core::is_quiet() { [[ "$WGCTL_QUIET" == "true" ]]; }
# ============================================
# Utility
# ============================================
function util::require_flag() {
local flag="$1" value="$2"
if [[ -z "$value" ]]; then
log::error "Flag ${flag} requires a value"
return 1
fi
}
# ============================================
# String
# ============================================
function string::trim() { echo "$1" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'; }
function string::to_lower() { echo "$1" | tr '[:upper:]' '[:lower:]'; }
function string::to_upper() { echo "$1" | tr '[:lower:]' '[:upper:]'; }
function string::contains() { [[ "$1" == *"$2"* ]]; }
function string::starts_with() { [[ "$1" == "$2"* ]]; }
function string::ends_with() { [[ "$1" == *"$2" ]]; }
function string::is_empty() { [[ -z "$1" ]]; }
# ============================================
# System
# ============================================
function system::require_root() {
if [[ "$EUID" -ne 0 ]]; then
log::error "wgctl must be run as root"
exit 1
fi
}
function system::command_exists() {
command -v "$1" >/dev/null 2>&1
}
function system::require_command() {
if ! system::command_exists "$1"; then
log::error "Required command not found: $1"
exit 1
fi
}