Skip to content

Commit

Permalink
Use promptly instead of dialoguer (#1410)
Browse files Browse the repository at this point in the history
See #1409

Co-authored-by: David James <davidcjames@gmail.com>
  • Loading branch information
xpe and David James authored Aug 30, 2021
1 parent 0e51272 commit ad81e35
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 26 deletions.
83 changes: 69 additions & 14 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 sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ anyhow = "1.0"
url = { version = "2.1.1", default-features = false }
async-trait = "0.1.30"
console = "0.14.1"
dialoguer = "0.8.0"
promptly = "0.3.0"
serde_json = "1.0.53"
serde = { version = "1.0.110", features = ["derive"] }
glob = "0.3.0"
Expand Down
38 changes: 27 additions & 11 deletions sqlx-cli/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::migrate;
use console::style;
use dialoguer::Confirm;
use promptly::{prompt, ReadlineError};
use sqlx::any::Any;
use sqlx::migrate::MigrateDatabase;

Expand All @@ -13,16 +13,7 @@ pub async fn create(uri: &str) -> anyhow::Result<()> {
}

pub async fn drop(uri: &str, confirm: bool) -> anyhow::Result<()> {
if confirm
&& !Confirm::new()
.with_prompt(format!(
"\nAre you sure you want to drop the database at {}?",
style(uri).cyan()
))
.wait_for_newline(true)
.default(false)
.interact()?
{
if confirm && !ask_to_continue(uri) {
return Ok(());
}

Expand All @@ -42,3 +33,28 @@ pub async fn setup(migration_source: &str, uri: &str) -> anyhow::Result<()> {
create(uri).await?;
migrate::run(migration_source, uri, false, false).await
}

fn ask_to_continue(uri: &str) -> bool {
loop {
let r: Result<String, ReadlineError> =
prompt(format!("Drop database at {}? (y/n)", style(uri).cyan()));
match r {
Ok(response) => {
if response == "n" || response == "N" {
return false;
} else if response == "y" || response == "Y" {
return true;
} else {
println!(
"Response not recognized: {}\nPlease type 'y' or 'n' and press enter.",
response
);
}
}
Err(e) => {
println!("{}", e);
return false;
}
}
}
}

0 comments on commit ad81e35

Please sign in to comment.