Added clan flakes inspect command. Improved ClanURI. Added democlan as dependency in flake.nix

This commit is contained in:
Qubasa
2023-12-09 00:04:56 +01:00
parent 9f4ab67fc2
commit d4b8cef242
14 changed files with 406 additions and 41 deletions

View File

@@ -3,6 +3,7 @@ import argparse
from clan_cli.flakes.add import register_add_parser
from clan_cli.flakes.history import register_list_parser
from clan_cli.flakes.inspect import register_inspect_parser
from .create import register_create_parser
@@ -21,3 +22,5 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
register_add_parser(add_parser)
list_parser = subparser.add_parser("list", help="List recently used flakes")
register_list_parser(list_parser)
inspect_parser = subparser.add_parser("inspect", help="Inspect a clan flake")
register_inspect_parser(inspect_parser)

View File

@@ -0,0 +1,83 @@
import argparse
import shlex
import subprocess
from dataclasses import dataclass
from pathlib import Path
from ..errors import ClanError
from ..nix import nix_config, nix_eval, nix_metadata
@dataclass
class FlakeConfig:
flake_url: str | Path
flake_attr: str
icon: str | None
description: str | None
last_updated: str
revision: str | None
def inspect_flake(flake_url: str | Path, flake_attr: str) -> FlakeConfig:
config = nix_config()
system = config["system"]
cmd = nix_eval(
[
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.clanCore.clanIcon'
]
)
proc = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
assert proc.stdout is not None
if proc.returncode != 0:
raise ClanError(
f"""
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
"""
)
res = proc.stdout.strip()
if res == "null":
icon_path = None
else:
icon_path = res
meta = nix_metadata(flake_url)
return FlakeConfig(
flake_url=flake_url,
flake_attr=flake_attr,
icon=icon_path,
description=meta.get("description"),
last_updated=meta["lastModified"],
revision=meta.get("revision"),
)
@dataclass
class InspectOptions:
machine: str
flake: Path
def inspect_command(args: argparse.Namespace) -> None:
inspect_options = InspectOptions(
machine=args.machine,
flake=args.flake or Path.cwd(),
)
res = inspect_flake(
flake_url=inspect_options.flake, flake_attr=inspect_options.machine
)
print("Icon:", res.icon)
print("Description:", res.description)
print("Last updated:", res.last_updated)
print("Revision:", res.revision)
def register_inspect_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--machine", type=str, default="defaultVM")
parser.set_defaults(func=inspect_command)