clan-cli: Fix clan flakes create inside an already existing git repo

This commit is contained in:
Qubasa
2025-02-17 13:23:31 +07:00
parent 892bd25a3a
commit 52fd912b45
2 changed files with 56 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ from clan_cli.cmd import CmdOut, RunOpts, run
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
from clan_cli.flake import Flake from clan_cli.flake import Flake
from clan_cli.inventory import Inventory, init_inventory from clan_cli.inventory import Inventory, init_inventory
from clan_cli.nix import nix_shell from clan_cli.nix import nix_metadata, nix_shell
from clan_cli.templates import ( from clan_cli.templates import (
InputPrio, InputPrio,
TemplateName, TemplateName,
@@ -48,6 +48,13 @@ def git_command(directory: Path, *args: str) -> list[str]:
def create_clan(opts: CreateOptions) -> CreateClanResponse: def create_clan(opts: CreateOptions) -> CreateClanResponse:
dest = opts.dest.resolve() dest = opts.dest.resolve()
try:
nix_metadata(str(opts.src_flake))
except ClanError:
log.error(f"Found a repository, but it is not a valid flake: {opts.src_flake}")
log.warning("Setting src_flake to None")
opts.src_flake = None
template = get_template( template = get_template(
TemplateName(opts.template_name), TemplateName(opts.template_name),
"clan", "clan",

View File

@@ -3,6 +3,7 @@ import subprocess
from pathlib import Path from pathlib import Path
import pytest import pytest
from clan_cli.cmd import run
from fixtures_flakes import substitute from fixtures_flakes import substitute
from helpers import cli from helpers import cli
from stdout import CaptureOutput from stdout import CaptureOutput
@@ -53,6 +54,53 @@ def test_create_flake(
pytest.fail("nixosConfigurations.machine1 not found in flake outputs") pytest.fail("nixosConfigurations.machine1 not found in flake outputs")
@pytest.mark.impure
def test_create_flake_existing_git(
monkeypatch: pytest.MonkeyPatch,
temporary_home: Path,
clan_core: Path,
capture_output: CaptureOutput,
) -> None:
flake_dir = temporary_home / "test-flake"
run(["git", "init", str(temporary_home)])
cli.run(["flakes", "create", str(flake_dir), "--template=default"])
assert (flake_dir / ".clan-flake").exists()
# Replace the inputs.clan.url in the template flake.nix
substitute(
flake_dir / "flake.nix",
clan_core,
)
# Dont evaluate the inventory before the substitute call
monkeypatch.chdir(flake_dir)
cli.run(["machines", "create", "machine1"])
# create a hardware-configuration.nix that doesn't throw an eval error
for patch_machine in ["jon", "sara"]:
(
flake_dir / "machines" / f"{patch_machine}/hardware-configuration.nix"
).write_text("{}")
with capture_output as output:
cli.run(["machines", "list"])
assert "machine1" in output.out
flake_show = subprocess.run(
["nix", "flake", "show", "--json"],
check=True,
capture_output=True,
text=True,
)
flake_outputs = json.loads(flake_show.stdout)
try:
flake_outputs["nixosConfigurations"]["machine1"]
except KeyError:
pytest.fail("nixosConfigurations.machine1 not found in flake outputs")
@pytest.mark.impure @pytest.mark.impure
def test_ui_template( def test_ui_template(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,