feat(api): define list machine options as data class
This commit is contained in:
@@ -2,7 +2,7 @@ import argparse
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from clan_lib.flake import require_flake
|
from clan_lib.flake import require_flake
|
||||||
from clan_lib.machines.actions import list_machines
|
from clan_lib.machines.actions import ListOptions, MachineFilter, list_machines
|
||||||
|
|
||||||
from clan_cli.completions import add_dynamic_completer, complete_tags
|
from clan_cli.completions import add_dynamic_completer, complete_tags
|
||||||
|
|
||||||
@@ -12,7 +12,9 @@ log = logging.getLogger(__name__)
|
|||||||
def list_command(args: argparse.Namespace) -> None:
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
flake = require_flake(args.flake)
|
flake = require_flake(args.flake)
|
||||||
|
|
||||||
for name in list_machines(flake, opts={"filter": {"tags": args.tags}}):
|
for name in list_machines(
|
||||||
|
flake, opts=ListOptions(filter=MachineFilter(tags=args.tags))
|
||||||
|
):
|
||||||
print(name)
|
print(name)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from clan_lib.async_run import AsyncContext, AsyncOpts, AsyncRuntime
|
|||||||
from clan_lib.errors import ClanError
|
from clan_lib.errors import ClanError
|
||||||
from clan_lib.flake import require_flake
|
from clan_lib.flake import require_flake
|
||||||
from clan_lib.flake.flake import Flake
|
from clan_lib.flake.flake import Flake
|
||||||
from clan_lib.machines.actions import list_machines
|
from clan_lib.machines.actions import ListOptions, MachineFilter, list_machines
|
||||||
from clan_lib.machines.list import instantiate_inventory_to_machines
|
from clan_lib.machines.list import instantiate_inventory_to_machines
|
||||||
from clan_lib.machines.machines import Machine
|
from clan_lib.machines.machines import Machine
|
||||||
from clan_lib.machines.suggestions import validate_machine_names
|
from clan_lib.machines.suggestions import validate_machine_names
|
||||||
@@ -49,7 +49,9 @@ def get_machines_for_update(
|
|||||||
filter_tags: list[str],
|
filter_tags: list[str],
|
||||||
) -> list[Machine]:
|
) -> list[Machine]:
|
||||||
all_machines = list_machines(flake)
|
all_machines = list_machines(flake)
|
||||||
machines_with_tags = list_machines(flake, {"filter": {"tags": filter_tags}})
|
machines_with_tags = list_machines(
|
||||||
|
flake, ListOptions(filter=MachineFilter(tags=filter_tags))
|
||||||
|
)
|
||||||
|
|
||||||
if filter_tags and not machines_with_tags:
|
if filter_tags and not machines_with_tags:
|
||||||
msg = f"No machines found with tags: {' AND '.join(filter_tags)}"
|
msg = f"No machines found with tags: {' AND '.join(filter_tags)}"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from dataclasses import dataclass, field
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
@@ -18,12 +19,14 @@ from clan_lib.persist.util import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class MachineFilter(TypedDict):
|
@dataclass
|
||||||
tags: list[str]
|
class MachineFilter:
|
||||||
|
tags: list[str] | None = None
|
||||||
|
|
||||||
|
|
||||||
class ListOptions(TypedDict):
|
@dataclass
|
||||||
filter: MachineFilter
|
class ListOptions:
|
||||||
|
filter: MachineFilter = field(default_factory=MachineFilter)
|
||||||
|
|
||||||
|
|
||||||
class MachineStatus(StrEnum):
|
class MachineStatus(StrEnum):
|
||||||
@@ -44,26 +47,18 @@ def list_machines(
|
|||||||
) -> dict[str, InventoryMachine]:
|
) -> dict[str, InventoryMachine]:
|
||||||
"""
|
"""
|
||||||
List machines of a clan
|
List machines of a clan
|
||||||
|
|
||||||
Usage Example:
|
|
||||||
|
|
||||||
machines = list_machines(flake, {"filter": {"tags": ["foo" "bar"]}})
|
|
||||||
|
|
||||||
lists only machines that include both "foo" AND "bar"
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
inventory_store = InventoryStore(flake=flake)
|
inventory_store = InventoryStore(flake=flake)
|
||||||
inventory = inventory_store.read()
|
inventory = inventory_store.read()
|
||||||
|
|
||||||
machines = inventory.get("machines", {})
|
machines = inventory.get("machines", {})
|
||||||
|
|
||||||
if opts and opts.get("filter"):
|
if opts and opts.filter.tags is not None:
|
||||||
filtered_machines = {}
|
filtered_machines = {}
|
||||||
filter_tags = opts.get("filter", {}).get("tags", [])
|
|
||||||
|
|
||||||
for machine_name, machine in machines.items():
|
for machine_name, machine in machines.items():
|
||||||
machine_tags = machine.get("tags", [])
|
machine_tags = machine.get("tags", [])
|
||||||
if all(ft in machine_tags for ft in filter_tags):
|
if all(ft in machine_tags for ft in opts.filter.tags):
|
||||||
filtered_machines[machine_name] = machine
|
filtered_machines[machine_name] = machine
|
||||||
|
|
||||||
return filtered_machines
|
return filtered_machines
|
||||||
|
|||||||
Reference in New Issue
Block a user