Avoid calling click commands from click commands
MODIFY
plex_trakt_sync/commands/login.py
plex_trakt_sync/commands/login.py
import click
-from plex_trakt_sync.commands.plex_login import has_plex_token, plex_login
-from plex_trakt_sync.commands.trakt_login import has_trakt_token, trakt_login
+from plex_trakt_sync.commands.plex_login import has_plex_token, plex_login_autoconfig
+from plex_trakt_sync.commands.trakt_login import has_trakt_token, trakt_login_autoconfig
def ensure_login():
if not has_plex_token():
- plex_login()
+ plex_login_autoconfig()
if not has_trakt_token():
- trakt_login()
+ trakt_login_autoconfig()
@click.command()

MODIFY
plex_trakt_sync/commands/plex_login.py
plex_trakt_sync/commands/plex_login.py
@@ -135,7 +135,13 @@ def has_plex_token():
return CONFIG["PLEX_TOKEN"] is not None
-@click.command()
+def plex_login_autoconfig():
+ username = environ.get("PLEX_USERNAME", CONFIG["PLEX_USERNAME"])
+ password = environ.get("PLEX_PASSWORD", None)
+ login(username, password)
+
+
+@click.command("plex-login")
@click.option("--username", help="Plex login", default=lambda: environ.get("PLEX_USERNAME", CONFIG["PLEX_USERNAME"]))
@click.option("--password", help="Plex password", default=lambda: environ.get("PLEX_PASSWORD", None))
def plex_login(username, password):
@@ -143,6 +149,10 @@ def plex_login(username, password):
Log in to Plex Account to obtain Access Token. Optionally can use managed user on servers that you own.
"""
+ login(username, password)
+
+
+def login(username: str, password: str):
if has_plex_token():
if not click.confirm(PROMPT_PLEX_RELOGIN, default=True):
return

MODIFY
plex_trakt_sync/commands/trakt_login.py
plex_trakt_sync/commands/trakt_login.py
@@ -48,12 +48,19 @@ def has_trakt_token():
return CONFIG["TRAKT_USERNAME"] is not None
+def trakt_login_autoconfig():
+ login()
+
+
@click.command()
def trakt_login():
"""
Log in to Trakt Account to obtain Access Token.
"""
+ login()
+
+def login():
api = factory.trakt_api()
trakt_authenticate(api)
user = api.me.username

Avoid calling click commands from click commands