Merge pull request 'api/flake/add: init' (#527) from DavHau-dave into main
All checks were successful
assets1 / test (push) Successful in 21s
checks / test (push) Successful in 41s
checks-impure / test (push) Successful in 1m29s

This commit is contained in:
clan-bot 2023-11-17 10:08:12 +00:00
commit 1233ff2644
6 changed files with 90 additions and 1 deletions

View File

@ -36,6 +36,10 @@ def user_config_dir() -> Path:
return Path(os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")))
def user_history_file() -> Path:
return user_config_dir() / "clan" / "history"
def machines_dir(flake_dir: Path) -> Path:
return flake_dir / "machines"

View File

@ -1,6 +1,8 @@
# !/usr/bin/env python3
import argparse
from clan_cli.flakes.add import register_add_parser
from .create import register_create_parser
@ -14,3 +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)

View File

@ -0,0 +1,35 @@
# !/usr/bin/env python3
import argparse
from pathlib import Path
from typing import Dict
from pydantic import AnyUrl
from pydantic.tools import parse_obj_as
from clan_cli.dirs import user_history_file
from ..async_cmd import CmdOut, runforcli
DEFAULT_URL: AnyUrl = parse_obj_as(
AnyUrl,
"git+https://git.clan.lol/clan/clan-core?new-clan",
)
async def add_flake(path: Path) -> Dict[str, CmdOut]:
user_history_file().parent.mkdir(parents=True, exist_ok=True)
# append line to history file
# TODO: Is this atomic?
with open(user_history_file(), "a+") as f:
f.write(f"{path}\n")
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

@ -17,7 +17,7 @@ from clan_cli.webui.api_outputs import (
)
from ...async_cmd import run
from ...flakes import create
from ...flakes import add, create
from ...nix import nix_command, nix_flake_show
from ..tags import Tags
@ -45,6 +45,11 @@ async def get_attrs(url: AnyUrl | Path) -> list[str]:
return flake_attrs
@router.put("/api/flake/add", tags=[Tags.flake])
async def add_flake(flake_dir: Path) -> None:
await add.add_flake(flake_dir)
# TODO: Check for directory traversal
@router.get("/api/flake/attrs", tags=[Tags.flake])
async def inspect_flake_attrs(url: AnyUrl | Path) -> FlakeAttrResponse:

View File

@ -4,10 +4,25 @@ import logging
import pytest
from api import TestClient
from fixtures_flakes import FlakeForTest
from path import Path
from clan_cli.dirs import user_history_file
log = logging.getLogger(__name__)
def test_flake_add(
api: TestClient, test_flake: FlakeForTest, temporary_home: Path
) -> None:
response = api.put(
f"/api/flake/add?flake_dir={str(test_flake.path)}",
json={},
)
assert response.status_code == 200, response.json()
assert user_history_file().exists()
assert open(user_history_file()).read().strip() == 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)}

View File

@ -0,0 +1,26 @@
from typing import TYPE_CHECKING
from cli import Cli
from fixtures_flakes import FlakeForTest
from clan_cli.dirs import user_history_file
if TYPE_CHECKING:
pass
def test_flakes_add(
test_flake: FlakeForTest,
) -> None:
cli = Cli()
cmd = [
"flakes",
"add",
str(test_flake.path),
]
cli.run(cmd)
history_file = user_history_file()
assert history_file.exists()
assert open(history_file).read().strip() == str(test_flake.path)