From 8ca573b159ed1a5d5a2ba45a7554f50ca8ef4065 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Mon, 11 Dec 2023 19:26:29 +0100 Subject: [PATCH] Improved history command --- pkgs/clan-cli/clan_cli/history/add.py | 40 +++++++++++------------- pkgs/clan-cli/clan_cli/history/update.py | 23 ++++++++------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/history/add.py b/pkgs/clan-cli/clan_cli/history/add.py index 7e410262..d40520f9 100644 --- a/pkgs/clan-cli/clan_cli/history/add.py +++ b/pkgs/clan-cli/clan_cli/history/add.py @@ -55,29 +55,27 @@ def get_dir_time(path: Path) -> str: def add_history(path: Path) -> list[HistoryEntry]: user_history_file().parent.mkdir(parents=True, exist_ok=True) logs = list_history() - found = False + + for entry in logs: + if entry.path == str(path): + found = True + entry.last_used = datetime.datetime.now().isoformat() + + flake = inspect_flake(path, "defaultVM") + flake.flake_url = str(flake.flake_url) + dir_datetime = get_dir_time(path) + + history = HistoryEntry( + flake=flake, + dir_datetime=dir_datetime, + path=str(path), + last_used=datetime.datetime.now().isoformat(), + ) + if not found: + logs.append(history) + with locked_open(user_history_file(), "w+") as f: - for entry in logs: - if entry.path == str(path): - found = True - entry.last_used = datetime.datetime.now().isoformat() - - flake = inspect_flake(path, "defaultVM") - - flake.flake_url = str(flake.flake_url) - dir_datetime = get_dir_time(path) - - history = HistoryEntry( - flake=flake, - dir_datetime=dir_datetime, - path=str(path), - last_used=datetime.datetime.now().isoformat(), - ) - - if not found: - logs.append(history) - f.write(json.dumps(logs, cls=EnhancedJSONEncoder, indent=4)) f.truncate() diff --git a/pkgs/clan-cli/clan_cli/history/update.py b/pkgs/clan-cli/clan_cli/history/update.py index d39182b7..99c2dab6 100644 --- a/pkgs/clan-cli/clan_cli/history/update.py +++ b/pkgs/clan-cli/clan_cli/history/update.py @@ -11,19 +11,22 @@ from .add import EnhancedJSONEncoder, HistoryEntry, get_dir_time, list_history def update_history() -> list[HistoryEntry]: + logs = list_history() + + new_logs = [] + for entry in logs: + new_entry = copy.deepcopy(entry) + new_time = get_dir_time(Path(entry.path)) + if new_time != entry.dir_datetime: + print(f"Updating {entry.path} from {entry.dir_datetime} to {new_time}") + new_entry.dir_datetime = new_time + new_entry.last_used = datetime.datetime.now().isoformat() + new_logs.append(new_entry) + with locked_open(user_history_file(), "w+") as f: - logs = list_history() - new_logs = [] - for entry in logs: - new_entry = copy.deepcopy(entry) - new_time = get_dir_time(Path(entry.path)) - if new_time != entry.dir_datetime: - new_entry.dir_datetime = new_time - new_entry.last_used = datetime.datetime.now().isoformat() - new_logs.append(new_entry) f.write(json.dumps(new_logs, cls=EnhancedJSONEncoder, indent=4)) f.truncate() - return new_logs + return new_logs def add_update_command(args: argparse.Namespace) -> None: