Skip to content

Commit 4717ffc

Browse files
authored
Infrahub repostory init (#467)
* Update dependencies Add init command Add testing changelog Add docs Add copier to all extras update lock file update lock file update lock file update lock file * update lock file * update lock file
1 parent 544b49a commit 4717ffc

File tree

6 files changed

+290
-18
lines changed

6 files changed

+290
-18
lines changed

changelog/466.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `infrahubctl repository init` command to allow the initialization of an Infrahub repository using [infrahub-template](https://github.com/opsmill/infrahub-template).

docs/docs/infrahubctl/infrahubctl-repository.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $ infrahubctl repository [OPTIONS] COMMAND [ARGS]...
1919
**Commands**:
2020

2121
* `add`: Add a new repository.
22+
* `init`: Initialize a new Infrahub repository.
2223
* `list`
2324

2425
## `infrahubctl repository add`
@@ -47,6 +48,29 @@ $ infrahubctl repository add [OPTIONS] NAME LOCATION
4748
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
4849
* `--help`: Show this message and exit.
4950

51+
## `infrahubctl repository init`
52+
53+
Initialize a new Infrahub repository.
54+
55+
**Usage**:
56+
57+
```console
58+
$ infrahubctl repository init [OPTIONS] DIRECTORY
59+
```
60+
61+
**Arguments**:
62+
63+
* `DIRECTORY`: Directory path for the new project. [required]
64+
65+
**Options**:
66+
67+
* `--template TEXT`: Template to use for the new repository. Can be a local path or a git repository URL. [default: https://github.com/opsmill/infrahub-template.git]
68+
* `--data PATH`: Path to YAML file containing answers to CLI prompt.
69+
* `--vcs-ref TEXT`: VCS reference to use for the template. Defaults to HEAD. [default: HEAD]
70+
* `--trust / --no-trust`: Trust the template repository. If set, the template will be cloned without verification. [default: no-trust]
71+
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
72+
* `--help`: Show this message and exit.
73+
5074
## `infrahubctl repository list`
5175

5276
**Usage**:

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. Can be a local path or a git repository URL.",
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)

poetry.lock

Lines changed: 138 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dulwich = "^0.21.4"
4545
whenever = ">=0.7.2,<0.8.0"
4646
netutils = "^1.0.0"
4747
click = { version = "8.1.*", optional = true }
48+
copier = { version = "^9.8.0", optional = true }
4849

4950
[tool.poetry.group.dev.dependencies]
5051
pytest = "*"
@@ -69,7 +70,7 @@ infrahub-testcontainers = { version = "^1.2.5", python = ">=3.10" }
6970
astroid = "~3.1"
7071

7172
[tool.poetry.extras]
72-
ctl = ["Jinja2", "numpy", "pyarrow", "pyyaml", "rich", "toml", "typer", "click"]
73+
ctl = ["Jinja2", "numpy", "pyarrow", "pyyaml", "rich", "toml", "typer", "click", "copier"]
7374
tests = ["Jinja2", "pytest", "pyyaml", "rich"]
7475
all = [
7576
"Jinja2",
@@ -81,6 +82,7 @@ all = [
8182
"toml",
8283
"typer",
8384
"click",
85+
"copier",
8486
]
8587

8688
[tool.poetry.scripts]

0 commit comments

Comments
 (0)