wgctl/core/framework/hook.sh
Nuno Duque Nunes 8ed491313d feat: command framework + logs migration
- core/framework/flag.sh: flag::define, flag::parse, accessors
- core/framework/hook.sh: hook::on, hook::fire, hook::off, hook::has
- core/framework/help.sh: help::section, command::help::auto
- core/framework/command.sh: command::define, command::route, lazy loading
- core restructure: framework/ + app/ separation
- load_command: directory-based command detection
- command::exists: accepts new-style commands
- command::run: routing for new-style, legacy fallback
- commands/logs/: migrated to new framework
  - logs.sh: router + command::define
  - show.sh: flag::define + flag::parse, no manual case blocks
  - clean.sh: flag::define + flag::parse
  - remove.sh: flag::define + flag::parse
  - rotate.sh: flag::define + flag::parse
- logs clean: fix dry_run bool to int conversion
- ctx::json_helper: fixed path after core restructure
- PYTHONPATH: exported in app/core.sh
2026-05-30 03:44:08 +00:00

47 lines
No EOL
1.2 KiB
Bash

#!/usr/bin/env bash
# core/framework/hook.sh
#
# Simple callback registry — fire named hooks, register handlers.
#
# Usage:
# hook::on "command:help" my_handler_function
# hook::off "command:help" my_handler_function
# hook::fire "command:help" arg1 arg2
#
# If no handler registered, hook::fire returns 0 silently.
# hook::fire returns the exit code of the handler.
# Registry: event_name → handler_function
declare -gA _HOOK_REGISTRY=()
# hook::on <event> <handler>
# Register a handler for an event. Replaces existing handler.
function hook::on() {
local event="${1:-}" handler="${2:-}"
[[ -z "$event" || -z "$handler" ]] && return 1
_HOOK_REGISTRY["$event"]="$handler"
}
# hook::off <event>
# Remove handler for an event.
function hook::off() {
local event="${1:-}"
[[ -z "$event" ]] && return 1
unset "_HOOK_REGISTRY[$event]"
}
# hook::fire <event> [args...]
# Call registered handler with args. Silent no-op if no handler.
function hook::fire() {
local event="${1:-}"
shift || true
local handler="${_HOOK_REGISTRY[$event]:-}"
[[ -z "$handler" ]] && return 0
"$handler" "$@"
}
# hook::has <event>
# Returns 0 if a handler is registered for event.
function hook::has() {
[[ -n "${_HOOK_REGISTRY[$1]:-}" ]]
}