Cli: delete machines bugfix. Dont modify the inventory in other places

This commit is contained in:
Johannes Kirschbauer
2025-02-10 17:16:50 +07:00
parent 2412513ad4
commit 153b5560c3
2 changed files with 12 additions and 14 deletions

View File

@@ -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)