Compare commits

...

2 Commits

Author SHA1 Message Date
grym f78bc64e98 Rename `manage` to `delete` 2022-12-14 18:45:27 -05:00
grym 882972a92e Delete stale tokens, add `manage` subcommand. 2022-12-14 18:15:58 -05:00
3 changed files with 47 additions and 15 deletions

View File

@ -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

40
oxo.py
View File

@ -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,32 +178,41 @@ 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 = []
for t in token_files:
try:
tokens.append(TokenData(**json.loads(t.read_text())))
good_tokens.append(t)
except Exception as e:
typer.secho(f"Ignoring {t}: {e}", err=True, fg=typer.colors.YELLOW)
with httpx.Client() as client:
for t in tokens:
if delete:
try:
for token, token_file in zip(tokens, good_tokens):
try:
if (interactive and did_confirm(token)) or not interactive:
res = client.post(
base_url, files=dict(token=(None, t.token), delete=(None, ""))
token.oxo_url, data=dict(token=token.token, delete="")
)
res.raise_for_status()
typer.echo(res.text.strip())
t.unlink()
typer.echo(f"Removed stale token {t}")
except Exception as e:
typer.secho(e, err=True, fg=typer.colors.RED)
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)

View File

@ -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]