2023-12-11 18:09:34 +00:00
|
|
|
# !/usr/bin/env python3
|
|
|
|
import argparse
|
2024-02-02 03:56:08 +00:00
|
|
|
import datetime
|
|
|
|
|
2024-06-11 10:10:55 +00:00
|
|
|
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
|
2024-01-16 15:16:12 +00:00
|
|
|
from ..errors import ClanCmdError
|
2023-12-31 14:17:12 +00:00
|
|
|
from ..locked_open import write_history_file
|
2023-12-12 17:11:38 +00:00
|
|
|
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:
|
2024-01-16 15:16:12 +00:00
|
|
|
try:
|
|
|
|
meta = nix_metadata(entry.flake.flake_url)
|
|
|
|
except ClanCmdError as e:
|
|
|
|
print(f"Failed to update {entry.flake.flake_url}: {e}")
|
|
|
|
continue
|
2023-12-12 17:11:38 +00:00
|
|
|
|
|
|
|
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}"
|
|
|
|
)
|
2024-01-16 15:16:12 +00:00
|
|
|
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-01-16 15:16:12 +00:00
|
|
|
)
|
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
|
|
|
|
2024-01-16 15:16:12 +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)
|