Merge pull request 'pkgs/clan: Add flake validation to clan show' (#4352) from kenji/ke-non-clan-show into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4352
This commit is contained in:
kenji
2025-07-15 10:30:19 +00:00
4 changed files with 47 additions and 3 deletions

View File

@@ -2,13 +2,13 @@ import argparse
import logging
from clan_lib.clan.get import get_clan_details
from clan_lib.flake import Flake
from clan_lib.flake import require_flake
log = logging.getLogger(__name__)
def show_command(args: argparse.Namespace) -> None:
flake: Flake = args.flake
flake = require_flake(args.flake)
meta = get_clan_details(flake)
print(f"Name: {meta.get('name')}")

View File

@@ -1,4 +1,7 @@
from pathlib import Path
import pytest
from clan_lib.errors import ClanError
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_cli.tests.helpers import cli
@@ -14,3 +17,19 @@ def test_clan_show(
assert "Name:" in output.out
assert "Name: test_flake_with_core" in output.out
assert "Description:" in output.out
def test_clan_show_no_flake(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capture_output: CaptureOutput
) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError) as exc_info:
cli.run(["show"])
assert "No clan flake found in the current directory or its parents" in str(
exc_info.value
)
assert "Use the --flake flag to specify a clan flake path or URL" in str(
exc_info.value
)

View File

@@ -1 +1 @@
from .flake import Flake # noqa
from .flake import Flake, require_flake # noqa

View File

@@ -931,3 +931,28 @@ class Flake:
full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}'
return self.select(full_selector)
def require_flake(flake: Flake | None) -> Flake:
"""
Require that a flake argument is provided, if not in a clan flake.
This should be called by commands that require a flake but don't have
a sensible default when no clan flake is found locally.
Args:
flake: The flake object to check, may be None
Returns:
The validated flake object
Raises:
ClanError: If the flake is None
"""
if flake is None:
msg = "No clan flake found in the current directory or its parents"
raise ClanError(
msg,
description="Use the --flake flag to specify a clan flake path or URL",
)
return flake