From 5128fb2b29b0bc954dc32bf5789324d257659766 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 28 Dec 2019 15:11:53 +0100 Subject: [PATCH] Reorganize code and provide clear-credentials --- cleantoots/commands/__init__.py | 0 cleantoots/{ => commands}/clean.py | 0 cleantoots/{ => commands}/config.py | 30 ++++++++++++++++++++++++++--- cleantoots/main.py | 3 +-- cleantoots/utils.py | 19 +++++++++--------- 5 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 cleantoots/commands/__init__.py rename cleantoots/{ => commands}/clean.py (100%) rename cleantoots/{ => commands}/config.py (78%) diff --git a/cleantoots/commands/__init__.py b/cleantoots/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cleantoots/clean.py b/cleantoots/commands/clean.py similarity index 100% rename from cleantoots/clean.py rename to cleantoots/commands/clean.py diff --git a/cleantoots/config.py b/cleantoots/commands/config.py similarity index 78% rename from cleantoots/config.py rename to cleantoots/commands/config.py index 0fed664..d4ad55d 100644 --- a/cleantoots/config.py +++ b/cleantoots/commands/config.py @@ -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") diff --git a/cleantoots/main.py b/cleantoots/main.py index 0e5259e..e2d2fa7 100644 --- a/cleantoots/main.py +++ b/cleantoots/main.py @@ -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") diff --git a/cleantoots/utils.py b/cleantoots/utils.py index 275aa02..f7cf214 100644 --- a/cleantoots/utils.py +++ b/cleantoots/utils.py @@ -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))