|
| 1 | +"""Command registry for managing helm-values commands.""" |
| 2 | + |
| 3 | +from typing import Dict, Type |
| 4 | + |
| 5 | +from helm_values_manager.commands.base_command import BaseCommand |
| 6 | + |
| 7 | + |
| 8 | +class CommandRegistry: |
| 9 | + """Registry for managing helm-values commands. |
| 10 | +
|
| 11 | + This class provides a central registry for all available commands in the |
| 12 | + helm-values plugin. It handles command registration and lookup. |
| 13 | + """ |
| 14 | + |
| 15 | + _instance = None |
| 16 | + _commands: Dict[str, Type[BaseCommand]] = {} |
| 17 | + |
| 18 | + def __new__(cls): |
| 19 | + """Create a new singleton instance of CommandRegistry.""" |
| 20 | + if cls._instance is None: |
| 21 | + cls._instance = super(CommandRegistry, cls).__new__(cls) |
| 22 | + return cls._instance |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def register(cls, name: str, command_class: Type[BaseCommand]) -> None: |
| 26 | + """Register a new command. |
| 27 | +
|
| 28 | + Args: |
| 29 | + name: The name of the command |
| 30 | + command_class: The command class to register |
| 31 | +
|
| 32 | + Raises: |
| 33 | + ValueError: If a command with the same name already exists |
| 34 | + """ |
| 35 | + if name in cls._commands: |
| 36 | + raise ValueError(f"Command {name} already registered") |
| 37 | + cls._commands[name] = command_class |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def get_command(cls, name: str) -> Type[BaseCommand]: |
| 41 | + """Get a command by name. |
| 42 | +
|
| 43 | + Args: |
| 44 | + name: The name of the command to get |
| 45 | +
|
| 46 | + Returns: |
| 47 | + The command class |
| 48 | +
|
| 49 | + Raises: |
| 50 | + KeyError: If the command is not found |
| 51 | + """ |
| 52 | + if name not in cls._commands: |
| 53 | + raise KeyError(f"Command {name} not found") |
| 54 | + return cls._commands[name] |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def list_commands(cls) -> Dict[str, Type[BaseCommand]]: |
| 58 | + """List all registered commands. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + A dictionary mapping command names to command classes |
| 62 | + """ |
| 63 | + return cls._commands.copy() |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def clear(cls) -> None: |
| 67 | + """Clear all registered commands. |
| 68 | +
|
| 69 | + This is primarily used for testing. |
| 70 | + """ |
| 71 | + cls._commands.clear() |
0 commit comments