base_module.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. """Base module class for template management."""
  2. from __future__ import annotations
  3. import logging
  4. from abc import ABC
  5. from typing import Annotated
  6. from typer import Argument, Option, Typer
  7. from ..display import DisplayManager
  8. from ..library import LibraryManager
  9. from ..template import Template
  10. from .base_commands import (
  11. GenerationConfig,
  12. generate_template,
  13. list_templates,
  14. search_templates,
  15. show_template,
  16. validate_templates,
  17. )
  18. from .config_commands import (
  19. config_clear,
  20. config_get,
  21. config_list,
  22. config_remove,
  23. config_set,
  24. )
  25. logger = logging.getLogger(__name__)
  26. # Expected length of library entry tuple: (path, library_name, needs_qualification)
  27. LIBRARY_ENTRY_MIN_LENGTH = 2
  28. class Module(ABC):
  29. """Streamlined base module that auto-detects variables from templates.
  30. Subclasses must define:
  31. - name: str (class attribute)
  32. - description: str (class attribute)
  33. """
  34. # Class attributes that must be defined by subclasses
  35. name: str
  36. description: str
  37. # Schema version supported by this module (override in subclasses)
  38. schema_version: str = "1.0"
  39. def __init__(self) -> None:
  40. # Validate required class attributes
  41. if not hasattr(self.__class__, "name") or not hasattr(self.__class__, "description"):
  42. raise TypeError(f"Module {self.__class__.__name__} must define 'name' and 'description' class attributes")
  43. logger.info(f"Initializing module '{self.name}'")
  44. logger.debug(f"Module '{self.name}' configuration: description='{self.description}'")
  45. self.libraries = LibraryManager()
  46. self.display = DisplayManager()
  47. def _load_all_templates(self, filter_fn=None) -> list:
  48. """Load all templates for this module with optional filtering."""
  49. templates = []
  50. entries = self.libraries.find(self.name, sort_results=True)
  51. for entry in entries:
  52. # Unpack entry - returns (path, library_name, needs_qualification)
  53. template_dir = entry[0]
  54. library_name = entry[1]
  55. needs_qualification = entry[2] if len(entry) > LIBRARY_ENTRY_MIN_LENGTH else False
  56. try:
  57. # Get library object to determine type
  58. library = next(
  59. (lib for lib in self.libraries.libraries if lib.name == library_name),
  60. None,
  61. )
  62. library_type = library.library_type if library else "git"
  63. template = Template(template_dir, library_name=library_name, library_type=library_type)
  64. # If template ID needs qualification, set qualified ID
  65. if needs_qualification:
  66. template.set_qualified_id()
  67. # Apply filter if provided
  68. if filter_fn is None or filter_fn(template):
  69. templates.append(template)
  70. except Exception as exc:
  71. logger.error(f"Failed to load template from {template_dir}: {exc}")
  72. continue
  73. return templates
  74. def _load_template_by_id(self, id: str):
  75. """Load a template by its ID, supporting qualified IDs."""
  76. logger.debug(f"Loading template with ID '{id}' from module '{self.name}'")
  77. # find_by_id now handles both simple and qualified IDs
  78. result = self.libraries.find_by_id(self.name, id)
  79. if not result:
  80. raise FileNotFoundError(f"Template '{id}' not found in module '{self.name}'")
  81. template_dir, library_name = result
  82. # Get library type
  83. library = next((lib for lib in self.libraries.libraries if lib.name == library_name), None)
  84. library_type = library.library_type if library else "git"
  85. try:
  86. template = Template(template_dir, library_name=library_name, library_type=library_type)
  87. # If the original ID was qualified, preserve it
  88. if "." in id:
  89. template.id = id
  90. return template
  91. except Exception as exc:
  92. logger.error(f"Failed to load template '{id}': {exc}")
  93. raise FileNotFoundError(f"Template '{id}' could not be loaded: {exc}") from exc
  94. def list(
  95. self,
  96. raw: Annotated[bool, Option("--raw", help="Output raw list format instead of rich table")] = False,
  97. ) -> list:
  98. """List all templates."""
  99. return list_templates(self, raw)
  100. def search(
  101. self,
  102. query: Annotated[str, Argument(help="Search string to filter templates by ID")],
  103. ) -> list:
  104. """Search for templates by ID containing the search string."""
  105. return search_templates(self, query)
  106. def show(
  107. self,
  108. id: str,
  109. var: Annotated[
  110. list[str] | None,
  111. Option(
  112. "--var",
  113. "-v",
  114. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  115. ),
  116. ] = None,
  117. var_file: Annotated[
  118. str | None,
  119. Option(
  120. "--var-file",
  121. "-f",
  122. help="Load variables from YAML file (overrides config defaults)",
  123. ),
  124. ] = None,
  125. ) -> None:
  126. """Show template details with optional variable overrides."""
  127. return show_template(self, id, var, var_file)
  128. def generate(
  129. self,
  130. id: Annotated[str, Argument(help="Template ID")],
  131. directory: Annotated[
  132. str | None, Argument(help="[DEPRECATED: use --output] Output directory (defaults to template ID)")
  133. ] = None,
  134. *,
  135. output: Annotated[
  136. str | None,
  137. Option(
  138. "--output",
  139. "-o",
  140. help="Output directory (defaults to template ID)",
  141. ),
  142. ] = None,
  143. interactive: Annotated[
  144. bool,
  145. Option(
  146. "--interactive/--no-interactive",
  147. "-i/-n",
  148. help="Enable interactive prompting for variables",
  149. ),
  150. ] = True,
  151. var: Annotated[
  152. list[str] | None,
  153. Option(
  154. "--var",
  155. "-v",
  156. help="Variable override (repeatable). Supports: KEY=VALUE or KEY VALUE",
  157. ),
  158. ] = None,
  159. var_file: Annotated[
  160. str | None,
  161. Option(
  162. "--var-file",
  163. "-f",
  164. help="Load variables from YAML file (overrides config defaults, overridden by --var)",
  165. ),
  166. ] = None,
  167. dry_run: Annotated[
  168. bool,
  169. Option("--dry-run", help="Preview template generation without writing files"),
  170. ] = False,
  171. show_files: Annotated[
  172. bool,
  173. Option(
  174. "--show-files",
  175. help="Display generated file contents in plain text (use with --dry-run)",
  176. ),
  177. ] = False,
  178. quiet: Annotated[bool, Option("--quiet", "-q", help="Suppress all non-error output")] = False,
  179. ) -> None:
  180. """Generate from template.
  181. Variable precedence chain (lowest to highest):
  182. 1. Module spec (defined in cli/modules/*.py)
  183. 2. Template spec (from template.yaml)
  184. 3. Config defaults (from ~/.config/boilerplates/config.yaml)
  185. 4. Variable file (from --var-file)
  186. 5. CLI overrides (--var flags)
  187. """
  188. config = GenerationConfig(
  189. id=id,
  190. directory=directory,
  191. output=output,
  192. interactive=interactive,
  193. var=var,
  194. var_file=var_file,
  195. dry_run=dry_run,
  196. show_files=show_files,
  197. quiet=quiet,
  198. )
  199. return generate_template(self, config)
  200. def validate(
  201. self,
  202. template_id: Annotated[
  203. str | None,
  204. Argument(help="Template ID to validate (omit to validate all templates)"),
  205. ] = None,
  206. *,
  207. path: Annotated[
  208. str | None,
  209. Option("--path", help="Path to template directory for validation"),
  210. ] = None,
  211. verbose: Annotated[bool, Option("--verbose", "-v", help="Show detailed validation information")] = False,
  212. semantic: Annotated[
  213. bool,
  214. Option(
  215. "--semantic/--no-semantic",
  216. help="Enable semantic validation (Docker Compose schema, etc.)",
  217. ),
  218. ] = True,
  219. ) -> None:
  220. """Validate templates for Jinja2 syntax, undefined variables, and semantic correctness.
  221. Examples:
  222. # Validate specific template
  223. cli compose validate netbox
  224. # Validate all templates
  225. cli compose validate
  226. # Validate with verbose output
  227. cli compose validate netbox --verbose
  228. """
  229. return validate_templates(self, template_id, path, verbose, semantic)
  230. def config_get(
  231. self,
  232. var_name: str | None = None,
  233. ) -> None:
  234. """Get default value(s) for this module."""
  235. return config_get(self, var_name)
  236. def config_set(
  237. self,
  238. var_name: str,
  239. value: str | None = None,
  240. ) -> None:
  241. """Set a default value for a variable."""
  242. return config_set(self, var_name, value)
  243. def config_remove(
  244. self,
  245. var_name: Annotated[str, Argument(help="Variable name to remove")],
  246. ) -> None:
  247. """Remove a specific default variable value."""
  248. return config_remove(self, var_name)
  249. def config_clear(
  250. self,
  251. var_name: str | None = None,
  252. force: bool = False,
  253. ) -> None:
  254. """Clear default value(s) for this module."""
  255. return config_clear(self, var_name, force)
  256. def config_list(self) -> None:
  257. """Display the defaults for this specific module in YAML format."""
  258. return config_list(self)
  259. @classmethod
  260. def register_cli(cls, app: Typer) -> None:
  261. """Register module commands with the main app."""
  262. logger.debug(f"Registering CLI commands for module '{cls.name}'")
  263. module_instance = cls()
  264. module_app = Typer(help=cls.description)
  265. module_app.command("list")(module_instance.list)
  266. module_app.command("search")(module_instance.search)
  267. module_app.command("show")(module_instance.show)
  268. module_app.command("validate")(module_instance.validate)
  269. module_app.command(
  270. "generate",
  271. context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
  272. )(module_instance.generate)
  273. # Add defaults commands (simplified - only manage default values)
  274. defaults_app = Typer(help="Manage default values for template variables")
  275. defaults_app.command("get", help="Get default value(s)")(module_instance.config_get)
  276. defaults_app.command("set", help="Set a default value")(module_instance.config_set)
  277. defaults_app.command("rm", help="Remove a specific default value")(module_instance.config_remove)
  278. defaults_app.command("clear", help="Clear default value(s)")(module_instance.config_clear)
  279. defaults_app.command("list", help="Display the config for this module in YAML format")(
  280. module_instance.config_list
  281. )
  282. module_app.add_typer(defaults_app, name="defaults")
  283. app.add_typer(
  284. module_app,
  285. name=cls.name,
  286. help=cls.description,
  287. rich_help_panel="Template Commands",
  288. )
  289. logger.info(f"Module '{cls.name}' CLI commands registered")