api/flake/add: init

Adds an endpoint to memoize clans.
clan flakes can be added to the history either via the endpoint or by executing `clan flakes add`
This commit is contained in:
DavHau
2023-11-17 17:05:15 +07:00
parent 43dbdf20d4
commit 999ad67277
6 changed files with 90 additions and 1 deletions

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)