Skip to content

Commit f3fe1bf

Browse files
committed
Add init command
1 parent c65502b commit f3fe1bf

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

infrahub_sdk/ctl/repository.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

3+
import asyncio
34
from pathlib import Path
45
from typing import Optional
56

67
import typer
78
import yaml
9+
from copier import run_copy
810
from pydantic import ValidationError
911
from rich.console import Console
1012
from rich.table import Table
@@ -165,3 +167,52 @@ async def list(
165167
)
166168

167169
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

Comments
 (0)