Skip to content

Commit dbee19b

Browse files
committed
Update REPL client to work better with host/port/key parameters
Before we couldn't really use "--host", "--port" and "--key" parameters with the REPL mode Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 672b9ea commit dbee19b

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/frequenz/client/dispatch/__main__.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import asyncio
77
import os
8-
import sys
98
from pprint import pformat
109
from typing import Any, List
1110

@@ -53,7 +52,7 @@ def get_client(host: str, port: int, key: str) -> Client:
5352

5453

5554
# Click command groups
56-
@click.group()
55+
@click.group(invoke_without_command=True)
5756
@click.option(
5857
"--host",
5958
default=DEFAULT_DISPATCH_API_HOST,
@@ -75,12 +74,24 @@ def get_client(host: str, port: int, key: str) -> Client:
7574
help="API key for authentication",
7675
envvar="DISPATCH_API_KEY",
7776
show_envvar=True,
77+
required=True,
7878
)
7979
@click.pass_context
8080
async def cli(ctx: click.Context, host: str, port: int, key: str) -> None:
8181
"""Dispatch Service CLI."""
82-
ctx.ensure_object(dict)
82+
if ctx.obj is None:
83+
ctx.obj = {}
84+
8385
ctx.obj["client"] = get_client(host, port, key)
86+
ctx.obj["params"] = {
87+
"host": host,
88+
"port": port,
89+
"key": key,
90+
}
91+
92+
# Check if a subcommand was given
93+
if ctx.invoked_subcommand is None:
94+
await interactive_mode(host, port, key)
8495

8596

8697
@cli.command("list")
@@ -334,6 +345,18 @@ async def get(ctx: click.Context, dispatch_ids: List[int]) -> None:
334345
raise click.ClickException("Some gets failed.")
335346

336347

348+
@cli.command()
349+
@click.pass_obj
350+
async def repl(
351+
obj: dict[str, Any],
352+
) -> None:
353+
"""Start an interactive interface."""
354+
click.echo(f"Parameters: {obj}")
355+
await interactive_mode(
356+
obj["params"]["host"], obj["params"]["port"], obj["params"]["key"]
357+
)
358+
359+
337360
@cli.command()
338361
@click.argument("dispatch_ids", type=FuzzyIntRange(), nargs=-1) # Allow multiple IDs
339362
@click.pass_context
@@ -366,7 +389,7 @@ async def delete(ctx: click.Context, dispatch_ids: list[list[int]]) -> None:
366389
raise click.ClickException("Some deletions failed.")
367390

368391

369-
async def interactive_mode() -> None:
392+
async def interactive_mode(host: str, port: int, key: str) -> None:
370393
"""Interactive mode for the CLI."""
371394
hist_file = os.path.expanduser("~/.dispatch_cli_history.txt")
372395
session: PromptSession[str] = PromptSession(history=FileHistory(filename=hist_file))
@@ -397,7 +420,15 @@ async def display_help() -> None:
397420
break
398421
else:
399422
# Split, but keep quoted strings together
400-
params = click.parser.split_arg_string(user_input)
423+
params = [
424+
"--host",
425+
host,
426+
"--port",
427+
str(port),
428+
"--key",
429+
key,
430+
] + click.parser.split_arg_string(user_input)
431+
401432
try:
402433
await cli.main(args=params, standalone_mode=False)
403434
except click.ClickException as e:
@@ -412,10 +443,7 @@ async def display_help() -> None:
412443

413444
def main() -> None:
414445
"""Entrypoint for the CLI."""
415-
if len(sys.argv) > 1:
416-
asyncio.run(cli.main())
417-
else:
418-
asyncio.run(interactive_mode())
446+
asyncio.run(cli.main())
419447

420448

421449
if __name__ == "__main__":

0 commit comments

Comments
 (0)