From 5108ca59f490f5f8020fb00f4fc6bcf16701cfee Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 28 Dec 2019 14:56:14 +0100 Subject: [PATCH] Allow requiring login only for accounts that miss files --- README.md | 29 +++++++++++++++++++++++++---- cleantoots/config.py | 32 +++++++++++++++++++++++--------- cleantoots/main.py | 3 +++ pyproject.toml | 2 +- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 47301c0..e4330d2 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,29 @@ Cleantoots helps you delete your old toots. Because not everything we say on social medias should stay there for eternity. -## Initial config - +## Config +### Initial setup Only once ```bash python -m pip install cleantoots -cleantoots setup-config # See the following section for config options -cleantoots login +cleantoots config setup # See the following section for config file options +cleantoots config login ``` +### View and edit +You can later view the parsed config file with +```bash +cleantoots config list +``` + +You can edit the config file using +```bash +cleantoots config edit +``` + +This will open the config file using your preferred editor (`EDITOR` env variable). + ## Config options ```ini @@ -68,6 +81,14 @@ cleantoots clean # Defaults to a dry run. Does NOT delete. cleantoots clean --delete # Delete without prompt. ``` +## Add an account +```bash +cleantoots config edit # Opens editor so you can add your config +cleantoots config list # Check your newly added account +cleantoots config login --only-missing # Store credentials for your newly created account +cleantoots clean --delete +``` + ## Tested environments Cleantoots test suite runs on Python 3.6, 3.7 and 3.8 on latest versions of macOS, Windows and Ubuntu as GitHub Actions understands it. diff --git a/cleantoots/config.py b/cleantoots/config.py index 8384bb4..0fed664 100644 --- a/cleantoots/config.py +++ b/cleantoots/config.py @@ -77,19 +77,33 @@ def edit(config): @config_command.command() +@click.option( + "-m", + "--only-missing", + help="Prompt for login only on accounts that miss a credentials file.", + is_flag=True, +) @click.pass_obj -def login(config): +def login(config, only_missing): """Fetch credentials for each app described in config file.""" if not _config_has_sections(config): return for section in config.sections(): section = config[section] - Mastodon.create_app( - "cleantoots", - api_base_url=section.get("api_base_url"), - to_file=config.file(section.get("app_secret_file")), - ) + app_file_exists = config.isfile(section.get("app_secret_file")) + user_file_exists = config.isfile(section.get("user_secret_file")) + + if not (only_missing and app_file_exists): + Mastodon.create_app( + "cleantoots", + api_base_url=section.get("api_base_url"), + to_file=config.file(section.get("app_secret_file")), + ) + mastodon = Mastodon(client_id=config.file(section.get("app_secret_file"))) - _open_url(mastodon.auth_request_url()) - 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"))) + if not (only_missing and user_file_exists and app_file_exists): + _open_url(mastodon.auth_request_url()) + 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")) + ) diff --git a/cleantoots/main.py b/cleantoots/main.py index 74959b7..0e5259e 100644 --- a/cleantoots/main.py +++ b/cleantoots/main.py @@ -25,6 +25,9 @@ class CleanTootsConfig(configparser.ConfigParser): def file(self, filename): return os.path.join(self.dir, filename) + def isfile(self, filename): + return os.path.isfile(self.file(filename)) + @click.group(context_settings=CONTEXT_SETTINGS) @click.option( diff --git a/pyproject.toml b/pyproject.toml index 05d74eb..50cdec7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cleantoots" -version = "0.3.1" +version = "0.3.2" description = "Cleanup your toot history." license = "GPL-3.0-or-later" authors = ["Gabriel Augendre "]