Skip to content

Commit 1e1cf4d

Browse files
committed
bin/transfer-crates: Use clap for CLI argument parsing
1 parent 04e90ab commit 1e1cf4d

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/bin/transfer-crates.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Transfer all crates from one user to another.
2-
//
3-
// Usage:
4-
// cargo run --bin transfer-crates from-user to-user
5-
61
#![warn(clippy::all, rust_2018_idioms)]
72

83
use cargo_registry::{
@@ -11,13 +6,25 @@ use cargo_registry::{
116
schema::{crate_owners, crates, users},
127
};
138
use std::{
14-
env,
159
io::{self, prelude::*},
1610
process::exit,
1711
};
1812

13+
use clap::Clap;
1914
use diesel::prelude::*;
2015

16+
#[derive(Clap, Debug)]
17+
#[clap(
18+
name = "transfer-crates",
19+
about = "Transfer all crates from one user to another."
20+
)]
21+
struct Opts {
22+
/// GitHub login of the "from" user
23+
from_user: String,
24+
/// GitHub login of the "to" user
25+
to_user: String,
26+
}
27+
2128
fn main() {
2229
let conn = db::connect_now().unwrap();
2330
conn.transaction::<_, diesel::result::Error, _>(|| {
@@ -28,27 +35,15 @@ fn main() {
2835
}
2936

3037
fn transfer(conn: &PgConnection) {
31-
let from = match env::args().nth(1) {
32-
None => {
33-
println!("needs a from-user argument");
34-
return;
35-
}
36-
Some(s) => s,
37-
};
38-
let to = match env::args().nth(2) {
39-
None => {
40-
println!("needs a to-user argument");
41-
return;
42-
}
43-
Some(s) => s,
44-
};
38+
let opts: Opts = Opts::parse();
4539

4640
let from: User = users::table
47-
.filter(users::gh_login.eq(from))
41+
.filter(users::gh_login.eq(opts.from_user))
4842
.first(conn)
4943
.unwrap();
44+
5045
let to: User = users::table
51-
.filter(users::gh_login.eq(to))
46+
.filter(users::gh_login.eq(opts.to_user))
5247
.first(conn)
5348
.unwrap();
5449

0 commit comments

Comments
 (0)