clan-core/pkgs/clan-cli/clan_cli/history/update.py

50 lines
1.4 KiB
Python
Raw Normal View History

2023-12-11 18:09:34 +00:00
# !/usr/bin/env python3
import argparse
2024-02-02 03:56:08 +00:00
import datetime
from clan_cli.clan.inspect import inspect_flake
2023-12-11 18:09:34 +00:00
2024-03-07 12:04:48 +00:00
from ..clan_uri import ClanURI
from ..errors import ClanCmdError
from ..locked_open import write_history_file
from ..nix import nix_metadata
2024-02-02 03:56:08 +00:00
from .add import HistoryEntry, list_history
2023-12-11 18:09:34 +00:00
def update_history() -> list[HistoryEntry]:
2023-12-11 18:26:29 +00:00
logs = list_history()
for entry in logs:
try:
meta = nix_metadata(entry.flake.flake_url)
except ClanCmdError as e:
print(f"Failed to update {entry.flake.flake_url}: {e}")
continue
new_hash = meta["locked"]["narHash"]
if new_hash != entry.flake.nar_hash:
print(
f"Updating {entry.flake.flake_url} from {entry.flake.nar_hash} to {new_hash}"
)
uri = ClanURI.from_str(
url=str(entry.flake.flake_url),
2024-03-07 12:04:48 +00:00
machine_name=entry.flake.flake_attr,
)
2024-03-07 12:04:48 +00:00
flake = inspect_flake(uri.get_url(), uri.machine.name)
2024-02-02 03:56:08 +00:00
flake.flake_url = str(flake.flake_url)
entry = HistoryEntry(
flake=flake, last_used=datetime.datetime.now().isoformat()
)
2023-12-11 18:26:29 +00:00
write_history_file(logs)
return logs
2023-12-11 18:09:34 +00:00
def add_update_command(args: argparse.Namespace) -> None:
update_history()
# takes a (sub)parser and configures it
def register_update_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=add_update_command)