diff --git a/src/makim/cli/__init__.py b/src/makim/cli/__init__.py index 0ad8c7a..843944f 100644 --- a/src/makim/cli/__init__.py +++ b/src/makim/cli/__init__.py @@ -12,14 +12,11 @@ from makim import __version__ from makim.cli.auto_generator import ( create_dynamic_command, - create_dynamic_command_cron, suggest_command, ) from makim.cli.config import CLI_ROOT_FLAGS_VALUES_COUNT, extract_root_config from makim.cli.cron_handlers import ( - _handle_cron_list, - _handle_cron_start, - _handle_cron_stop, + _handle_cron_commands, ) from makim.core import Makim @@ -101,71 +98,6 @@ def _get_command_from_cli() -> str: return command -def _handle_cron_commands(makim_instance: Makim) -> typer.Typer: - """Create and handle cron-related commands. - - Returns - ------- - typer.Typer: The cron command group with all subcommands. - """ - typer_cron = typer.Typer( - help='Tasks Scheduler', - invoke_without_command=True, - ) - - if 'scheduler' in makim_instance.global_data: - for schedule_name, schedule_params in makim_instance.global_data.get( - 'scheduler', {} - ).items(): - create_dynamic_command_cron( - makim_instance, - typer_cron, - schedule_name, - schedule_params or {}, - ) - - @typer_cron.command(help='List all scheduled tasks') - def list() -> None: - """List tasks defined in .makim.yaml and their current status.""" - _handle_cron_list(makim_instance) - - @typer_cron.command(help='Start a scheduler by its name') - def start( - name: str = typer.Argument( - None, - help="""Name of the scheduler to start. - Use '--all' for all schedulers""", - ), - all: bool = typer.Option( - False, - '--all', - help='Start all available schedulers', - is_flag=True, - ), - ) -> None: - """Start (enable) a scheduled task.""" - _handle_cron_start(makim_instance, name, all) - - @typer_cron.command(help='Stop a scheduler by its name') - def stop( - name: str = typer.Argument( - None, - help="""Name of the scheduler to stop. - Use '--all' for all schedulers""", - ), - all: bool = typer.Option( - False, - '--all', - help='Stop all running schedulers', - is_flag=True, - ), - ) -> None: - """Stop (disable) scheduled task(s).""" - _handle_cron_stop(makim_instance, name, all) - - return typer_cron - - def run_app() -> None: """Run the Typer app.""" root_config = extract_root_config() @@ -192,23 +124,6 @@ def run_app() -> None: for task_name, task_data in group_data.get('tasks', {}).items(): tasks[f'{group_name}.{task_name}'] = task_data - # Add dynamically cron commands to Typer app - if 'scheduler' in makim.global_data: - typer_cron = typer.Typer( - help='Tasks Scheduler', - invoke_without_command=True, - ) - - for schedule_name, schedule_params in makim.global_data.get( - 'scheduler', {} - ).items(): - create_dynamic_command_cron( - makim, typer_cron, schedule_name, schedule_params or {} - ) - - # Add cron command - app.add_typer(typer_cron, name='cron', rich_help_panel='Extensions') - # Add dynamically commands to Typer app # Add cron commands if scheduler is configured typer_cron = _handle_cron_commands(makim) diff --git a/src/makim/cli/cron_handlers.py b/src/makim/cli/cron_handlers.py index 1ad3d3b..bf34d44 100644 --- a/src/makim/cli/cron_handlers.py +++ b/src/makim/cli/cron_handlers.py @@ -7,6 +7,7 @@ from rich.console import Console from rich.table import Table +from makim.cli.auto_generator import create_dynamic_command_cron from makim.core import Makim @@ -21,6 +22,71 @@ def _create_cron_table() -> Table: return table +def _handle_cron_commands(makim_instance: Makim) -> typer.Typer: + """Create and handle cron-related commands. + + Returns + ------- + typer.Typer: The cron command group with all subcommands. + """ + typer_cron = typer.Typer( + help='Tasks Scheduler', + invoke_without_command=True, + ) + + if 'scheduler' in makim_instance.global_data: + for schedule_name, schedule_params in makim_instance.global_data.get( + 'scheduler', {} + ).items(): + create_dynamic_command_cron( + makim_instance, + typer_cron, + schedule_name, + schedule_params or {}, + ) + + @typer_cron.command(help='List all scheduled tasks') + def list() -> None: + """List tasks defined in .makim.yaml and their current status.""" + _handle_cron_list(makim_instance) + + @typer_cron.command(help='Start a scheduler by its name') + def start( + name: str = typer.Argument( + None, + help="""Name of the scheduler to start. + Use '--all' for all schedulers""", + ), + all: bool = typer.Option( + False, + '--all', + help='Start all available schedulers', + is_flag=True, + ), + ) -> None: + """Start (enable) a scheduled task.""" + _handle_cron_start(makim_instance, name, all) + + @typer_cron.command(help='Stop a scheduler by its name') + def stop( + name: str = typer.Argument( + None, + help="""Name of the scheduler to stop. + Use '--all' for all schedulers""", + ), + all: bool = typer.Option( + False, + '--all', + help='Stop all running schedulers', + is_flag=True, + ), + ) -> None: + """Stop (disable) scheduled task(s).""" + _handle_cron_stop(makim_instance, name, all) + + return typer_cron + + def _handle_cron_list(makim_instance: Makim) -> None: """Handle the cron list command.""" scheduled_tasks = makim_instance.global_data.get('scheduler', {})