use pathlib
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
from clan_cli.inventory import (
|
||||
AdminConfig,
|
||||
ServiceAdmin,
|
||||
@@ -25,7 +27,7 @@ def get_admin_service(base_url: str) -> ServiceAdmin | None:
|
||||
@API.register
|
||||
def set_admin_service(
|
||||
base_url: str,
|
||||
allowed_keys: dict[str, str],
|
||||
allowed_keys: dict[str, Path],
|
||||
instance_name: str = "admin",
|
||||
extra_machines: list[str] | None = None,
|
||||
) -> None:
|
||||
@@ -43,10 +45,10 @@ def set_admin_service(
|
||||
|
||||
keys = {}
|
||||
for name, keyfile in allowed_keys.items():
|
||||
if not keyfile.startswith("/"):
|
||||
if not keyfile.is_absolute():
|
||||
msg = f"Keyfile '{keyfile}' must be an absolute path"
|
||||
raise ValueError(msg)
|
||||
with open(keyfile) as f:
|
||||
with keyfile.open() as f:
|
||||
pubkey = f.read()
|
||||
keys[name] = pubkey
|
||||
|
||||
|
||||
@@ -69,8 +69,8 @@ def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatte
|
||||
)
|
||||
|
||||
|
||||
def get_roles(module_path: str) -> None | list[str]:
|
||||
roles_dir = Path(module_path) / "roles"
|
||||
def get_roles(module_path: Path) -> None | list[str]:
|
||||
roles_dir = module_path / "roles"
|
||||
if not roles_dir.exists() or not roles_dir.is_dir():
|
||||
return None
|
||||
|
||||
@@ -117,14 +117,14 @@ def list_modules(base_path: str) -> dict[str, ModuleInfo]:
|
||||
"""
|
||||
modules = get_modules(base_path)
|
||||
return {
|
||||
module_name: get_module_info(module_name, module_path)
|
||||
module_name: get_module_info(module_name, Path(module_path))
|
||||
for module_name, module_path in modules.items()
|
||||
}
|
||||
|
||||
|
||||
def get_module_info(
|
||||
module_name: str,
|
||||
module_path: str,
|
||||
module_path: Path,
|
||||
) -> ModuleInfo:
|
||||
"""
|
||||
Retrieves information about a module
|
||||
@@ -136,7 +136,7 @@ def get_module_info(
|
||||
location=f"show_module_info {module_name}",
|
||||
description="Module does not exist",
|
||||
)
|
||||
module_readme = Path(module_path) / "README.md"
|
||||
module_readme = module_path / "README.md"
|
||||
if not module_readme.exists():
|
||||
msg = "Module not found"
|
||||
raise ClanError(
|
||||
@@ -144,7 +144,7 @@ def get_module_info(
|
||||
location=f"show_module_info {module_name}",
|
||||
description="Module does not exist or doesn't have any README.md file",
|
||||
)
|
||||
with open(module_readme) as f:
|
||||
with module_readme.open() as f:
|
||||
readme = f.read()
|
||||
frontmatter, readme_content = extract_frontmatter(
|
||||
readme, f"{module_path}/README.md"
|
||||
|
||||
@@ -114,7 +114,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"path", type=Path, help="Path to the clan directory", default=Path(".")
|
||||
"path", type=Path, help="Path to the clan directory", default=Path()
|
||||
)
|
||||
|
||||
def create_flake_command(args: argparse.Namespace) -> None:
|
||||
|
||||
@@ -172,7 +172,7 @@ def get_or_set_option(args: argparse.Namespace) -> None:
|
||||
args.flake, machine_name=args.machine, show_trace=args.show_trace
|
||||
)
|
||||
else:
|
||||
with open(args.options_file) as f:
|
||||
with args.options_file.open() as f:
|
||||
options = json.load(f)
|
||||
# compute settings json file location
|
||||
if args.settings_file is None:
|
||||
|
||||
@@ -21,7 +21,7 @@ def list_state_folders(machine: str, service: None | str = None) -> None:
|
||||
if (clan_dir_result := get_clan_flake_toplevel_or_env()) is not None:
|
||||
flake = clan_dir_result
|
||||
else:
|
||||
flake = Path(".")
|
||||
flake = Path()
|
||||
cmd = nix_eval(
|
||||
[
|
||||
f"{flake}#nixosConfigurations.{machine}.config.clanCore.state",
|
||||
|
||||
Reference in New Issue
Block a user