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.group("config")
@click.pass_obj def config_command():
def config_command(config):
"""Manage cleantoot's config.""" """Manage cleantoot's config."""
pass pass
@ -88,6 +87,7 @@ def login(config, only_missing):
"""Fetch credentials for each app described in config file.""" """Fetch credentials for each app described in config file."""
if not _config_has_sections(config): if not _config_has_sections(config):
return return
prompt = True
for section in config.sections(): for section in config.sections():
section = config[section] section = config[section]
app_file_exists = config.isfile(section.get("app_secret_file")) 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"))) mastodon = Mastodon(client_id=config.file(section.get("app_secret_file")))
if not (only_missing and user_file_exists and app_file_exists): 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"))) code = click.prompt("Enter code for {}".format(section.get("api_base_url")))
mastodon.log_in( mastodon.log_in(
code=code, to_file=config.file(section.get("user_secret_file")) 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 import click
from cleantoots import config as config_commands from cleantoots.commands import clean as clean_commands, config as config_commands
from cleantoots import clean as clean_commands
HOME = pathlib.Path.home() HOME = pathlib.Path.home()
DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots") DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots")

View file

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