clan-cli: machines delete: delete the machine's vars and secrets

When a machine is deleted with `clan machines delete`, remove its
vars and legacy secrets, and update any secrets that reference the
machine's key.

This command is a superset of `clan secrets machine delete`, and I am
wondering if we could remove the `clan secrets machine` subcommand,
unless there is an use case for having a machine defined without its
key, and any secrets/vars?

Note:

- This deletes the `ListSecretsOptions` dataclass, as it did not seem to
  bring any value, especially since `list_secrets` was receiving its
  individual members instead of the whole dataclass. We can always bring
  it back if complexity grows to demand it.
This commit is contained in:
Louis Opter
2025-02-07 11:31:38 +00:00
committed by Mic92
parent ef5ad09b2d
commit 538374558d
3 changed files with 45 additions and 25 deletions

View File

@@ -6,7 +6,6 @@ import os
import shutil
import sys
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from typing import IO
@@ -18,7 +17,6 @@ from clan_cli.completions import (
complete_users,
)
from clan_cli.errors import ClanError
from clan_cli.flake import Flake
from clan_cli.git import commit_files
from . import sops
@@ -325,31 +323,26 @@ def has_secret(secret_path: Path) -> bool:
return (secret_path / "secret").exists()
def list_secrets(flake_dir: Path, pattern: str | None = None) -> list[str]:
def list_secrets(
flake_dir: Path, filter_fn: Callable[[str], bool] | None = None
) -> list[str]:
path = sops_secrets_folder(flake_dir)
def validate(name: str) -> bool:
return (
VALID_SECRET_NAME.match(name) is not None
and has_secret(sops_secrets_folder(flake_dir) / name)
and (pattern is None or pattern in name)
and (filter_fn is None or filter_fn(name) is True)
)
return list_objects(path, validate)
@dataclass
class ListSecretsOptions:
flake: Flake
pattern: str | None
def list_command(args: argparse.Namespace) -> None:
options = ListSecretsOptions(
flake=args.flake,
pattern=args.pattern,
)
lst = list_secrets(options.flake.path, options.pattern)
def filter_fn(name: str) -> bool:
return args.pattern in name
lst = list_secrets(args.flake.path, filter_fn if args.pattern else None)
if len(lst) > 0:
print("\n".join(lst))