|
| 1 | +use anyhow::{Result, bail}; |
| 2 | +use clap::builder::ArgAction; |
| 3 | +use crates_io::db; |
| 4 | +use crates_io::schema::crates; |
| 5 | +use crates_io::worker::jobs; |
| 6 | +use crates_io_worker::BackgroundJob; |
| 7 | +use diesel::dsl::exists; |
| 8 | +use diesel::prelude::*; |
| 9 | +use diesel_async::RunQueryDsl; |
| 10 | + |
| 11 | +#[derive(clap::Parser, Debug)] |
| 12 | +#[command( |
| 13 | + name = "sync-index", |
| 14 | + about = "Synchronize crate index data to git and sparse indexes" |
| 15 | +)] |
| 16 | +pub struct Opts { |
| 17 | + /// Name of the crate to synchronize |
| 18 | + name: String, |
| 19 | + |
| 20 | + /// Skip syncing to the git index |
| 21 | + #[arg(long = "no-git", action = ArgAction::SetFalse)] |
| 22 | + git: bool, |
| 23 | + |
| 24 | + /// Skip syncing to the sparse index |
| 25 | + #[arg(long = "no-sparse", action = ArgAction::SetFalse)] |
| 26 | + sparse: bool, |
| 27 | +} |
| 28 | + |
| 29 | +pub async fn run(opts: Opts) -> Result<()> { |
| 30 | + let mut conn = db::oneoff_connection().await?; |
| 31 | + |
| 32 | + let query = crates::table.filter(crates::name.eq(&opts.name)); |
| 33 | + let crate_exists: bool = diesel::select(exists(query)).get_result(&mut conn).await?; |
| 34 | + if !crate_exists { |
| 35 | + bail!("Crate `{}` does not exist", opts.name); |
| 36 | + } |
| 37 | + |
| 38 | + if opts.git { |
| 39 | + println!("Enqueueing SyncToGitIndex job for `{}`", opts.name); |
| 40 | + jobs::SyncToGitIndex::new(&opts.name) |
| 41 | + .enqueue(&mut conn) |
| 42 | + .await?; |
| 43 | + } |
| 44 | + |
| 45 | + if opts.sparse { |
| 46 | + println!("Enqueueing SyncToSparseIndex job for `{}`", opts.name); |
| 47 | + jobs::SyncToSparseIndex::new(&opts.name) |
| 48 | + .enqueue(&mut conn) |
| 49 | + .await?; |
| 50 | + } |
| 51 | + |
| 52 | + Ok(()) |
| 53 | +} |
0 commit comments