diff --git a/README.org b/README.org index c3a9aad..89e1e85 100644 --- a/README.org +++ b/README.org @@ -101,6 +101,27 @@ oxo shorten --help │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ +#+end_example +#+begin_src bash :results output replace :tangle no +oxo delete --help +#+end_src + +#+RESULTS: +#+begin_example + + Usage: oxo delete [OPTIONS] [TOKEN_CACHE_DIR] + + Delete uploaded files if tokens are found on disk at `token_cache_dir`. + +╭─ Arguments ──────────────────────────────────────────────────────────────────╮ +│ token_cache_dir [TOKEN_CACHE_DIR] [env var: OXO_TOKEN_CACHE_DIR] │ +╰──────────────────────────────────────────────────────────────────────────────╯ +╭─ Options ────────────────────────────────────────────────────────────────────╮ +│ --interactive --no-interactive If True, prompt for each token found. │ +│ [default: no-interactive] │ +│ --help Show this message and exit. │ +╰──────────────────────────────────────────────────────────────────────────────╯ + #+end_example ** Installation diff --git a/oxo.py b/oxo.py index 230b43a..cde32af 100644 --- a/oxo.py +++ b/oxo.py @@ -5,6 +5,7 @@ from dataclasses import asdict, dataclass from pathlib import Path import httpx +import inquirer import typer from rich.console import Console @@ -177,15 +178,22 @@ def shorten(urls: t.List[str] = typer.Argument(..., min=1), base_url=BASE_URL): typer.echo(post_shorten(base_url, urls)) +def did_confirm(token: TokenData) -> bool: + confirm = inquirer.Confirm( + "delete", message=f"delete {token.oxo_url}?", default=True + ) + res = inquirer.prompt([confirm]) + return res["delete"] + + @app.command() -def manage( +def delete( token_cache_dir: Path = typer.Argument("", envvar="OXO_TOKEN_CACHE_DIR"), - # interactive: bool = typer.Option( - # False, help="If True, prompt for each token found." - # ), - delete: bool = typer.Option(True, help="If True, send delete requests."), - base_url=BASE_URL, + interactive: bool = typer.Option( + False, help="If True, prompt for each token found." + ), ): + """Delete uploaded files if tokens are found on disk at `token_cache_dir`.""" token_files = token_cache_dir.glob("*.token") tokens = [] good_tokens = [] @@ -197,14 +205,14 @@ def manage( typer.secho(f"Ignoring {t}: {e}", err=True, fg=typer.colors.YELLOW) with httpx.Client() as client: for token, token_file in zip(tokens, good_tokens): - if delete: - try: + try: + if (interactive and did_confirm(token)) or not interactive: res = client.post( token.oxo_url, data=dict(token=token.token, delete="") ) res.raise_for_status() - typer.echo(res.text.strip()) + typer.echo(f"Removed {token.oxo_url} {res.text.strip()}") token_file.unlink() typer.echo(f"Removed stale token {token_file}") - except Exception as e: - typer.secho(e, err=True, fg=typer.colors.RED) + except Exception as e: + typer.secho(e, err=True, fg=typer.colors.RED) diff --git a/pyproject.toml b/pyproject.toml index 386090d..26e8b35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ dynamic = ["version"] authors=[{name="grym", email="grym@ctrl-c.club"}] dependencies = ["typer[all]", "httpx", + "inquirer", 'importlib-metadata; python_version<"3.8"'] [project.optional-dependencies]