Use promptly instead of dialoguer (#1410)

See #1409

Co-authored-by: David James <davidcjames@gmail.com>
This commit is contained in:
David James
2021-08-30 17:10:53 -04:00
committed by GitHub
parent 0e51272b72
commit ad81e35f28
3 changed files with 97 additions and 26 deletions

View File

@@ -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"

View File

@@ -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;
@@ -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(());
}
@@ -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;
}
}
}
}