Skip to content

Commit

Permalink
refactor: mutation interactions with the cli
Browse files Browse the repository at this point in the history
implemented the first complete mutation cycle with the cli for alarm
comments, moved to using the `log` methods to follow output wisdom
defined by clig.dev REFS #35
  • Loading branch information
devraj committed May 6, 2024
1 parent ed512f5 commit 07eb407
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
72 changes: 58 additions & 14 deletions gallagher/cli/alarms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@

import typer

from rich.console import Console
from rich.progress import Progress, TaskID
from rich.table import Table

from .utils import AsyncTyper

from ..exception import (
NotFoundException,
)

from gallagher.cc.alarms import (
Alarms,
)

app = AsyncTyper(
help="list or query alarms in the command centre"
)
Expand All @@ -22,15 +34,15 @@ async def list():
"""
console = Console()
with console.status(
"[bold green]Fetching divisions...",
"[bold green]Fetching alarms...",
spinner="clock"
):
divisions = await Division.list()
table = Table(title="Divisions")
for header in divisions.cli_header:
alarms = await Alarms.list()
table = Table(title="Alarms")
for header in alarms.cli_header:
table.add_column(header)

for row in divisions.__rich_repr__():
for row in alarms.__rich_repr__():
table.add_row(*row)

console.print(table)
Expand All @@ -51,22 +63,46 @@ async def get(
@app.command("comment")
async def comment(
id: Annotated[int, typer.Argument(help="alarm id")],
comment: Annotated[
message: Annotated[
str,
typer.Option(help="comment to add to history"
)],
typer.Option(
'-m',
'--message',
help="comment to add to history",
)
],
):
""" comment on an alarm
"""
console = Console()
with console.status(
"[magenta] Commenting on alarm ...",
) as status:
try:
console.log("Finding alarm ...")
comment_detail = await Alarms.retrieve(id)
console.log("Adding comment to history ...")
await Alarms.comment(
comment_detail,
message
)
console.print("[green]Comment posted successfully[/green]")
except NotFoundException as e:
console.print(f"[red bold]No alarm with id={id} found")
raise typer.Exit(code=1)



@app.command("ack")
async def acknowledge(
id: Annotated[int, typer.Argument(help="alarm id")],
comment: Annotated[
message: Annotated[
Optional[str],
typer.Option(help="comment to add to history")
typer.Option(
'-m',
'--message',
help="comment to add to history",
)
] = None,
):
""" acknowledge an alarm, optionally with a comment
Expand All @@ -77,9 +113,13 @@ async def acknowledge(
@app.command("view")
async def acknowledge(
id: Annotated[int, typer.Argument(help="alarm id")],
comment: Annotated[
message: Annotated[
Optional[str],
typer.Option(help="comment to add to history")
typer.Option(
'-m',
'--message',
help="comment to add to history",
)
] = None,
):
""" mark alarm as viewed, optionally with a comment
Expand All @@ -90,9 +130,13 @@ async def acknowledge(
@app.command("process")
async def acknowledge(
id: Annotated[int, typer.Argument(help="alarm id")],
comment: Annotated[
message: Annotated[
Optional[str],
typer.Option(help="comment to add to history")
typer.Option(
'-m',
'--message',
help="comment to add to history",
)
] = None,
):
""" mark alarm as processed, optionally with a comment
Expand Down
21 changes: 13 additions & 8 deletions gallagher/cli/cardholders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import typer

from rich import print as rprint
from rich.console import Console
from rich.table import Table

Expand All @@ -29,7 +28,7 @@ async def list():
console = Console()
with console.status(
"[bold green]Fetching cardholders...",
spinner="clock"
spinner="dots"
):
cardholders = await Cardholder.list()

Expand All @@ -47,9 +46,15 @@ async def list():
async def get(id: int):
""" get a cardholder by id
"""
try:
cardholder = await Cardholder.retrieve(id)
[rprint(r) for r in cardholder.__rich_repr__()]
except NotFoundException as e:
rprint(f"[bold]No cardholder with id={id} found[/bold]")
raise typer.Exit(code=1)
console = Console()
with console.status(
"[bold]Finding cardholder...",
spinner="dots"
):
try:

cardholder = await Cardholder.retrieve(id)
[console.print(r) for r in cardholder.__rich_repr__()]
except NotFoundException as e:
console.print(f"[bold]No cardholder with id={id} found[/bold]")
raise typer.Exit(code=1)

0 comments on commit 07eb407

Please sign in to comment.