Skip to content

Commit 0cac0b2

Browse files
committed
admin/delete_crate: Add --deleted-by CLI option
1 parent abd3190 commit 0cac0b2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/bin/crates-admin/delete_crate.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::dialoguer;
22
use anyhow::Context;
33
use chrono::{NaiveDateTime, Utc};
44
use colored::Colorize;
5-
use crates_io::models::NewDeletedCrate;
5+
use crates_io::models::{NewDeletedCrate, User};
66
use crates_io::schema::{crate_downloads, deleted_crates};
77
use crates_io::worker::jobs;
88
use crates_io::{db, schema::crates};
@@ -29,6 +29,10 @@ pub struct Opts {
2929
/// Don't ask for confirmation: yes, we are sure. Best for scripting.
3030
#[arg(short, long)]
3131
yes: bool,
32+
33+
/// Your GitHub username.
34+
#[arg(long)]
35+
deleted_by: String,
3236
}
3337

3438
pub async fn run(opts: Opts) -> anyhow::Result<()> {
@@ -47,6 +51,10 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
4751
.await
4852
.context("Failed to look up crate name from the database")?;
4953

54+
let deleted_by = User::async_find_by_login(&mut conn, &opts.deleted_by)
55+
.await
56+
.context("Failed to look up `--deleted-by` user from the database")?;
57+
5058
println!("Deleting the following crates:");
5159
println!();
5260
for name in &crate_names {
@@ -73,6 +81,7 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
7381
let deleted_crate = NewDeletedCrate::builder(name)
7482
.created_at(&created_at)
7583
.deleted_at(&now)
84+
.deleted_by(deleted_by.id)
7685
.available_at(&now)
7786
.build();
7887

src/models/user.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ impl User {
5151
.first(conn)
5252
}
5353

54+
pub async fn async_find_by_login(
55+
conn: &mut AsyncPgConnection,
56+
login: &str,
57+
) -> QueryResult<User> {
58+
use diesel_async::RunQueryDsl;
59+
60+
users::table
61+
.filter(lower(users::gh_login).eq(login.to_lowercase()))
62+
.filter(users::gh_id.ne(-1))
63+
.order(users::gh_id.desc())
64+
.first(conn)
65+
.await
66+
}
67+
5468
pub fn owning(krate: &Crate, conn: &mut impl Conn) -> QueryResult<Vec<Owner>> {
5569
use diesel::RunQueryDsl;
5670

0 commit comments

Comments
 (0)