mirror of
https://github.com/ihabunek/toot.git
synced 2024-09-29 04:35:54 -04:00
Add toot --as option to override active account
This commit is contained in:
parent
41b77cc9de
commit
ca2912fa78
@ -4,11 +4,14 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
|
from click.shell_completion import CompletionItem
|
||||||
from click.testing import Result
|
from click.testing import Result
|
||||||
|
from click.types import StringParamType
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from toot import App, User, config, __version__
|
from toot import App, User, config, __version__
|
||||||
from toot.settings import get_settings
|
|
||||||
from toot.output import print_warning
|
from toot.output import print_warning
|
||||||
|
from toot.settings import get_settings
|
||||||
|
|
||||||
if t.TYPE_CHECKING:
|
if t.TYPE_CHECKING:
|
||||||
import typing_extensions as te
|
import typing_extensions as te
|
||||||
@ -89,10 +92,24 @@ class TootObj(t.NamedTuple):
|
|||||||
"""Data to add to Click context"""
|
"""Data to add to Click context"""
|
||||||
color: bool = True
|
color: bool = True
|
||||||
debug: bool = False
|
debug: bool = False
|
||||||
|
as_user: t.Optional[str] = None
|
||||||
# Pass a context for testing purposes
|
# Pass a context for testing purposes
|
||||||
test_ctx: t.Optional[Context] = None
|
test_ctx: t.Optional[Context] = None
|
||||||
|
|
||||||
|
|
||||||
|
class AccountParamType(StringParamType):
|
||||||
|
"""Custom type to add shell completion for account names"""
|
||||||
|
name = "account"
|
||||||
|
|
||||||
|
def shell_complete(self, ctx, param, incomplete: str):
|
||||||
|
users = config.load_config()["users"].keys()
|
||||||
|
return [
|
||||||
|
CompletionItem(u)
|
||||||
|
for u in users
|
||||||
|
if u.lower().startswith(incomplete.lower())
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[P, R]":
|
def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[P, R]":
|
||||||
"""Pass the toot Context as first argument."""
|
"""Pass the toot Context as first argument."""
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
@ -110,6 +127,11 @@ def get_context() -> Context:
|
|||||||
if obj.test_ctx:
|
if obj.test_ctx:
|
||||||
return obj.test_ctx
|
return obj.test_ctx
|
||||||
|
|
||||||
|
if obj.as_user:
|
||||||
|
user, app = config.get_user_app(obj.as_user)
|
||||||
|
if not user or not app:
|
||||||
|
raise click.ClickException(f"Account '{obj.as_user}' not found. Run `toot auth` to see available accounts.")
|
||||||
|
else:
|
||||||
user, app = config.get_active_user_app()
|
user, app = config.get_active_user_app()
|
||||||
if not user or not app:
|
if not user or not app:
|
||||||
raise click.ClickException("This command requires you to be logged in.")
|
raise click.ClickException("This command requires you to be logged in.")
|
||||||
@ -129,11 +151,12 @@ json_option = click.option(
|
|||||||
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
|
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
|
||||||
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
|
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
|
||||||
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
|
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
|
||||||
|
@click.option("--as", "as_user", type=AccountParamType(), help="The account to use, overrides the active account.")
|
||||||
@click.version_option(__version__, message="%(prog)s v%(version)s")
|
@click.version_option(__version__, message="%(prog)s v%(version)s")
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool):
|
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, as_user: str):
|
||||||
"""Toot is a Mastodon CLI"""
|
"""Toot is a Mastodon CLI"""
|
||||||
ctx.obj = TootObj(color, debug)
|
ctx.obj = TootObj(color, debug, as_user)
|
||||||
ctx.color = color
|
ctx.color = color
|
||||||
ctx.max_content_width = max_width
|
ctx.max_content_width = max_width
|
||||||
|
|
||||||
|
@ -2,13 +2,10 @@ import click
|
|||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from click.shell_completion import CompletionItem
|
|
||||||
|
|
||||||
from click.types import StringParamType
|
|
||||||
|
|
||||||
from toot import api, config, __version__
|
from toot import api, config, __version__
|
||||||
from toot.auth import get_or_create_app, login_auth_code, login_username_password
|
from toot.auth import get_or_create_app, login_auth_code, login_username_password
|
||||||
from toot.cli import cli
|
from toot.cli import AccountParamType, cli
|
||||||
from toot.cli.validators import validate_instance
|
from toot.cli.validators import validate_instance
|
||||||
|
|
||||||
|
|
||||||
@ -22,18 +19,6 @@ instance_option = click.option(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class AccountParamType(StringParamType):
|
|
||||||
"""Custom type to add shell completion for account names"""
|
|
||||||
|
|
||||||
def shell_complete(self, ctx, param, incomplete: str):
|
|
||||||
accounts = config.load_config()["users"].keys()
|
|
||||||
return [
|
|
||||||
CompletionItem(a)
|
|
||||||
for a in accounts
|
|
||||||
if a.lower().startswith(incomplete.lower())
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def auth():
|
def auth():
|
||||||
"""Show logged in accounts and instances"""
|
"""Show logged in accounts and instances"""
|
||||||
|
Loading…
Reference in New Issue
Block a user