Skip to content

Commit c26351b

Browse files
committed
Auto merge of #2996 - Turbo87:dialoguer, r=jtgeibel
Use `dialoguer` crate for command line confirmations This simplifies our command line confirmation code in the admin tools by using https://docs.rs/dialoguer. r? `@jtgeibel`
2 parents 6439f3d + 7d99a6c commit c26351b

File tree

6 files changed

+105
-36
lines changed

6 files changed

+105
-36
lines changed

Cargo.lock

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ conduit-static = "0.9.0-alpha.3"
4949
cookie = { version = "0.14", features = ["secure"] }
5050
ctrlc = { version = "3.0", features = ["termination"] }
5151
derive_deref = "1.1.1"
52+
dialoguer = "0.7.1"
5253
diesel = { version = "1.4.0", features = ["postgres", "serde_json", "chrono", "r2d2"] }
5354
diesel_full_text_search = "1.0.0"
5455
dotenv = "0.15"

src/bin/delete-crate.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

33
use cargo_registry::{db, models::Crate, schema::crates};
4-
use std::io::{self, prelude::*};
54

65
use clap::Clap;
76
use diesel::prelude::*;
87

8+
mod dialoguer;
9+
910
#[derive(Clap, Debug)]
1011
#[clap(
1112
name = "delete-crate",
@@ -29,14 +30,12 @@ fn delete(conn: &PgConnection) {
2930
let opts: Opts = Opts::parse();
3031

3132
let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
32-
print!(
33-
"Are you sure you want to delete {} ({}) [y/N]: ",
33+
34+
let prompt = format!(
35+
"Are you sure you want to delete {} ({})?",
3436
opts.crate_name, krate.id
3537
);
36-
io::stdout().flush().unwrap();
37-
let mut line = String::new();
38-
io::stdin().read_line(&mut line).unwrap();
39-
if !line.starts_with('y') {
38+
if !dialoguer::confirm(&prompt) {
4039
return;
4140
}
4241

@@ -46,11 +45,7 @@ fn delete(conn: &PgConnection) {
4645
.unwrap();
4746
println!(" {} deleted", n);
4847

49-
print!("commit? [y/N]: ");
50-
io::stdout().flush().unwrap();
51-
let mut line = String::new();
52-
io::stdin().read_line(&mut line).unwrap();
53-
if !line.starts_with('y') {
48+
if !dialoguer::confirm("commit?") {
5449
panic!("aborting transaction");
5550
}
5651
}

src/bin/delete-version.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use cargo_registry::{
55
models::{Crate, Version},
66
schema::versions,
77
};
8-
use std::io::{self, prelude::*};
98

109
use clap::Clap;
1110
use diesel::prelude::*;
1211

12+
mod dialoguer;
13+
1314
#[derive(Clap, Debug)]
1415
#[clap(
1516
name = "delete-version",
@@ -39,14 +40,12 @@ fn delete(conn: &PgConnection) {
3940
.filter(versions::num.eq(&opts.version))
4041
.first(conn)
4142
.unwrap();
42-
print!(
43-
"Are you sure you want to delete {}#{} ({}) [y/N]: ",
43+
44+
let prompt = format!(
45+
"Are you sure you want to delete {}#{} ({})?",
4446
opts.crate_name, opts.version, v.id
4547
);
46-
io::stdout().flush().unwrap();
47-
let mut line = String::new();
48-
io::stdin().read_line(&mut line).unwrap();
49-
if !line.starts_with('y') {
48+
if !dialoguer::confirm(&prompt) {
5049
return;
5150
}
5251

@@ -55,11 +54,7 @@ fn delete(conn: &PgConnection) {
5554
.execute(conn)
5655
.unwrap();
5756

58-
print!("commit? [y/N]: ");
59-
io::stdout().flush().unwrap();
60-
let mut line = String::new();
61-
io::stdin().read_line(&mut line).unwrap();
62-
if !line.starts_with('y') {
57+
if !dialoguer::confirm("commit?") {
6358
panic!("aborting transaction");
6459
}
6560
}

src/bin/dialoguer/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use ::dialoguer::{theme::Theme, Confirm};
2+
3+
pub fn confirm(msg: &str) -> bool {
4+
Confirm::with_theme(&CustomTheme)
5+
.with_prompt(msg)
6+
.default(false)
7+
.wait_for_newline(true)
8+
.interact()
9+
.unwrap()
10+
}
11+
12+
#[derive(Debug, Copy, Clone)]
13+
pub struct CustomTheme;
14+
15+
impl Theme for CustomTheme {
16+
fn format_confirm_prompt(
17+
&self,
18+
f: &mut dyn std::fmt::Write,
19+
prompt: &str,
20+
default: Option<bool>,
21+
) -> std::fmt::Result {
22+
if !prompt.is_empty() {
23+
write!(f, "{} ", &prompt)?;
24+
}
25+
match default {
26+
None => write!(f, "[y/n] ")?,
27+
Some(true) => write!(f, "[Y/n] yes")?,
28+
Some(false) => write!(f, "[y/N] no")?,
29+
}
30+
Ok(())
31+
}
32+
}

src/bin/transfer-crates.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ use cargo_registry::{
55
models::{Crate, OwnerKind, User},
66
schema::{crate_owners, crates, users},
77
};
8-
use std::{
9-
io::{self, prelude::*},
10-
process::exit,
11-
};
8+
use std::process::exit;
129

1310
use clap::Clap;
1411
use diesel::prelude::*;
1512

13+
mod dialoguer;
14+
1615
#[derive(Clap, Debug)]
1716
#[clap(
1817
name = "transfer-crates",
@@ -59,11 +58,11 @@ fn transfer(conn: &PgConnection) {
5958
get_confirm("continue?");
6059
}
6160

62-
println!(
63-
"Are you sure you want to transfer crates from {} to {}",
61+
let prompt = format!(
62+
"Are you sure you want to transfer crates from {} to {}?",
6463
from.gh_login, to.gh_login
6564
);
66-
get_confirm("continue");
65+
get_confirm(&prompt);
6766

6867
let crate_owners = crate_owners::table
6968
.filter(crate_owners::owner_id.eq(from.id))
@@ -89,11 +88,7 @@ fn transfer(conn: &PgConnection) {
8988
}
9089

9190
fn get_confirm(msg: &str) {
92-
print!("{} [y/N]: ", msg);
93-
io::stdout().flush().unwrap();
94-
let mut line = String::new();
95-
io::stdin().read_line(&mut line).unwrap();
96-
if !line.starts_with('y') {
91+
if !dialoguer::confirm(msg) {
9792
exit(0);
9893
}
9994
}

0 commit comments

Comments
 (0)