Make instance work without logging in
This commit is contained in:
parent
6cf0e84d7a
commit
438a8ab206
|
@ -46,7 +46,7 @@ def test_instance_anon(app, run_anon, base_url):
|
||||||
# Need to specify the instance name when running anon
|
# Need to specify the instance name when running anon
|
||||||
result = run_anon(cli.read.instance)
|
result = run_anon(cli.read.instance)
|
||||||
assert result.exit_code == 1
|
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):
|
def test_whoami(user, run):
|
||||||
|
|
|
@ -85,12 +85,12 @@ def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[
|
||||||
"""Pass the toot Context as first argument."""
|
"""Pass the toot Context as first argument."""
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def wrapped(*args: "P.args", **kwargs: "P.kwargs") -> R:
|
def wrapped(*args: "P.args", **kwargs: "P.kwargs") -> R:
|
||||||
return f(_get_context(), *args, **kwargs)
|
return f(get_context(), *args, **kwargs)
|
||||||
|
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
def _get_context() -> Context:
|
def get_context() -> Context:
|
||||||
click_context = click.get_current_context()
|
click_context = click.get_current_context()
|
||||||
obj: TootObj = click_context.obj
|
obj: TootObj = click_context.obj
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ from toot.cli.validators import validate_instance
|
||||||
from toot.entities import Instance, Status, from_dict, Account
|
from toot.entities import Instance, Status, from_dict, Account
|
||||||
from toot.exceptions import ApiError, ConsoleError
|
from toot.exceptions import ApiError, ConsoleError
|
||||||
from toot.output import print_account, print_instance, print_search_results, print_status, print_timeline
|
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()
|
@cli.command()
|
||||||
|
@ -43,30 +43,33 @@ def whois(ctx: Context, account: str, json: bool):
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument("instance_url", required=False, callback=validate_instance)
|
@click.argument("instance", callback=validate_instance, required=False)
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
def instance(instance: Optional[str], json: bool):
|
||||||
def instance(ctx: Context, instance_url: Optional[str], json: bool):
|
"""Display instance details
|
||||||
"""Display instance details"""
|
|
||||||
default_url = ctx.app.base_url if ctx.app else None
|
|
||||||
base_url = instance_url or default_url
|
|
||||||
|
|
||||||
if not base_url:
|
INSTANCE can be a domain or base URL of the instance to display.
|
||||||
raise ConsoleError("Please specify an instance.")
|
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:
|
try:
|
||||||
response = api.get_instance(base_url)
|
response = api.get_instance(instance)
|
||||||
except ApiError:
|
except ApiError:
|
||||||
raise ConsoleError(
|
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."
|
"The given domain probably does not host a Mastodon instance."
|
||||||
)
|
)
|
||||||
|
|
||||||
if json:
|
if json:
|
||||||
click.echo(response.text)
|
click.echo(response.text)
|
||||||
else:
|
else:
|
||||||
instance = from_dict(Instance, response.json())
|
print_instance(from_dict(Instance, response.json()))
|
||||||
print_instance(instance)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
Loading…
Reference in New Issue