handle token_dir in all subcommands

This commit is contained in:
grym 2023-02-13 12:15:49 -05:00
parent 040f1717c6
commit 214e139163
1 changed files with 21 additions and 14 deletions

35
oxo.py
View File

@ -159,6 +159,22 @@ def main(
"""
def configure_token_dir(token_cache_dir: Path) -> Path:
if token_cache_dir:
token_dir = Path(token_cache_dir).expanduser().resolve()
if not token_dir.is_dir():
typer.secho(
f"token_dir={token_dir} is not a directory; cannot store tokens here.",
fg=typer.colors.RED,
err=True,
)
raise typer.Exit(-1)
token_dir.mkdir(exist_ok=True, parents=True)
else:
token_dir = None
return token_dir
@app.command()
def files(
files: t.List[Path] = typer.Argument(
@ -182,18 +198,7 @@ def files(
use.
"""
if token_cache_dir:
token_dir = Path(token_cache_dir).expanduser().resolve()
if not token_dir.is_dir():
typer.secho(
f"token_dir={token_dir} is not a directory; cannot store tokens here.",
fg=typer.colors.RED,
err=True,
)
raise typer.Exit(-1)
token_dir.mkdir(exist_ok=True, parents=True)
else:
token_dir = None
token_dir = configure_token_dir(token_cache_dir)
typer.echo(
post_files(
base_url,
@ -218,12 +223,13 @@ def repost(
token_cache_dir: str = TOKEN_CACHE_DIR,
):
"""Repost one or more urls."""
token_dir = configure_token_dir(token_cache_dir)
typer.echo(
post_repost(
base_url,
urls,
expires=expires,
token_dir=token_cache_dir,
token_dir=token_dir,
use_long_name=use_long_name,
)
)
@ -239,6 +245,7 @@ def shorten(
token_cache_dir: str = TOKEN_CACHE_DIR,
):
"""Shorten one or more urls."""
token_dir = configure_token_dir(token_cache_dir)
if base_url == BASE_URL.default:
typer.secho(
f"Warning: shortening is often disabled "
@ -246,7 +253,7 @@ def shorten(
fg=typer.colors.RED,
err=True,
)
typer.echo(post_shorten(base_url, urls, expires, token_cache_dir))
typer.echo(post_shorten(base_url, urls, expires, token_dir))
def did_confirm(token: TokenData, filename: Path) -> bool: