udpated show output

This commit is contained in:
xcad
2025-09-04 16:03:17 +02:00
parent 4dbadc711a
commit 489189c025
2 changed files with 90 additions and 11 deletions
+36 -2
View File
@@ -13,7 +13,21 @@ class Library:
self.path = path
def find_by_id(self, module_name: str, files: list[str], template_id: str) -> "Template | None":
"""Find a template by its ID in this library."""
"""
Find a template by its ID in this library.
Args:
module_name: The module name (e.g., 'terraform', 'compose') to search within.
This narrows the search to the specific technology directory in the library.
files: List of file patterns to search for (e.g., ['*.tf', '*.yaml']).
This filters templates to only those with matching file extensions,
ensuring we only process relevant template files for the module.
template_id: The unique identifier of the template to find.
This is typically derived from the template's directory name or filename.
Returns:
Template object if found, None otherwise.
"""
for template in self.find(module_name, files, sorted=False):
if template.id == template_id:
return template
@@ -71,7 +85,27 @@ class LibraryManager:
return all_templates
def find_by_id(self, module_name: str, files: list[str], template_id: str) -> "Template | None":
"""Find a template by its ID across all libraries."""
"""
Find a template by its ID across all libraries.
Args:
module_name: The module name (e.g., 'terraform', 'compose') to search within.
This narrows the search to the specific technology directory across all libraries,
allowing for modular organization of templates by technology type.
files: List of file patterns to search for (e.g., ['*.tf', '*.yaml']).
This filters templates to only those with matching file extensions,
ensuring we only process relevant template files for the specific module type.
template_id: The unique identifier of the template to find.
This is typically derived from the template's directory name or filename,
providing a human-readable way to reference specific templates.
Returns:
Template object if found across any library, None otherwise.
Note:
This method searches through all registered libraries in order, returning the first
matching template found. This allows for library precedence and template overriding.
"""
for library in self.libraries:
template = library.find_by_id(module_name, files, template_id)
if template:
+54 -9
View File
@@ -3,6 +3,13 @@ from pathlib import Path
from typing import Any, Dict, Optional, Tuple, List
import logging
from typer import Typer, Option, Argument
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.syntax import Syntax
from rich.table import Table
from io import StringIO
from rich import box
from .library import LibraryManager
from .prompt import PromptHandler
@@ -73,17 +80,55 @@ class Module(ABC):
def show(self, id: str = Argument(..., metavar="template", help="The template to show details for")):
"""Show details about a template"""
logger.debug(f"Showing details for template: {id} in module: {self.name}")
template = self.libraries.find_by_id(module_name=self.name, files=self.files, template_id=id)
template = self.libraries.find_by_id(self.name, self.files, id)
if template:
logger.debug(f"Template found: {template.name}")
print(f"ID: {template.id}")
print(f"Name: {template.name}")
print(f"Directory: {template.directory}")
print(f"Content:\n{template.content}")
else:
if not template:
logger.error(f"Template with ID '{id}' not found")
print(f"Template with ID '{id}' not found.")
return
console = Console()
# Template title with name, id and version
title_text = Text()
title_text.append(f"{template.name} ", style="bold magenta")
title_text.append(f"({template.id}", style="bold magenta")
if template.version:
title_text.append(f" v{template.version}", style="bold magenta")
title_text.append(")", style="bold magenta")
# Description
description_text = Text(template.description, style="dim white")
# Print template info without panels
console.print(title_text)
console.print(description_text)
console.print()
# Print info fields
if template.author:
console.print(f"Author: [cyan]{template.author}[/cyan]")
if template.date:
console.print(f"Date: [cyan]{template.date}[/cyan]")
if template.tags:
console.print(f"Tags: [cyan]{', '.join(template.tags)}[/cyan]")
# Determine which variable groups are used by this template
template_var_groups = []
for var_group in self.variable_manager.variable_groups:
# Check if any variables from this group are used in the template
group_vars = [var.name for var in var_group.vars]
if any(var_name in template.vars for var_name in group_vars):
template_var_groups.append(var_group.name)
if template_var_groups:
console.print(f"Functions: [cyan]{', '.join(template_var_groups)}[/cyan]")
# Template content
if template.content:
console.print(f"\n{template.content}")
def generate(self, id: str = Argument(..., metavar="template", help="The template to generate from"), out: Optional[Path] = Option(None, "--out", "-o", help="Output file to save the generated template")):
"""Generate a new template with complex variable prompting logic"""
@@ -91,7 +136,7 @@ class Module(ABC):
# Step 1: Find template by ID
logger.debug(f"Step 1: Finding template by ID: {id}")
template = self.libraries.find_by_id(self.name, self.files, id)
template = self.libraries.find_by_id(module_name=self.name, files=self.files, template_id=id)
if not template:
logger.error(f"Template '{id}' not found")
print(f"Template '{id}' not found.")