cleantoots/cleantoots/commands/config.py

139 lines
4.3 KiB
Python
Raw Normal View History

2019-12-28 10:42:02 +01:00
import os
import sys
import click
from mastodon import Mastodon
2019-12-28 13:31:34 +01:00
from cleantoots.utils import (
2021-12-19 14:21:43 +01:00
CleanTootsConfig,
2019-12-28 13:31:34 +01:00
_config_has_sections,
_get_default_config,
_is_tty,
2021-12-19 14:21:43 +01:00
_open_url,
2019-12-28 13:31:34 +01:00
)
2019-12-28 10:42:02 +01:00
2019-12-28 13:31:34 +01:00
@click.group("config")
def config_command():
2019-12-28 10:42:02 +01:00
"""Manage cleantoot's config."""
pass
2019-12-28 13:31:34 +01:00
@config_command.command()
2019-12-28 10:42:02 +01:00
@click.pass_obj
def setup(config: CleanTootsConfig):
2019-12-28 10:42:02 +01:00
"""Initial setup for configuration directories and files."""
if os.path.isfile(config.main_file):
2021-12-19 14:21:43 +01:00
click.secho(f"{config.main_file} found. Not touching anything.", fg="yellow")
2019-12-28 10:42:02 +01:00
command = click.style("cleantoots config edit", bold=True)
2021-12-19 14:21:43 +01:00
click.echo(f"You may want to edit the file. Use: {command}.")
2019-12-28 10:42:02 +01:00
return
2019-12-28 13:31:34 +01:00
default_config = _get_default_config()
2019-12-28 10:42:02 +01:00
with open(config.main_file, "w") as _file:
default_config.write(_file)
2021-12-19 14:21:43 +01:00
click.secho(f"{config.main_file} written.", fg="green")
2019-12-28 10:42:02 +01:00
click.echo()
click.secho("Next steps", bold=True)
click.echo(
"You'll need to edit the config file in order to set some settings such as:"
)
click.echo("* The base URL of your Mastodon instance")
click.echo("* The toots you want to protect")
2019-12-28 13:31:34 +01:00
if _is_tty():
2019-12-28 10:42:02 +01:00
click.echo()
click.secho("We're going to open the file for you now.")
click.pause()
click.edit(filename=config.main_file)
2019-12-28 13:31:34 +01:00
@config_command.command(name="list")
2019-12-28 10:42:02 +01:00
@click.pass_obj
def list_(config: CleanTootsConfig):
2019-12-28 10:42:02 +01:00
"""Display parsed config."""
2019-12-28 13:31:34 +01:00
if not _config_has_sections(config):
2019-12-28 10:42:02 +01:00
return
for section_name in config.sections():
click.secho(section_name, bold=True)
section = config[section_name]
for key, value in section.items():
2021-12-19 14:21:43 +01:00
click.secho(f"{key} = {value}")
2019-12-28 10:42:02 +01:00
click.echo()
2019-12-28 13:31:34 +01:00
@config_command.command()
2019-12-28 10:42:02 +01:00
@click.pass_obj
def edit(config: CleanTootsConfig):
2019-12-28 10:42:02 +01:00
"""Edit config file."""
2019-12-28 13:31:34 +01:00
if not _config_has_sections(config):
click.pause()
2019-12-28 10:42:02 +01:00
if sys.stdout.isatty() and sys.stdin.isatty():
click.edit(filename=config.main_file)
else:
click.secho("Not running in a terminal, can't open file.", fg="red")
2019-12-30 20:29:39 +01:00
@config_command.command()
@click.pass_obj
def path(config: CleanTootsConfig):
"""Print config path and exit."""
click.echo(config.dir)
2019-12-28 13:31:34 +01:00
@config_command.command()
@click.option(
"-m",
"--only-missing",
help="Prompt for login only on accounts that miss a credentials file.",
is_flag=True,
)
2019-12-28 10:42:02 +01:00
@click.pass_obj
def login(config: CleanTootsConfig, only_missing: bool):
2019-12-28 10:42:02 +01:00
"""Fetch credentials for each app described in config file."""
2019-12-28 13:31:34 +01:00
if not _config_has_sections(config):
return
prompt = True
2019-12-28 10:42:02 +01:00
for section in config.sections():
section = config[section]
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")),
)
2019-12-28 10:42:02 +01:00
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(), 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: CleanTootsConfig):
"""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
2021-12-19 14:21:43 +01:00
click.secho(f"Removed files for {section_name}", fg="green")