allow to overwrite hardware configuration

now that we call it "update" hardware configurration and we are heading
towards facter anyway, we don't need all the force overide logic. Just
allow this to be overwritten by default.
This commit is contained in:
Jörg Thalheim
2024-09-29 16:45:14 +02:00
parent deea4a74b3
commit bd8bc98863

View File

@@ -97,7 +97,6 @@ class HardwareGenerateOptions:
flake: FlakeId flake: FlakeId
machine: str machine: str
backend: Literal["nixos-generate-config", "nixos-facter"] backend: Literal["nixos-generate-config", "nixos-facter"]
force: bool = False
target_host: str | None = None target_host: str | None = None
keyfile: str | None = None keyfile: str | None = None
password: str | None = None password: str | None = None
@@ -120,15 +119,6 @@ def generate_machine_hardware_info(opts: HardwareGenerateOptions) -> HardwareRep
else: else:
hw_file /= facter_file hw_file /= facter_file
# Check if the hardware-configuration.nix file is a template
is_template = hw_file.exists() and "throw ''" in hw_file.read_text()
if hw_file.exists() and not opts.force and not is_template:
msg = "File exists"
raise ClanError(
msg,
description=f"'{hw_file}' already exists. To force overwrite the existing configuration use '--force'.",
)
hw_file.parent.mkdir(parents=True, exist_ok=True) hw_file.parent.mkdir(parents=True, exist_ok=True)
if opts.backend == "nixos-facter": if opts.backend == "nixos-facter":
@@ -174,12 +164,6 @@ def generate_machine_hardware_info(opts: HardwareGenerateOptions) -> HardwareRep
raise ClanError(msg) raise ClanError(msg)
backup_file = None backup_file = None
if hw_file.exists() and opts.force:
# Backup the existing file
backup_file = hw_file.with_suffix(".bak")
hw_file.replace(backup_file)
print(f"Backed up existing {hw_file} to {backup_file}")
with hw_file.open("w") as f: with hw_file.open("w") as f:
f.write(out.stdout) f.write(out.stdout)
print(f"Successfully generated: {hw_file}") print(f"Successfully generated: {hw_file}")
@@ -217,7 +201,6 @@ def update_hardware_config_command(args: argparse.Namespace) -> None:
machine=args.machine, machine=args.machine,
target_host=args.target_host, target_host=args.target_host,
password=args.password, password=args.password,
force=args.force,
backend=args.backend, backend=args.backend,
) )
generate_machine_hardware_info(opts) generate_machine_hardware_info(opts)
@@ -250,9 +233,4 @@ def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
choices=["nixos-generate-config", "nixos-facter"], choices=["nixos-generate-config", "nixos-facter"],
default="nixos-generate-config", default="nixos-generate-config",
) )
machine_parser = parser.add_argument(
"--force",
help="Will overwrite the hardware-configuration.nix file.",
action="store_true",
)
add_dynamic_completer(machine_parser, complete_machines) add_dynamic_completer(machine_parser, complete_machines)