Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/466.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `infrahubctl repository init` command to allow the initialization of an Infrahub repository using [infrahub-template](https://github.com/opsmill/infrahub-template).
24 changes: 24 additions & 0 deletions docs/docs/infrahubctl/infrahubctl-repository.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $ infrahubctl repository [OPTIONS] COMMAND [ARGS]...
**Commands**:

* `add`: Add a new repository.
* `init`: Initialize a new Infrahub repository.
* `list`

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

## `infrahubctl repository init`

Initialize a new Infrahub repository.

**Usage**:

```console
$ infrahubctl repository init [OPTIONS] DIRECTORY
```

**Arguments**:

* `DIRECTORY`: Directory path for the new project. [required]

**Options**:

* `--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]
* `--data PATH`: Path to YAML file containing answers to CLI prompt.
* `--vcs-ref TEXT`: VCS reference to use for the template. Defaults to HEAD. [default: HEAD]
* `--trust / --no-trust`: Trust the template repository. If set, the template will be cloned without verification. [default: no-trust]
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
* `--help`: Show this message and exit.

## `infrahubctl repository list`

**Usage**:
Expand Down
51 changes: 51 additions & 0 deletions infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import asyncio
from pathlib import Path
from typing import Optional

import typer
import yaml
from copier import run_copy
from pydantic import ValidationError
from rich.console import Console
from rich.table import Table
Expand Down Expand Up @@ -165,3 +167,52 @@
)

console.print(table)


@app.command()
async def init(
directory: Path = typer.Argument(help="Directory path for the new project."),
template: str = typer.Option(
default="https://github.com/opsmill/infrahub-template.git",
help="Template to use for the new repository. Can be a local path or a git repository URL.",
),
data: Optional[Path] = typer.Option(default=None, help="Path to YAML file containing answers to CLI prompt."),
vcs_ref: Optional[str] = typer.Option(
default="HEAD",
help="VCS reference to use for the template. Defaults to HEAD.",
),
trust: Optional[bool] = typer.Option(
default=False,
help="Trust the template repository. If set, the template will be cloned without verification.",
),
_: str = CONFIG_PARAM,
) -> None:
"""Initialize a new Infrahub repository."""

config_data = None
if data:
try:
with Path.open(data, encoding="utf-8") as file:
config_data = yaml.safe_load(file)
typer.echo(f"Loaded config: {config_data}")
except Exception as exc:
typer.echo(f"Error loading YAML file: {exc}", err=True)
raise typer.Exit(code=1)

Check warning on line 200 in infrahub_sdk/ctl/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/repository.py#L198-L200

Added lines #L198 - L200 were not covered by tests

# Allow template to be a local path or a URL
template_source = template or ""
if template and Path(template).exists():
template_source = str(Path(template).resolve())

try:
await asyncio.to_thread(
run_copy,
template_source,
str(directory),
data=config_data,
vcs_ref=vcs_ref,
unsafe=trust,
)
except Exception as e:
typer.echo(f"Error running copier: {e}", err=True)
raise typer.Exit(code=1)

Check warning on line 218 in infrahub_sdk/ctl/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/repository.py#L216-L218

Added lines #L216 - L218 were not covered by tests
155 changes: 138 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dulwich = "^0.21.4"
whenever = ">=0.7.2,<0.8.0"
netutils = "^1.0.0"
click = { version = "8.1.*", optional = true }
copier = { version = "^9.8.0", optional = true }

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

[tool.poetry.extras]
ctl = ["Jinja2", "numpy", "pyarrow", "pyyaml", "rich", "toml", "typer", "click"]
ctl = ["Jinja2", "numpy", "pyarrow", "pyyaml", "rich", "toml", "typer", "click", "copier"]
tests = ["Jinja2", "pytest", "pyyaml", "rich"]
all = [
"Jinja2",
Expand All @@ -81,6 +82,7 @@ all = [
"toml",
"typer",
"click",
"copier",
]

[tool.poetry.scripts]
Expand Down
Loading