Skip to content

Use clap for CLI argument parsing #2993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 101 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ base64 = "0.13"
cargo-registry-s3 = { path = "src/s3", version = "0.2.0" }
chrono = { version = "0.4.0", features = ["serde"] }
civet = "0.12.0-alpha.4"
clap = "=3.0.0-beta.2"
comrak = { version = "0.8", default-features = false }

conduit = "0.9.0-alpha.3"
Expand All @@ -50,7 +51,6 @@ ctrlc = { version = "3.0", features = ["termination"] }
derive_deref = "1.1.1"
diesel = { version = "1.4.0", features = ["postgres", "serde_json", "chrono", "r2d2"] }
diesel_full_text_search = "1.0.0"
docopt = "1.0"
dotenv = "0.15"
env_logger = "0.8"
failure = "0.1.1"
Expand Down
37 changes: 16 additions & 21 deletions src/bin/delete-crate.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Purge all references to a crate from the database.
//
// Please be super sure you want to do this before running this.
//
// Usage:
// cargo run --bin delete-crate crate-name

#![warn(clippy::all, rust_2018_idioms)]

use cargo_registry::{db, models::Crate, schema::crates};
use std::{
env,
io::{self, prelude::*},
};
use std::io::{self, prelude::*};

use clap::Clap;
use diesel::prelude::*;

#[derive(Clap, Debug)]
#[clap(
name = "delete-crate",
about = "Purge all references to a crate from the database.\n\nPlease be super sure you want to do this before running this."
)]
struct Opts {
/// Name of the crate
crate_name: String,
}

fn main() {
let conn = db::connect_now().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|| {
Expand All @@ -25,18 +26,12 @@ fn main() {
}

fn delete(conn: &PgConnection) {
let name = match env::args().nth(1) {
None => {
println!("needs a crate-name argument");
return;
}
Some(s) => s,
};

let krate: Crate = Crate::by_name(&name).first(conn).unwrap();
let opts: Opts = Opts::parse();

let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
print!(
"Are you sure you want to delete {} ({}) [y/N]: ",
name, krate.id
opts.crate_name, krate.id
);
io::stdout().flush().unwrap();
let mut line = String::new();
Expand Down
46 changes: 18 additions & 28 deletions src/bin/delete-version.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
// Purge all references to a crate's version from the database.
//
// Please be super sure you want to do this before running this.
//
// Usage:
// cargo run --bin delete-version crate-name version-number

#![warn(clippy::all, rust_2018_idioms)]

use cargo_registry::{
db,
models::{Crate, Version},
schema::versions,
};
use std::{
env,
io::{self, prelude::*},
};
use std::io::{self, prelude::*};

use clap::Clap;
use diesel::prelude::*;

#[derive(Clap, Debug)]
#[clap(
name = "delete-version",
about = "Purge all references to a crate's version from the database.\n\nPlease be super sure you want to do this before running this."
)]
struct Opts {
/// Name of the crate
crate_name: String,
/// Version number that should be deleted
version: String,
}

fn main() {
let conn = db::connect_now().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|| {
Expand All @@ -29,29 +32,16 @@ fn main() {
}

fn delete(conn: &PgConnection) {
let name = match env::args().nth(1) {
None => {
println!("needs a crate-name argument");
return;
}
Some(s) => s,
};
let version = match env::args().nth(2) {
None => {
println!("needs a version argument");
return;
}
Some(s) => s,
};
let opts: Opts = Opts::parse();

let krate: Crate = Crate::by_name(&name).first(conn).unwrap();
let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
let v: Version = Version::belonging_to(&krate)
.filter(versions::num.eq(&version))
.filter(versions::num.eq(&opts.version))
.first(conn)
.unwrap();
print!(
"Are you sure you want to delete {}#{} ({}) [y/N]: ",
name, version, v.id
opts.crate_name, opts.version, v.id
);
io::stdout().flush().unwrap();
let mut line = String::new();
Expand Down
25 changes: 14 additions & 11 deletions src/bin/populate.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Populate a set of dummy download statistics for a specific version in the
// database.
//
// Usage:
// cargo run --bin populate version_id1 version_id2 ...

#![warn(clippy::all, rust_2018_idioms)]

use cargo_registry::{db, schema::version_downloads};
use std::env;

use clap::Clap;
use diesel::prelude::*;
use rand::{thread_rng, Rng};

#[derive(Clap, Debug)]
#[clap(
name = "populate",
about = "Populate a set of dummy download statistics for a specific version in the database."
)]
struct Opts {
#[clap(required = true)]
version_ids: Vec<i32>,
}

fn main() {
let conn = db::connect_now().unwrap();
conn.transaction(|| update(&conn)).unwrap();
Expand All @@ -20,10 +24,9 @@ fn main() {
fn update(conn: &PgConnection) -> QueryResult<()> {
use diesel::dsl::*;

let ids = env::args()
.skip(1)
.filter_map(|arg| arg.parse::<i32>().ok());
for id in ids {
let opts: Opts = Opts::parse();

for id in opts.version_ids {
let mut rng = thread_rng();
let mut dls = rng.gen_range(5_000i32, 10_000);

Expand Down
Loading