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/+470be9b4.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed: Removed copier as a dependency, this impacts the `infrahub repository init` command and contains new instructions for how to initialize a repository from the template.
11 changes: 1 addition & 10 deletions docs/docs/infrahubctl/infrahubctl-repository.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,11 @@ Initialize a new Infrahub repository.
**Usage**:

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

**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`
Expand Down
50 changes: 4 additions & 46 deletions infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations

import asyncio
from pathlib import Path

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 @@ -207,49 +205,9 @@ async def list(


@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: Path | None = typer.Option(default=None, help="Path to YAML file containing answers to CLI prompt."),
vcs_ref: str | None = typer.Option(
default="HEAD",
help="VCS reference to use for the template. Defaults to HEAD.",
),
trust: bool | None = typer.Option(
default=False,
help="Trust the template repository. If set, the template will be cloned without verification.",
),
_: str = CONFIG_PARAM,
) -> None:
async def init() -> 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)

# 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)
console.print("The copier tool is not included in the Infrahub SDK CLI due to license restrictions,")
console.print("please run the following command to create a new Infrahub repository project:\n")
console.print("uv tool run --from 'copier' copier copy https://github.com/opsmill/infrahub-template <project-name>")
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ ctl = [
"rich>=12,<14",
"typer>=0.12.5",
"click==8.1.*",
"copier>=9.8.0",
"ariadne-codegen==0.15.3",
]

Expand All @@ -63,7 +62,6 @@ all = [
"rich>=12,<14",
"typer>=0.12.5",
"click==8.1.*",
"copier>=9.8.0",
"ariadne-codegen==0.15.3",
]

Expand Down
73 changes: 3 additions & 70 deletions tests/unit/ctl/test_repository_app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""Integration tests for infrahubctl commands."""

import tempfile
from pathlib import Path
from unittest import mock

import pytest
import yaml
from typer.testing import CliRunner

from infrahub_sdk.client import InfrahubClient
Expand Down Expand Up @@ -327,70 +324,6 @@ def test_repo_list(self, mock_repositories_list) -> None:

def test_repo_init(self) -> None:
"""Test the repository init command."""
with (
tempfile.TemporaryDirectory() as temp_dst,
tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False, encoding="utf-8") as temp_yaml,
):
dst = Path(temp_dst)
yaml_path = Path(temp_yaml.name)
commit = "v0.0.1"

answers = {
"generators": True,
"menus": True,
"project_name": "test",
"queries": True,
"scripts": True,
"tests": True,
"transforms": True,
"package_mode": False,
"_commit": commit,
}

yaml.safe_dump(answers, temp_yaml)
temp_yaml.close()
runner.invoke(app, ["repository", "init", str(dst), "--data", str(yaml_path), "--vcs-ref", commit])
coppied_answers = yaml.safe_load((dst / ".copier-answers.yml").read_text())
coppied_answers.pop("_src_path")

assert coppied_answers == answers
assert (dst / "generators").is_dir()
assert (dst / "queries").is_dir()
assert (dst / "scripts").is_dir()
assert (dst / "pyproject.toml").is_file()

def test_repo_init_local_template(self) -> None:
"""Test the repository init command with a local template."""
with (
tempfile.TemporaryDirectory() as temp_src,
tempfile.TemporaryDirectory() as temp_dst,
tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False, encoding="utf-8") as temp_yaml,
):
src = Path(temp_src)
dst = Path(temp_dst)

# Create a simple copier template
(src / "copier.yml").write_text("project_name:\n type: str")
template_dir = src / "{{project_name}}"
template_dir.mkdir()
(template_dir / "file.txt.jinja").write_text("Hello {{ project_name }}")

# Create answers file
yaml_path = Path(temp_yaml.name)
answers = {"project_name": "local-test"}
yaml.safe_dump(answers, temp_yaml)
temp_yaml.close()

# Run the command
result = runner.invoke(
app, ["repository", "init", str(dst), "--template", str(src), "--data", str(yaml_path)]
)

assert result.exit_code == 0, result.stdout

# Check the output
project_dir = dst / "local-test"
assert project_dir.is_dir()
output_file = project_dir / "file.txt"
assert output_file.is_file()
assert output_file.read_text() == "Hello local-test"
output = runner.invoke(app, ["repository", "init"])
raw = strip_color(output.stdout)
assert "uv tool run --from 'copier' copier copy https://github.com/opsmill/infrahub-template" in raw
87 changes: 0 additions & 87 deletions uv.lock

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