pkgs/clan/lib(directory): Add API function to query the configured directory

Similar to the implementation in #4526
Co-authored-by: Mayeu <m@mayeu.me>
This commit is contained in:
a-kenji
2025-08-09 15:38:41 +02:00
parent 40682972ef
commit e5bea3d49a
2 changed files with 45 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Literal
from clan_lib.cmd import RunOpts, run
@@ -129,3 +130,15 @@ def get_clan_directory_relative(flake: Flake) -> str:
_, relative_dir = get_clan_directories(flake)
return relative_dir
def get_clan_dir(flake: Flake) -> Path:
"""
Get the effective clan directory, respecting the clan.directory configuration.
Args:
flake: The clan flake
Returns:
Path to the effective clan directory
"""
relative_clan_dir = get_clan_directory_relative(flake)
return flake.path / relative_clan_dir if relative_clan_dir else flake.path

View File

@@ -4,7 +4,7 @@ from pathlib import Path
import pytest
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_lib.api.directory import get_clan_directory_relative
from clan_lib.api.directory import get_clan_dir, get_clan_directory_relative
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
@@ -46,3 +46,34 @@ def test_get_relative_clan_directory_invalid_flake() -> None:
with pytest.raises(ClanError):
get_clan_directory_relative(invalid_flake)
@pytest.mark.with_core
def test_get_clan_dir_default(
test_flake_with_core: FlakeForTest,
) -> None:
flake = Flake(str(test_flake_with_core.path))
clan_dir = get_clan_dir(flake)
# Default configuration should return the full path
assert clan_dir == flake.path
@pytest.mark.with_core
def test_get_clan_dir_custom(
clan_flake: Callable[..., Flake],
) -> None:
flake = clan_flake(
raw="""
{
directory = ./direct-config;
}
"""
)
test_subdir = Path(flake.path) / "direct-config"
test_subdir.mkdir(exist_ok=True)
relative_dir = get_clan_dir(flake)
assert relative_dir == test_subdir