api/flake/list_history: init

Add an api endpoint to list the history of clan flakes that have been interacted with

Also add `clan flake list`
This commit is contained in:
DavHau
2023-11-17 17:56:07 +07:00
parent a01cb9434b
commit 64649ff7a9
5 changed files with 85 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
import argparse
from clan_cli.flakes.add import register_add_parser
from clan_cli.flakes.list_history import register_list_parser
from .create import register_create_parser
@@ -18,3 +19,5 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
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 clan flakes")
register_list_parser(list_parser)

View File

@@ -0,0 +1,33 @@
# !/usr/bin/env python3
import argparse
from pathlib import Path
from pydantic import AnyUrl
from pydantic.tools import parse_obj_as
from clan_cli.dirs import user_history_file
DEFAULT_URL: AnyUrl = parse_obj_as(
AnyUrl,
"git+https://git.clan.lol/clan/clan-core?new-clan",
)
def list_history() -> list[Path]:
user_history_file().parent.mkdir(parents=True, exist_ok=True)
if not user_history_file().exists():
return []
# read path lines from history file
with open(user_history_file(), "r") as f:
lines = f.readlines()
return [Path(line.strip()) for line in lines]
def list_history_command(args: argparse.Namespace) -> None:
for path in list_history():
print(path)
# takes a (sub)parser and configures it
def register_list_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=list_history_command)