Raxol.Core.Runtime.Plugins.CommandRegistry (Raxol Core v2.4.0)

Copy Markdown View Source

Manages command registration and execution for plugins.

Commands are stored in a plain map keyed by namespace (module) with lists of {name, {module, function, arity}, metadata} tuples as values.

Summary

Functions

Finds and executes a command by name with timeout support.

Searches all namespaces for a command by name.

Looks up a command by namespace and name. Returns a handler function wrapping apply(module, function, ...).

Returns the default table name atom for ETS-backed registries.

Registers a command under a namespace in the command table.

Registers commands from a plugin module's commands/0 callback.

Removes a single command from a namespace. Returns the updated table, or the input unchanged if table is not a map.

Removes all commands for a module from the table.

Unregisters all commands for a plugin module.

Types

command_entry()

@type command_entry() ::
  {command_name(), {module(), atom(), non_neg_integer()}, command_metadata()}

command_handler()

@type command_handler() :: (list(), map() -> term())

command_metadata()

@type command_metadata() :: %{
  optional(:description) => String.t(),
  optional(:usage) => String.t(),
  optional(:aliases) => [String.t()],
  optional(:timeout) => pos_integer()
}

command_name()

@type command_name() :: String.t()

command_table()

@type command_table() :: %{optional(module()) => [command_entry()]}

Functions

execute_command(command_name, args, command_table)

@spec execute_command(String.t(), list(), command_table()) ::
  term() | {:error, term()}

Finds and executes a command by name with timeout support.

find_command(command_name, command_table)

@spec find_command(String.t(), command_table()) ::
  {:ok, {term(), command_metadata()}} | {:error, :not_found}

Searches all namespaces for a command by name.

lookup_command(table, namespace, command_name)

@spec lookup_command(command_table() | term(), module(), String.t()) ::
  {:ok, {module(), command_handler(), non_neg_integer()}}
  | {:error, :not_found | :invalid_table}

Looks up a command by namespace and name. Returns a handler function wrapping apply(module, function, ...).

new()

@spec new() :: atom()

Returns the default table name atom for ETS-backed registries.

register_command(table, namespace, command_name, module, function, arity)

@spec register_command(
  command_table() | term(),
  module(),
  String.t(),
  module(),
  atom(),
  non_neg_integer()
) :: command_table() | {:error, :invalid_table}

Registers a command under a namespace in the command table.

Returns the updated table map, or {:error, :invalid_table} if table is not a map.

register_plugin_commands(plugin_module, plugin_state, command_table)

@spec register_plugin_commands(module(), map(), command_table()) ::
  {:ok, command_table()} | {:error, term()}

Registers commands from a plugin module's commands/0 callback.

Validates handlers and metadata, checks for name conflicts against existing commands in the table.

unregister_command(table, namespace, command_name)

@spec unregister_command(command_table() | term(), module(), String.t()) ::
  command_table() | term()

Removes a single command from a namespace. Returns the updated table, or the input unchanged if table is not a map.

unregister_commands_by_module(table, module)

@spec unregister_commands_by_module(command_table() | term(), module()) ::
  command_table() | term()

Removes all commands for a module from the table.

unregister_plugin_commands(plugin_module, command_table)

@spec unregister_plugin_commands(module(), command_table()) ::
  {:ok, command_table()} | :ok

Unregisters all commands for a plugin module.