Skip to content

Commit

Permalink
Add paste remove to the CLI
Browse files Browse the repository at this point in the history
Enable admins to remove pastes without having to access the DB directly.
  • Loading branch information
tucked committed May 4, 2023
1 parent dce3204 commit 5f4d029
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pbnh/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ def info(ctx: click.Context, show_data: bool, hashids: tuple[str]) -> None:
+ [("data", paste["data"] if show_data else f"({len(value)} bytes)")]
):
click.echo(f"{column + ':':>15} {value}")


@paste.command() # type: ignore
@click.argument("hashids", type=str, nargs=-1)
@click.pass_context
def remove(ctx: click.Context, hashids: tuple[str]) -> None:
"""Remove pastes."""
for hashid in hashids:
removed = ctx.obj.data["paster"].delete(hashid=hashid)
message = "removed" if removed else "not found"
click.echo(f"{hashid} {message}")
4 changes: 3 additions & 1 deletion pbnh/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ def query(self, *, hashid: str) -> dict[str, Any] | None:
}
return None

def delete(self, *, hashid: str) -> None:
def delete(self, *, hashid: str) -> bool:
with self._session.begin():
result = self._query(hashid=hashid)
if result:
self._session.delete(result)
return True
return False


def _get_engine() -> Engine:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ def test_cli_paste_info_hash_mismatch(test_cli_runner, monkeypatch):
result = test_cli_runner.invoke(args=["paste", "info", hashid])
assert hashid in result.output
assert "WARNING" in result.output


def test_cli_paste_remove(app, test_cli_runner):
"""Pastes can be removed from the cLI."""
hashid = "abc123"
with app.app_context():
result = test_cli_runner.invoke(args=["paste", "remove", hashid])
assert hashid in result.output
2 changes: 1 addition & 1 deletion tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ def test_delete(paster):

def test_delete_nonexistent(paster):
with paster as p:
assert p.delete(hashid="nonexistent") is None
assert not p.delete(hashid="nonexistent")

0 comments on commit 5f4d029

Please sign in to comment.