-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import click | ||
import platform | ||
import sys | ||
import os | ||
|
||
from toot import cli, config, __version__ | ||
from toot.auth import create_app_interactive, login_interactive | ||
from toot.cli.base import Context, cli, pass_context | ||
from toot.output import print_list_accounts, print_lists | ||
from toot.utils import args_get_instance | ||
|
||
|
||
@cli.command() | ||
def auth(): | ||
config_data = config.load_config() | ||
|
||
if not config_data["users"]: | ||
click.echo("You are not logged in to any accounts") | ||
return | ||
|
||
active_user = config_data["active_user"] | ||
|
||
click.echo("Authenticated accounts:") | ||
for uid, u in config_data["users"].items(): | ||
active_label = "ACTIVE" if active_user == uid else "" | ||
uid = click.style(uid, fg="green") | ||
active_label = click.style(active_label, fg="yellow") | ||
click.echo(f"* {uid} {active_label}") | ||
|
||
path = config.get_config_file_path() | ||
path = click.style(path, "blue") | ||
click.echo(f"\nAuth tokens are stored in: {path}") | ||
|
||
|
||
@cli.command() | ||
def env(): | ||
"""Print environment information for inclusion in bug reports.""" | ||
click.echo(f"toot {__version__}") | ||
click.echo(f"Python {sys.version}") | ||
click.echo(platform.platform()) | ||
|
||
|
||
@cli.command(name="login_cli") | ||
@click.option( | ||
"--instance", "-i", | ||
help="""Domain or base URL of the instance to log into, | ||
e.g. 'mastodon.social' or 'https://mastodon.social'""" | ||
) | ||
@click.option("--email", "-e", help="Email address to log in with") | ||
@pass_context | ||
def login_cli(ctx: Context, instance: str, email: str): | ||
""" | ||
Log into an instance from the console (not recommended) | ||
Does NOT support two factor authentication, may not work on instances | ||
other than Mastodon, mostly useful for scripting. | ||
""" | ||
if not instance: | ||
instance = click.prompt("Enter instance URL", default="https://mastodon.social") | ||
|
||
instance = instance.rstrip("/") | ||
base_url = instance if instance.startswith("http") else f"https://{instance}" | ||
|
||
app = create_app_interactive(base_url) | ||
login_interactive(app, email) | ||
|
||
click.echo() | ||
click.secho("✓ Successfully logged in.", fg="green") | ||
|
||
|
||
@cli.command() | ||
@pass_context | ||
def login(ctx: Context, args): | ||
"""Log into an instance using your browser (recommended)""" | ||
base_url = args_get_instance(args.instance, args.scheme) | ||
app = create_app_interactive(base_url) | ||
login_browser_interactive(app) | ||
|
||
click.echo() | ||
click.secho("✓ Successfully logged in.", fg="green") | ||
|
||
|
||
@cli.command() | ||
@pass_context | ||
def logout(ctx: Context, args): | ||
user = config.load_user(args.account, throw=True) | ||
config.delete_user(user) | ||
user_id = config.user_id(user) | ||
click.secho(f"✓ User {user_id} logged out", fg="green") | ||
|
||
|
||
@cli.command() | ||
@pass_context | ||
def activate(ctx: Context, args): | ||
if not args.account: | ||
click.echo("Specify one of the following user accounts to activate:\n") | ||
print_user_list(config.get_user_list()) | ||
return | ||
|
||
user = config.load_user(args.account, throw=True) | ||
config.activate_user(user) | ||
user_id = config.user_id(user) | ||
click.secho(f"✓ User {user_id} active", fg="green") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters