forked from clan/clan-core
Improved history command
This commit is contained in:
parent
21ad905285
commit
8ca573b159
@ -55,29 +55,27 @@ def get_dir_time(path: Path) -> str:
|
|||||||
def add_history(path: Path) -> list[HistoryEntry]:
|
def add_history(path: Path) -> list[HistoryEntry]:
|
||||||
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
||||||
logs = list_history()
|
logs = list_history()
|
||||||
|
|
||||||
found = False
|
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:
|
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.write(json.dumps(logs, cls=EnhancedJSONEncoder, indent=4))
|
||||||
f.truncate()
|
f.truncate()
|
||||||
|
|
||||||
|
@ -11,19 +11,22 @@ from .add import EnhancedJSONEncoder, HistoryEntry, get_dir_time, list_history
|
|||||||
|
|
||||||
|
|
||||||
def update_history() -> list[HistoryEntry]:
|
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:
|
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.write(json.dumps(new_logs, cls=EnhancedJSONEncoder, indent=4))
|
||||||
f.truncate()
|
f.truncate()
|
||||||
return new_logs
|
return new_logs
|
||||||
|
|
||||||
|
|
||||||
def add_update_command(args: argparse.Namespace) -> None:
|
def add_update_command(args: argparse.Namespace) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user