Merge pull request 'clan_lib: Add get_service_readmes api function' (#5530) from Qubasa/clan-core:add_get_readme into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/5530
This commit is contained in:
Luis Hebendanz
2025-10-14 16:59:42 +00:00
2 changed files with 57 additions and 0 deletions

View File

@@ -210,6 +210,41 @@ def find_instance_refs_for_module(
return res
type ServiceName = str
type Readme = str | None
type ServiceReadmes = dict[ServiceName, Readme]
type InputName = str | None
@dataclass(frozen=True)
class ServiceReadmeCollection:
input_name: str | None
readmes: ServiceReadmes
@API.register
def get_service_readmes(
input_name: InputName,
service_names: list[ServiceName],
flake: Flake,
) -> ServiceReadmeCollection:
"""Get the README content for a service module"""
query_param = "modulesPerSource"
if input_name is None:
query_param = "staticModules"
service_queries = "{" + ",".join(service_names) + "}"
query = (
f"clanInternals.inventoryClass.{query_param}.{service_queries}.manifest.readme"
)
readmes = flake.select(query)
return ServiceReadmeCollection(input_name=input_name, readmes=readmes)
@API.register
def list_service_modules(flake: Flake) -> ClanModules:
"""Show information about a module"""

View File

@@ -6,6 +6,7 @@ from clan_cli.tests.fixtures_flakes import nested_dict
from clan_lib.errors import ClanError
from clan_lib.flake.flake import Flake
from clan_lib.services.modules import (
get_service_readmes,
list_service_instances,
list_service_modules,
set_service_instance,
@@ -70,6 +71,27 @@ def test_list_service_instances(
assert borgbackup_service.info.roles["server"].description is not None
@pytest.mark.with_core
def test_get_service_readmes(
clan_flake: Callable[..., Flake],
) -> None:
clan_config: Clan = {"inventory": {}}
flake = clan_flake(clan_config)
service_modules = list_service_modules(flake)
service_names = [m.usage_ref["name"] for m in service_modules.modules]
collection = get_service_readmes(
input_name=None,
service_names=service_names,
flake=flake,
)
assert collection.input_name is None
assert collection.readmes["borgbackup"]
assert len(collection.readmes["borgbackup"]) > 10
@pytest.mark.with_core
def test_list_service_modules(
clan_flake: Callable[..., Flake],