__main__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. """
  3. Main entry point for the Boilerplates CLI application.
  4. This file serves as the primary executable when running the CLI.
  5. """
  6. import importlib
  7. import logging
  8. import pkgutil
  9. import sys
  10. from pathlib import Path
  11. from typer import Typer, Option, Context
  12. import cli.modules
  13. from cli.core.registry import registry
  14. app = Typer(no_args_is_help=True)
  15. # Set up logging
  16. logging.basicConfig(
  17. level=logging.CRITICAL,
  18. format='[%(levelname)s] %(message)s',
  19. stream=sys.stdout
  20. )
  21. logger = logging.getLogger('boilerplates')
  22. @app.callback()
  23. def main(
  24. ctx: Context,
  25. debug: bool = Option(False, "--debug", help="Enable debug logging")
  26. ):
  27. """Main CLI application for managing boilerplates."""
  28. # Enable debug logging if requested
  29. if debug:
  30. logging.getLogger('boilerplates').setLevel(logging.DEBUG)
  31. logger.debug("Debug logging enabled")
  32. logger.debug("Starting boilerplates CLI application")
  33. def init_app():
  34. """Initialize the application by discovering and registering modules."""
  35. try:
  36. # Auto-discover and import all modules
  37. modules_path = Path(cli.modules.__file__).parent
  38. logger.debug(f"Discovering modules in: {modules_path}")
  39. for finder, name, ispkg in pkgutil.iter_modules([str(modules_path)]):
  40. if not ispkg and not name.startswith('_') and name != 'base':
  41. try:
  42. logger.debug(f"Importing module: {name}")
  43. importlib.import_module(f"cli.modules.{name}")
  44. except ImportError as e:
  45. logger.warning(f"Could not import {name}: {e}")
  46. # Register modules with app
  47. logger.debug(f"Registering {len(registry.create_instances())} modules")
  48. for module in registry.create_instances():
  49. try:
  50. logger.debug(f"Registering module: {module.__class__.__name__}")
  51. module.register(app)
  52. except Exception as e:
  53. logger.error(f"Error registering {module.__class__.__name__}: {e}")
  54. except Exception as e:
  55. logger.error(f"Application initialization error: {e}")
  56. exit(1)
  57. def run():
  58. """Run the CLI application."""
  59. init_app()
  60. app()
  61. if __name__ == "__main__":
  62. run()