app.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. Main application factory and CLI entry point.
  3. Creates and configures the main Typer application with all modules.
  4. """
  5. import logging
  6. import sys
  7. from pathlib import Path
  8. from typing import Optional
  9. import typer
  10. from rich.console import Console
  11. from rich.traceback import install
  12. from cli import __version__
  13. from ..modules import get_all_modules
  14. from .logging import setup_logging
  15. def version_callback(value: bool):
  16. """Callback for version option."""
  17. if value:
  18. console = Console()
  19. console.print(f"Boilerplates CLI v{__version__}", style="bold blue")
  20. raise typer.Exit()
  21. def create_app() -> typer.Typer:
  22. """
  23. Create and configure the main CLI application.
  24. Returns:
  25. Configured Typer application with all modules registered.
  26. """
  27. # Install rich traceback handler for better error display
  28. install(show_locals=True)
  29. # Create main app
  30. app = typer.Typer(
  31. name="boilerplates",
  32. help="🚀 Sophisticated CLI tool for managing infrastructure boilerplates",
  33. epilog="Made with ❤️ by Christian Lempa",
  34. rich_markup_mode="rich",
  35. no_args_is_help=True,
  36. )
  37. @app.callback()
  38. def main(
  39. ctx: typer.Context,
  40. version: Optional[bool] = typer.Option(
  41. None,
  42. "--version",
  43. "-v",
  44. callback=version_callback,
  45. is_eager=True,
  46. help="Show version and exit"
  47. ),
  48. log_level: str = typer.Option(
  49. "WARNING",
  50. "--log-level",
  51. "-l",
  52. help="Set logging level",
  53. case_sensitive=False,
  54. ),
  55. verbose: bool = typer.Option(
  56. False,
  57. "--verbose",
  58. help="Enable verbose output"
  59. ),
  60. quiet: bool = typer.Option(
  61. False,
  62. "--quiet",
  63. "-q",
  64. help="Suppress output"
  65. ),
  66. ):
  67. """
  68. 🚀 Boilerplates CLI - Manage your infrastructure templates with ease!
  69. """
  70. # Configure logging
  71. setup_logging(log_level=log_level.upper())
  72. # Store context for subcommands
  73. ctx.ensure_object(dict)
  74. # Register all module commands
  75. modules = get_all_modules()
  76. for module in modules:
  77. try:
  78. module_app = module.get_app()
  79. app.add_typer(
  80. module_app,
  81. name=module.name,
  82. # Don't override help - let the module define it
  83. )
  84. logging.getLogger("boilerplates.app").info(f"Registered module: {module.name}")
  85. except Exception as e:
  86. logging.getLogger("boilerplates.app").error(f"Failed to register module {module.name}: {e}")
  87. return app