This commit is contained in:
Waylon Walker 2023-06-08 09:02:43 -05:00
parent a9ee4a2bd8
commit d68cda91cf
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
22 changed files with 824 additions and 55 deletions

View file

@ -5,6 +5,7 @@ from typer.main import get_group
from learn_sql_model.cli.api import api_app
from learn_sql_model.cli.config import config_app
from learn_sql_model.cli.hero import hero_app
from learn_sql_model.cli.model import model_app
app = typer.Typer(
name="learn_sql_model",
@ -12,7 +13,7 @@ app = typer.Typer(
)
app.add_typer(config_app, name="config")
# app.add_typer(tui_app, name="tui")
# app.add_typer(model_app, name="model")
app.add_typer(model_app, name="model")
app.add_typer(api_app, name="api")
app.add_typer(hero_app, name="hero")

View file

@ -2,6 +2,7 @@ import sys
from typing import List, Optional, Union
from engorgio import engorgio
import httpx
from rich.console import Console
import typer
@ -58,17 +59,17 @@ def create(
config: Config = None,
) -> Hero:
"read all the heros"
# config.init()
hero = hero.post(config=config)
Console().print(hero)
return hero
# config.init()
# with Session(config.database.engine) as session:
# db_hero = Hero.from_orm(hero)
# session.add(db_hero)
# session.commit()
# session.refresh(db_hero)
# return db_hero
r = httpx.post(
f"{config.api_client.url}/hero/",
json=hero.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
# hero = hero.post(config=config)
# Console().print(hero)
# return hero
@hero_app.command()
@ -78,9 +79,12 @@ def update(
config: Config = None,
) -> Hero:
"read all the heros"
hero = hero.update(config=config)
Console().print(hero)
return hero
r = httpx.patch(
f"{config.api_client.url}/hero/",
json=hero.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
@hero_app.command()
@ -90,10 +94,11 @@ def delete(
config: Config = None,
) -> Hero:
"read all the heros"
# config.init()
hero = hero.delete(config=config)
return hero
# Console().print(hero)
r = httpx.delete(
f"{config.api_client.url}/hero/{hero.id}",
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
@hero_app.command()

View file

@ -1,7 +1,9 @@
from pathlib import Path
import alembic
import typer
from alembic.config import Config
from copier import run_auto
import typer
from learn_sql_model.cli.common import verbose_callback
@ -26,18 +28,19 @@ def create(
callback=verbose_callback,
help="show the log messages",
),
template=Path('templates/model')
run_auto(template, Path('.'))
):
template = Path("templates/model")
run_auto(str(template), ".")
@ model_app.command()
@model_app.command()
def create_revision(
verbose: bool=typer.Option(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
message: str=typer.Option(
message: str = typer.Option(
prompt=True,
),
):
@ -51,23 +54,23 @@ def create_revision(
alembic.command.upgrade(config=alembic_cfg, revision="head")
@ model_app.command()
@model_app.command()
def checkout(
verbose: bool=typer.Option(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
revision: str=typer.Option("head"),
revision: str = typer.Option("head"),
):
alembic_cfg = Config("alembic.ini")
alembic.command.upgrade(config=alembic_cfg, revision="head")
@ model_app.command()
@model_app.command()
def populate(
verbose: bool=typer.Option(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",

107
learn_sql_model/cli/new.py Normal file
View file

@ -0,0 +1,107 @@
import sys
from typing import List, Optional, Union
from engorgio import engorgio
from rich.console import Console
import typer
from learn_sql_model.config import Config, get_config
from learn_sql_model.factories.new import newFactory
from learn_sql_model.factories.pet import PetFactory
from learn_sql_model.models.new import (
new,
newCreate,
newDelete,
newRead,
newUpdate,
)
new_app = typer.Typer()
@new_app.callback()
def new():
"model cli"
@new_app.command()
@engorgio(typer=True)
def get(
id: Optional[int] = typer.Argument(default=None),
config: Config = None,
) -> Union[new, List[new]]:
"get one new"
config.init()
new = newRead.get(id=id, config=config)
Console().print(new)
return new
@new_app.command()
@engorgio(typer=True)
def list(
where: Optional[str] = None,
config: Config = None,
offset: int = 0,
limit: Optional[int] = None,
) -> Union[new, List[new]]:
"get one new"
new = newRead.list(config=config, where=where, offset=offset, limit=limit)
Console().print(new)
return new
@new_app.command()
@engorgio(typer=True)
def create(
new: newCreate,
config: Config = None,
) -> new:
"read all the news"
# config.init()
new = new.post(config=config)
Console().print(new)
return new
@new_app.command()
@engorgio(typer=True)
def update(
new: newUpdate,
config: Config = None,
) -> new:
"read all the news"
new = new.update(config=config)
Console().print(new)
return new
@new_app.command()
@engorgio(typer=True)
def delete(
new: newDelete,
config: Config = None,
) -> new:
"read all the news"
# config.init()
new = new.delete(config=config)
return new
@new_app.command()
@engorgio(typer=True)
def populate(
new: new,
n: int = 10,
) -> new:
"read all the news"
config = get_config()
if config.env == "prod":
Console().print("populate is not supported in production")
sys.exit(1)
for new in newFactory().batch(n):
pet = PetFactory().build()
new.pet = pet
Console().print(new)
new.post(config=config)