Reorganize code and provide clear-credentials

This commit is contained in:
Gabriel Augendre 2019-12-28 15:11:53 +01:00
parent 5108ca59f4
commit 5128fb2b29
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
5 changed files with 38 additions and 14 deletions

View file

View file

@ -13,8 +13,7 @@ from cleantoots.utils import (
@click.group("config")
@click.pass_obj
def config_command(config):
def config_command():
"""Manage cleantoot's config."""
pass
@ -88,6 +87,7 @@ def login(config, only_missing):
"""Fetch credentials for each app described in config file."""
if not _config_has_sections(config):
return
prompt = True
for section in config.sections():
section = config[section]
app_file_exists = config.isfile(section.get("app_secret_file"))
@ -102,8 +102,32 @@ def login(config, only_missing):
mastodon = Mastodon(client_id=config.file(section.get("app_secret_file")))
if not (only_missing and user_file_exists and app_file_exists):
_open_url(mastodon.auth_request_url())
_open_url(mastodon.auth_request_url(), echo=prompt)
prompt = False
code = click.prompt("Enter code for {}".format(section.get("api_base_url")))
mastodon.log_in(
code=code, to_file=config.file(section.get("user_secret_file"))
)
@config_command.command()
@click.confirmation_option(
prompt="Are you sure you want to delete all credential files? "
"You will need to run `cleantoots config login` to re-authenticate."
)
@click.pass_obj
def clear_credentials(config):
"""Delete all credential files described in config file."""
if not _config_has_sections(config):
return
for section_name in config.sections():
section = config[section_name]
try:
os.remove(config.file(section.get("app_secret_file")))
except FileNotFoundError:
pass
try:
os.remove(config.file(section.get("user_secret_file")))
except FileNotFoundError:
pass
click.secho("Removed files for {}".format(section_name), fg="green")

View file

@ -4,8 +4,7 @@ import pathlib
import click
from cleantoots import config as config_commands
from cleantoots import clean as clean_commands
from cleantoots.commands import clean as clean_commands, config as config_commands
HOME = pathlib.Path.home()
DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots")

View file

@ -17,16 +17,17 @@ def _config_has_sections(config):
return True
def _open_url(url):
def _open_url(url, echo):
if _is_tty():
click.echo(
"We will now open a browser for each account set in the config file."
)
click.echo(
"You'll need to authenticate and then copy the code provided in the web "
"page back into this terminal, upon prompt."
)
click.pause()
if echo:
click.echo(
"We will now open a browser for each account set in the config file."
)
click.echo(
"You'll need to authenticate and then copy the code provided in the web "
"page back into this terminal, upon prompt."
)
click.pause()
click.launch(url)
else:
click.echo("Go to {}, authenticate and enter the code below.".format(url))