Reorganize code and provide clear-credentials
This commit is contained in:
parent
5108ca59f4
commit
5128fb2b29
5 changed files with 38 additions and 14 deletions
0
cleantoots/commands/__init__.py
Normal file
0
cleantoots/commands/__init__.py
Normal 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")
|
|
@ -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")
|
||||||
|
|
|
@ -17,8 +17,9 @@ def _config_has_sections(config):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _open_url(url):
|
def _open_url(url, echo):
|
||||||
if _is_tty():
|
if _is_tty():
|
||||||
|
if echo:
|
||||||
click.echo(
|
click.echo(
|
||||||
"We will now open a browser for each account set in the config file."
|
"We will now open a browser for each account set in the config file."
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue