-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recap now has a CLI! 💫 The CLI supports two commands: `ls` and `schema`. These behave the same way as they do in Recap's gateway. I've updated the Recap gateway to share the same logic as the CLI. I did this by moving everything into a `commands` file, and calling it from both `cli` and `gateway`.
- Loading branch information
1 parent
7393fcc
commit c36acee
Showing
8 changed files
with
174 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Annotated | ||
|
||
import typer | ||
from rich import print_json | ||
|
||
from recap import commands | ||
from recap.types import to_dict | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def ls(path: Annotated[str, typer.Argument(help="Path to list children of.")] = "/"): | ||
""" | ||
List the children of a path. | ||
""" | ||
|
||
if children := commands.ls(path): | ||
print_json(data=children) | ||
|
||
|
||
@app.command() | ||
def schema(path: Annotated[str, typer.Argument(help="Path to get schema of.")]): | ||
""" | ||
Get the schema of a path. | ||
""" | ||
|
||
if recap_struct := commands.schema(path): | ||
print_json(data=to_dict(recap_struct)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from recap.clients import create_client | ||
from recap.settings import RecapSettings | ||
from recap.types import StructType | ||
|
||
settings = RecapSettings() | ||
|
||
|
||
def ls(path: str = "/") -> list[str] | None: | ||
""" | ||
List the children of a path. | ||
:param path: Path to list children of. Defaults to root. | ||
:return: List of children. | ||
""" | ||
|
||
system, *args = _args(path) if path not in ("", "/") else [None] | ||
if not system: | ||
return list(settings.systems.keys()) | ||
if system and (url := settings.systems.get(system)): | ||
with create_client(url.unicode_string()) as client: | ||
return client.ls(*args) | ||
|
||
|
||
def schema(path: str) -> StructType | None: | ||
""" | ||
Get the schema of a path. | ||
:param path: Path to get schema of. | ||
:return: Schema of path. | ||
""" | ||
|
||
system, *args = _args(path) | ||
if system and (url := settings.systems.get(system)): | ||
with create_client(url.unicode_string()) as client: | ||
return client.get_schema(*args) | ||
|
||
|
||
def _args(path: str | None) -> list[str]: | ||
return path.strip("/").split("/") if path else [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from fastapi import FastAPI, HTTPException | ||
|
||
from recap import commands | ||
from recap.types import to_dict | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/ls/{path:path}") | ||
async def ls(path: str = "/") -> list[str]: | ||
""" | ||
List the children of a path. | ||
""" | ||
|
||
children = commands.ls(path) | ||
if children is not None: | ||
return children | ||
raise HTTPException(status_code=404, detail="Path not found") | ||
|
||
|
||
@app.get("/schema/{path:path}") | ||
async def schema(path: str) -> dict: | ||
""" | ||
Get the schema of a path. | ||
""" | ||
|
||
if recap_struct := commands.schema(path): | ||
recap_dict = to_dict(recap_struct) | ||
if not isinstance(recap_dict, dict): | ||
raise HTTPException( | ||
status_code=503, | ||
detail=f"Expected a schema dict, but got {type(recap_dict)}", | ||
) | ||
return recap_dict | ||
raise HTTPException(status_code=404, detail="Path not found") |
Empty file.
This file was deleted.
Oops, something went wrong.
File renamed without changes.