From 438a8ab2061ad260d985c2b13d116be73d9d7d18 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 14 Dec 2023 13:04:05 +0100 Subject: [PATCH] Make instance work without logging in --- tests/integration/test_read.py | 2 +- toot/cli/__init__.py | 4 ++-- toot/cli/read.py | 29 ++++++++++++++++------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/tests/integration/test_read.py b/tests/integration/test_read.py index 5c9e4bb..4a85792 100644 --- a/tests/integration/test_read.py +++ b/tests/integration/test_read.py @@ -46,7 +46,7 @@ def test_instance_anon(app, run_anon, base_url): # Need to specify the instance name when running anon result = run_anon(cli.read.instance) assert result.exit_code == 1 - assert result.stderr == "Error: Please specify an instance.\n" + assert result.stderr.strip() == "Error: INSTANCE argument not given and not logged in" def test_whoami(user, run): diff --git a/toot/cli/__init__.py b/toot/cli/__init__.py index 0e345ac..6e888a6 100644 --- a/toot/cli/__init__.py +++ b/toot/cli/__init__.py @@ -85,12 +85,12 @@ def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[ """Pass the toot Context as first argument.""" @wraps(f) def wrapped(*args: "P.args", **kwargs: "P.kwargs") -> R: - return f(_get_context(), *args, **kwargs) + return f(get_context(), *args, **kwargs) return wrapped -def _get_context() -> Context: +def get_context() -> Context: click_context = click.get_current_context() obj: TootObj = click_context.obj diff --git a/toot/cli/read.py b/toot/cli/read.py index 341fb8e..cfbcea8 100644 --- a/toot/cli/read.py +++ b/toot/cli/read.py @@ -9,7 +9,7 @@ from toot.cli.validators import validate_instance from toot.entities import Instance, Status, from_dict, Account from toot.exceptions import ApiError, ConsoleError from toot.output import print_account, print_instance, print_search_results, print_status, print_timeline -from toot.cli import cli, json_option, pass_context, Context +from toot.cli import cli, get_context, json_option, pass_context, Context @cli.command() @@ -43,30 +43,33 @@ def whois(ctx: Context, account: str, json: bool): @cli.command() -@click.argument("instance_url", required=False, callback=validate_instance) +@click.argument("instance", callback=validate_instance, required=False) @json_option -@pass_context -def instance(ctx: Context, instance_url: Optional[str], json: bool): - """Display instance details""" - default_url = ctx.app.base_url if ctx.app else None - base_url = instance_url or default_url +def instance(instance: Optional[str], json: bool): + """Display instance details - if not base_url: - raise ConsoleError("Please specify an instance.") + INSTANCE can be a domain or base URL of the instance to display. + e.g. 'mastodon.social' or 'https://mastodon.social'. If not + given will display details for the currently logged in instance. + """ + if not instance: + context = get_context() + if not context.app: + raise click.ClickException("INSTANCE argument not given and not logged in") + instance = context.app.base_url try: - response = api.get_instance(base_url) + response = api.get_instance(instance) except ApiError: raise ConsoleError( - f"Instance not found at {base_url}.\n" + + f"Instance not found at {instance}.\n" + "The given domain probably does not host a Mastodon instance." ) if json: click.echo(response.text) else: - instance = from_dict(Instance, response.json()) - print_instance(instance) + print_instance(from_dict(Instance, response.json())) @cli.command()