Moved history to own subcommand

This commit is contained in:
Qubasa
2023-12-11 19:09:34 +01:00
parent 87a8f5518b
commit 771c0e43ca
10 changed files with 112 additions and 95 deletions

View File

@@ -1,8 +1,6 @@
# !/usr/bin/env python3
import argparse
from clan_cli.flakes.add import register_add_parser
from clan_cli.flakes.history import register_list_parser
from clan_cli.flakes.inspect import register_inspect_parser
from .create import register_create_parser
@@ -18,9 +16,5 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
)
create_parser = subparser.add_parser("create", help="Create a clan flake")
register_create_parser(create_parser)
add_parser = subparser.add_parser("add", help="Add a clan flake")
register_add_parser(add_parser)
list_parser = subparser.add_parser("list", help="List recently used flakes")
register_list_parser(list_parser)
inspect_parser = subparser.add_parser("inspect", help="Inspect a clan flake")
register_inspect_parser(inspect_parser)

View File

@@ -1,22 +0,0 @@
# !/usr/bin/env python3
import argparse
from pathlib import Path
from clan_cli.flakes.history import push_history
from ..async_cmd import CmdOut, runforcli
async def add_flake(path: Path) -> dict[str, CmdOut]:
push_history(path)
return {}
def add_flake_command(args: argparse.Namespace) -> None:
runforcli(add_flake, args.path)
# takes a (sub)parser and configures it
def register_add_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("path", type=Path, help="Path to the flake", default=Path("."))
parser.set_defaults(func=add_flake_command)

View File

@@ -1,74 +0,0 @@
# !/usr/bin/env python3
import argparse
import dataclasses
import json
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Any
from clan_cli.dirs import user_history_file
from ..locked_open import locked_open
class EnhancedJSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
return super().default(o)
@dataclass
class HistoryEntry:
path: str
last_used: str
def list_history() -> list[HistoryEntry]:
logs: list[HistoryEntry] = []
if not user_history_file().exists():
return []
with locked_open(user_history_file(), "r") as f:
try:
content: str = f.read()
parsed: list[dict] = json.loads(content)
logs = [HistoryEntry(**p) for p in parsed]
except json.JSONDecodeError as ex:
print("Failed to load history. Invalid JSON.")
print(f"{user_history_file()}: {ex}")
return logs
def push_history(path: Path) -> list[HistoryEntry]:
user_history_file().parent.mkdir(parents=True, exist_ok=True)
logs = list_history()
found = False
with locked_open(user_history_file(), "w+") as f:
for entry in logs:
if entry.path == str(path):
found = True
entry.last_used = datetime.now().isoformat()
if not found:
logs.append(
HistoryEntry(path=str(path), last_used=datetime.now().isoformat())
)
f.write(json.dumps(logs, cls=EnhancedJSONEncoder))
f.truncate()
return logs
def list_history_command(args: argparse.Namespace) -> None:
for history_entry in list_history():
print(history_entry.path)
# takes a (sub)parser and configures it
def register_list_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=list_history_command)