From 2c0567ca274155e7800e22d5c092dda3aab73bc7 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Mon, 10 Feb 2025 17:16:50 +0700 Subject: [PATCH] Cli: delete machines bugfix. Dont modify the inventory in other places --- pkgs/clan-cli/clan_cli/machines/delete.py | 17 +++-------------- pkgs/clan-cli/tests/test_machines_cli.py | 9 +++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/delete.py b/pkgs/clan-cli/clan_cli/machines/delete.py index 5234fa487..18adc78c4 100644 --- a/pkgs/clan-cli/clan_cli/machines/delete.py +++ b/pkgs/clan-cli/clan_cli/machines/delete.py @@ -6,27 +6,16 @@ from clan_cli.api import API from clan_cli.clan_uri import Flake from clan_cli.completions import add_dynamic_completer, complete_machines from clan_cli.dirs import specific_machine_dir -from clan_cli.errors import ClanError -from clan_cli.inventory import get_inventory, set_inventory +from clan_cli.inventory import delete log = logging.getLogger(__name__) @API.register def delete_machine(flake: Flake, name: str) -> None: - inventory = get_inventory(flake.path) - - if "machines" not in inventory: - msg = "No machines in inventory" - raise ClanError(msg) - - machine = inventory["machines"].pop(name, None) - if machine is None: - msg = f"Machine {name} does not exist" - raise ClanError(msg) - - set_inventory(inventory, flake.path, f"Delete machine {name}") + delete(str(flake.path), {f"machines.{name}"}) + # Remove the machine directory folder = specific_machine_dir(flake.path, name) if folder.exists(): shutil.rmtree(folder) diff --git a/pkgs/clan-cli/tests/test_machines_cli.py b/pkgs/clan-cli/tests/test_machines_cli.py index b79223909..9b31e5438 100644 --- a/pkgs/clan-cli/tests/test_machines_cli.py +++ b/pkgs/clan-cli/tests/test_machines_cli.py @@ -1,4 +1,5 @@ import pytest +from clan_cli.inventory import load_inventory_json from fixtures_flakes import FlakeForTest from helpers import cli from stdout import CaptureOutput @@ -21,6 +22,10 @@ def test_machine_subcommands( ] ) + inventory: dict = dict(load_inventory_json(str(test_flake_with_core.path))) + assert "machine1" in inventory["machines"] + assert "service" not in inventory + with capture_output as output: cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) @@ -33,6 +38,10 @@ def test_machine_subcommands( ["machines", "delete", "--flake", str(test_flake_with_core.path), "machine1"] ) + inventory_2: dict = dict(load_inventory_json(str(test_flake_with_core.path))) + assert "machine1" not in inventory_2["machines"] + assert "service" not in inventory_2 + with capture_output as output: cli.run(["machines", "list", "--flake", str(test_flake_with_core.path)]) assert "machine1" not in output.out