Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement key-based authentication #56

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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 <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Jun 19, 2024
commit dbee19b64a152114dd3e9c943148699192ad08a1
46 changes: 37 additions & 9 deletions src/frequenz/client/dispatch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import asyncio
import os
import sys
from pprint import pformat
from typing import Any, List

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


# Click command groups
@click.group()
@click.group(invoke_without_command=True)
@click.option(
"--host",
default=DEFAULT_DISPATCH_API_HOST,
Expand All @@ -75,12 +74,24 @@ def get_client(host: str, port: int, key: str) -> Client:
help="API key for authentication",
envvar="DISPATCH_API_KEY",
show_envvar=True,
required=True,
)
@click.pass_context
async def cli(ctx: click.Context, host: str, port: int, key: str) -> None:
"""Dispatch Service CLI."""
ctx.ensure_object(dict)
if ctx.obj is None:
ctx.obj = {}

ctx.obj["client"] = get_client(host, port, key)
ctx.obj["params"] = {
"host": host,
"port": port,
"key": key,
}

# Check if a subcommand was given
if ctx.invoked_subcommand is None:
await interactive_mode(host, port, key)


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


@cli.command()
@click.pass_obj
async def repl(
obj: dict[str, Any],
) -> None:
"""Start an interactive interface."""
click.echo(f"Parameters: {obj}")
await interactive_mode(
obj["params"]["host"], obj["params"]["port"], obj["params"]["key"]
)


@cli.command()
@click.argument("dispatch_ids", type=FuzzyIntRange(), nargs=-1) # Allow multiple IDs
@click.pass_context
Expand Down Expand Up @@ -366,7 +389,7 @@ async def delete(ctx: click.Context, dispatch_ids: list[list[int]]) -> None:
raise click.ClickException("Some deletions failed.")


async def interactive_mode() -> None:
async def interactive_mode(host: str, port: int, key: str) -> None:
"""Interactive mode for the CLI."""
hist_file = os.path.expanduser("~/.dispatch_cli_history.txt")
session: PromptSession[str] = PromptSession(history=FileHistory(filename=hist_file))
Expand Down Expand Up @@ -397,7 +420,15 @@ async def display_help() -> None:
break
else:
# Split, but keep quoted strings together
params = click.parser.split_arg_string(user_input)
params = [
"--host",
host,
"--port",
str(port),
"--key",
key,
] + click.parser.split_arg_string(user_input)

try:
await cli.main(args=params, standalone_mode=False)
except click.ClickException as e:
Expand All @@ -412,10 +443,7 @@ async def display_help() -> None:

def main() -> None:
"""Entrypoint for the CLI."""
if len(sys.argv) > 1:
asyncio.run(cli.main())
else:
asyncio.run(interactive_mode())
asyncio.run(cli.main())


if __name__ == "__main__":
Expand Down