|  | 
| 1 | 1 | from __future__ import annotations | 
| 2 | 2 | 
 | 
|  | 3 | +import asyncio | 
| 3 | 4 | from pathlib import Path | 
| 4 | 5 | from typing import Optional | 
| 5 | 6 | 
 | 
| 6 | 7 | import typer | 
| 7 | 8 | import yaml | 
|  | 9 | +from copier import run_copy | 
| 8 | 10 | from pydantic import ValidationError | 
| 9 | 11 | from rich.console import Console | 
| 10 | 12 | from rich.table import Table | 
| @@ -165,3 +167,52 @@ async def list( | 
| 165 | 167 |         ) | 
| 166 | 168 | 
 | 
| 167 | 169 |     console.print(table) | 
|  | 170 | + | 
|  | 171 | + | 
|  | 172 | +@app.command() | 
|  | 173 | +async def init( | 
|  | 174 | +    directory: Path = typer.Argument(help="Directory path for the new project."), | 
|  | 175 | +    template: str = typer.Option( | 
|  | 176 | +        default="https://github.com/opsmill/infrahub-template.git", | 
|  | 177 | +        help="Template to use for the new repository. If not provided, the default template will be used.", | 
|  | 178 | +    ), | 
|  | 179 | +    data: Optional[Path] = typer.Option(default=None, help="Path to YAML file containing answers to CLI prompt."), | 
|  | 180 | +    vcs_ref: Optional[str] = typer.Option( | 
|  | 181 | +        default="HEAD", | 
|  | 182 | +        help="VCS reference to use for the template. Defaults to HEAD.", | 
|  | 183 | +    ), | 
|  | 184 | +    trust: Optional[bool] = typer.Option( | 
|  | 185 | +        default=False, | 
|  | 186 | +        help="Trust the template repository. If set, the template will be cloned without verification.", | 
|  | 187 | +    ), | 
|  | 188 | +    _: str = CONFIG_PARAM, | 
|  | 189 | +) -> None: | 
|  | 190 | +    """Initialize a new Infrahub repository.""" | 
|  | 191 | + | 
|  | 192 | +    config_data = None | 
|  | 193 | +    if data: | 
|  | 194 | +        try: | 
|  | 195 | +            with Path.open(data, encoding="utf-8") as file: | 
|  | 196 | +                config_data = yaml.safe_load(file) | 
|  | 197 | +            typer.echo(f"Loaded config: {config_data}") | 
|  | 198 | +        except Exception as exc: | 
|  | 199 | +            typer.echo(f"Error loading YAML file: {exc}", err=True) | 
|  | 200 | +            raise typer.Exit(code=1) | 
|  | 201 | + | 
|  | 202 | +    # Allow template to be a local path or a URL | 
|  | 203 | +    template_source = template or "" | 
|  | 204 | +    if template and Path(template).exists(): | 
|  | 205 | +        template_source = str(Path(template).resolve()) | 
|  | 206 | + | 
|  | 207 | +    try: | 
|  | 208 | +        await asyncio.to_thread( | 
|  | 209 | +            run_copy, | 
|  | 210 | +            template_source, | 
|  | 211 | +            str(directory), | 
|  | 212 | +            data=config_data, | 
|  | 213 | +            vcs_ref=vcs_ref, | 
|  | 214 | +            unsafe=trust, | 
|  | 215 | +        ) | 
|  | 216 | +    except Exception as e: | 
|  | 217 | +        typer.echo(f"Error running copier: {e}", err=True) | 
|  | 218 | +        raise typer.Exit(code=1) | 
0 commit comments