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:
parent
a01cb9434b
commit
64649ff7a9
@ -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)
|
||||
|
33
pkgs/clan-cli/clan_cli/flakes/list_history.py
Normal file
33
pkgs/clan-cli/clan_cli/flakes/list_history.py
Normal 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)
|
@ -6,6 +6,7 @@ from typing import Annotated
|
||||
from fastapi import APIRouter, Body, HTTPException, status
|
||||
from pydantic import AnyUrl
|
||||
|
||||
from clan_cli import flakes
|
||||
from clan_cli.webui.api_inputs import (
|
||||
FlakeCreateInput,
|
||||
)
|
||||
@ -50,6 +51,11 @@ async def add_flake(flake_dir: Path) -> None:
|
||||
await add.add_flake(flake_dir)
|
||||
|
||||
|
||||
@router.get("/api/flake/list_history", tags=[Tags.flake])
|
||||
async def list_flakes() -> list[Path]:
|
||||
return flakes.list_history.list_history()
|
||||
|
||||
|
||||
# TODO: Check for directory traversal
|
||||
@router.get("/api/flake/attrs", tags=[Tags.flake])
|
||||
async def inspect_flake_attrs(url: AnyUrl | Path) -> FlakeAttrResponse:
|
||||
|
@ -23,6 +23,30 @@ def test_flake_add(
|
||||
assert open(user_history_file()).read().strip() == str(test_flake.path)
|
||||
|
||||
|
||||
def test_flake_list(
|
||||
api: TestClient, test_flake: FlakeForTest, temporary_home: Path
|
||||
) -> None:
|
||||
response = api.get(
|
||||
"/api/flake/list_history",
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == []
|
||||
|
||||
# add the test_flake
|
||||
response = api.put(
|
||||
f"/api/flake/add?flake_dir={str(test_flake.path)}",
|
||||
json={},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
# list the flakes again
|
||||
response = api.get(
|
||||
"/api/flake/list_history",
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == [str(test_flake.path)]
|
||||
|
||||
|
||||
@pytest.mark.impure
|
||||
def test_inspect_ok(api: TestClient, test_flake_with_core: FlakeForTest) -> None:
|
||||
params = {"url": str(test_flake_with_core.path)}
|
||||
|
@ -2,6 +2,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from cli import Cli
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from pytest import CaptureFixture
|
||||
|
||||
from clan_cli.dirs import user_history_file
|
||||
|
||||
@ -24,3 +25,21 @@ def test_flakes_add(
|
||||
history_file = user_history_file()
|
||||
assert history_file.exists()
|
||||
assert open(history_file).read().strip() == str(test_flake.path)
|
||||
|
||||
|
||||
def test_flakes_list(
|
||||
capsys: CaptureFixture,
|
||||
test_flake: FlakeForTest,
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cmd = [
|
||||
"flakes",
|
||||
"list",
|
||||
]
|
||||
|
||||
cli.run(cmd)
|
||||
assert str(test_flake.path) not in capsys.readouterr().out
|
||||
|
||||
cli.run(["flakes", "add", str(test_flake.path)])
|
||||
cli.run(cmd)
|
||||
assert str(test_flake.path) in capsys.readouterr().out
|
||||
|
Loading…
Reference in New Issue
Block a user