diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 56bae1f5..9bf7b83a 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -82,6 +82,7 @@ nav: - reference/cli/history.md - reference/cli/machines.md - reference/cli/secrets.md + - reference/cli/show.md - reference/cli/ssh.md - reference/cli/vms.md - Clan Core: diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 9e2ee5ea..6acb18f6 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -8,6 +8,7 @@ from typing import Any # These imports are unused, but necessary for @API.register to run once. from clan_cli.api import directory +from clan_cli.flakes import show __all__ = ["directory"] @@ -112,8 +113,17 @@ For more detailed information, visit: https://docs.clan.lol ), formatter_class=argparse.RawTextHelpFormatter, ) + subparsers = parser.add_subparsers() + # Commands directly under the root i.e. "clan show" + show_parser = subparsers.add_parser( + "show", + help="Show the clan meta if present.", + description="Final meta results from clan/meta.json and flake arguments.", + ) + show_parser.set_defaults(func=show.show_command) + parser_backups = subparsers.add_parser( "backups", help="manage backups of clan machines", diff --git a/pkgs/clan-cli/clan_cli/flakes/show.py b/pkgs/clan-cli/clan_cli/flakes/show.py new file mode 100644 index 00000000..3bbf7bd2 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/flakes/show.py @@ -0,0 +1,64 @@ +import argparse +import dataclasses +import json +import logging +from pathlib import Path + +from clan_cli.api import API +from clan_cli.errors import ClanCmdError, ClanError + +from ..cmd import run_no_stdout +from ..nix import nix_eval + +log = logging.getLogger(__name__) + + +@dataclasses.dataclass +class ClanMeta: + name: str + description: str | None + icon: str | None + + +@API.register +def show_clan(uri: str | Path) -> ClanMeta: + cmd = nix_eval( + [ + f"{uri}#clanInternals.meta", + "--json", + ] + ) + res = "{}" + try: + proc = run_no_stdout(cmd) + res = proc.stdout.strip() + except ClanCmdError: + raise ClanError( + "Clan might not have meta attributes", + location=f"show_clan {uri}", + description="Evaluation failed on clanInternals.meta attribute", + ) + + clan_meta = json.loads(res) + return ClanMeta( + name=clan_meta.get("name"), + description=clan_meta.get("description", None), + icon=clan_meta.get("icon", None), + ) + + +def show_command(args: argparse.Namespace) -> None: + flake_path = Path(args.flake).resolve() + meta = show_clan(flake_path) + + print(f"Name: {meta.name}") + print(f"Description: {meta.description or ''}") + print(f"Icon: {meta.icon or ''}") + + +def register_parser(parser: argparse.ArgumentParser) -> None: + parser.set_defaults(func=show_command) + parser.add_argument( + "show", + help="Show", + ) diff --git a/pkgs/clan-cli/out.ts b/pkgs/clan-cli/out.ts deleted file mode 100644 index 3eafc603..00000000 --- a/pkgs/clan-cli/out.ts +++ /dev/null @@ -1,991 +0,0 @@ -export const schema = { - "$comment": "An object containing API methods. ", - "type": "object", - "additionalProperties": false, - "required": [ - "open_file", - "get_directory", - "create_machine", - "list_machines", - "show_machine", - "create_clan" - ], - "properties": { - "open_file": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "status" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "file_request" - ], - "additionalProperties": false, - "properties": { - "file_request": { - "type": "object", - "properties": { - "mode": { - "type": "string", - "enum": [ - "open_file", - "select_folder" - ] - }, - "title": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "filters": { - "oneOf": [ - { - "type": "object", - "properties": { - "title": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "mime_types": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - }, - "patterns": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - }, - "suffixes": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [], - "additionalProperties": false - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "mode" - ], - "additionalProperties": false - } - } - } - } - }, - "get_directory": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "type": "object", - "properties": { - "path": { - "type": "string" - }, - "files": { - "type": "array", - "items": { - "type": "object", - "properties": { - "path": { - "type": "string" - }, - "file_type": { - "type": "string", - "enum": [ - "file", - "directory", - "symlink" - ] - } - }, - "required": [ - "path", - "file_type" - ], - "additionalProperties": false - } - } - }, - "required": [ - "path", - "files" - ], - "additionalProperties": false - } - }, - "required": [ - "status", - "data" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "current_path" - ], - "additionalProperties": false, - "properties": { - "current_path": { - "type": "string" - } - } - } - } - }, - "create_machine": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "type": "null" - } - }, - "required": [ - "status" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "flake_dir", - "machine" - ], - "additionalProperties": false, - "properties": { - "flake_dir": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "string" - } - ] - }, - "machine": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "config": { - "type": "object", - "additionalProperties": { - "type": "integer" - } - } - }, - "required": [ - "name", - "config" - ], - "additionalProperties": false - } - } - } - } - }, - "list_machines": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "type": "array", - "items": { - "type": "string" - } - } - }, - "required": [ - "status", - "data" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "flake_url", - "debug" - ], - "additionalProperties": false, - "properties": { - "flake_url": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "string" - } - ] - }, - "debug": { - "type": "boolean" - } - } - } - } - }, - "show_machine": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "type": "object", - "properties": { - "machine_name": { - "type": "string" - }, - "machine_description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "machine_icon": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "machine_name" - ], - "additionalProperties": false - } - }, - "required": [ - "status", - "data" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "flake_url", - "machine_name", - "debug" - ], - "additionalProperties": false, - "properties": { - "flake_url": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "string" - } - ] - }, - "machine_name": { - "type": "string" - }, - "debug": { - "type": "boolean" - } - } - } - } - }, - "create_clan": { - "type": "object", - "required": [ - "arguments", - "return" - ], - "additionalProperties": false, - "properties": { - "return": { - "oneOf": [ - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "success" - ], - "description": "The status of the response." - }, - "data": { - "type": "object", - "properties": { - "git_init": { - "type": "object", - "properties": { - "stdout": { - "type": "string" - }, - "stderr": { - "type": "string" - }, - "cwd": { - "type": "string" - }, - "command": { - "type": "string" - }, - "returncode": { - "type": "integer" - }, - "msg": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "stdout", - "stderr", - "cwd", - "command", - "returncode" - ], - "additionalProperties": false - }, - "git_add": { - "type": "object", - "properties": { - "stdout": { - "type": "string" - }, - "stderr": { - "type": "string" - }, - "cwd": { - "type": "string" - }, - "command": { - "type": "string" - }, - "returncode": { - "type": "integer" - }, - "msg": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "stdout", - "stderr", - "cwd", - "command", - "returncode" - ], - "additionalProperties": false - }, - "git_config": { - "type": "object", - "properties": { - "stdout": { - "type": "string" - }, - "stderr": { - "type": "string" - }, - "cwd": { - "type": "string" - }, - "command": { - "type": "string" - }, - "returncode": { - "type": "integer" - }, - "msg": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "stdout", - "stderr", - "cwd", - "command", - "returncode" - ], - "additionalProperties": false - }, - "flake_update": { - "type": "object", - "properties": { - "stdout": { - "type": "string" - }, - "stderr": { - "type": "string" - }, - "cwd": { - "type": "string" - }, - "command": { - "type": "string" - }, - "returncode": { - "type": "integer" - }, - "msg": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "stdout", - "stderr", - "cwd", - "command", - "returncode" - ], - "additionalProperties": false - } - }, - "required": [ - "git_init", - "git_add", - "git_config", - "flake_update" - ], - "additionalProperties": false - } - }, - "required": [ - "status", - "data" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "status": { - "type": "string", - "enum": [ - "error" - ] - }, - "errors": { - "type": "array", - "items": { - "type": "object", - "properties": { - "message": { - "type": "string" - }, - "description": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "location": { - "oneOf": [ - { - "type": "array", - "items": { - "type": "string" - } - }, - { - "type": "null" - } - ] - } - }, - "required": [ - "message" - ], - "additionalProperties": false - } - } - }, - "required": [ - "status", - "errors" - ], - "additionalProperties": false - } - ] - }, - "arguments": { - "type": "object", - "required": [ - "directory", - "template_url" - ], - "additionalProperties": false, - "properties": { - "directory": { - "type": "string" - }, - "template_url": { - "type": "string" - } - } - } - } - } - } -} as const; -