mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 03:05:25 +00:00
feat(cli): create library
This commit is contained in:
parent
8caa5c5a7e
commit
c1daee5f3d
@ -2,23 +2,55 @@ import typer
|
||||
import httpx
|
||||
from memos.server import run_server
|
||||
from tabulate import tabulate
|
||||
from typing import List
|
||||
from pathlib import Path
|
||||
|
||||
app = typer.Typer()
|
||||
lib_app = typer.Typer()
|
||||
app.add_typer(lib_app, name="lib")
|
||||
|
||||
|
||||
def display_libraries(libraries):
|
||||
table = []
|
||||
for library in libraries:
|
||||
table.append(
|
||||
[
|
||||
library["id"],
|
||||
library["name"],
|
||||
"\n".join(
|
||||
f"{folder['id']}: {folder['path']}" for folder in library["folders"]
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
print(tabulate(table, headers=["ID", "Name", "Folders"], tablefmt="plain"))
|
||||
|
||||
|
||||
@app.command()
|
||||
def serve():
|
||||
run_server()
|
||||
|
||||
@app.command()
|
||||
|
||||
@lib_app.command("ls")
|
||||
def ls():
|
||||
response = httpx.get("http://localhost:8080/libraries")
|
||||
libraries = response.json()
|
||||
display_libraries(libraries)
|
||||
|
||||
|
||||
@lib_app.command("create")
|
||||
def add(name: str, folders: List[str]):
|
||||
|
||||
absolute_folders = [str(Path(folder).resolve()) for folder in folders]
|
||||
response = httpx.post(
|
||||
"http://localhost:8080/libraries",
|
||||
json={"name": name, "folders": absolute_folders},
|
||||
)
|
||||
if 200 <= response.status_code < 300:
|
||||
print("Library created successfully")
|
||||
else:
|
||||
print(f"Failed to create library: {response.status_code} - {response.text}")
|
||||
|
||||
table = []
|
||||
for library in libraries:
|
||||
table.append([library['id'], library['name'], "\n".join(folder['path'] for folder in library['folders'])])
|
||||
|
||||
print(tabulate(table, headers=["ID", "Name", "Folders"]))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
|
Loading…
x
Reference in New Issue
Block a user