Skip to content

Commit

Permalink
refactor(cli): replace promptly with dialoguer
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Jan 11, 2025
1 parent 6b33766 commit 62112e5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 165 deletions.
158 changes: 18 additions & 140 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ path = "src/bin/cargo-sqlx.rs"

[dependencies]
dotenvy = "0.15.0"
tokio = { version = "1.15.0", features = ["macros", "rt", "rt-multi-thread"] }
tokio = { version = "1.15.0", features = ["macros", "rt", "rt-multi-thread", "signal"] }
sqlx = { workspace = true, default-features = false, features = [
"runtime-tokio",
"migrate",
Expand All @@ -39,7 +39,7 @@ chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
anyhow = "1.0.52"
async-trait = "0.1.52"
console = "0.15.0"
promptly = "0.3.0"
dialoguer = { version = "0.11", default-features = false }
serde_json = "1.0.73"
glob = "0.3.0"
openssl = { version = "0.10.38", optional = true }
Expand Down
65 changes: 42 additions & 23 deletions sqlx-cli/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::migrate;
use crate::opt::ConnectOpts;
use console::style;
use promptly::{prompt, ReadlineError};
use console::{style, Term};
use dialoguer::Confirm;
use sqlx::any::Any;
use sqlx::migrate::MigrateDatabase;
use std::{io, mem};
use tokio::task;

pub async fn create(connect_opts: &ConnectOpts) -> anyhow::Result<()> {
// NOTE: only retry the idempotent action.
Expand All @@ -24,7 +26,7 @@ pub async fn create(connect_opts: &ConnectOpts) -> anyhow::Result<()> {
}

pub async fn drop(connect_opts: &ConnectOpts, confirm: bool, force: bool) -> anyhow::Result<()> {
if confirm && !ask_to_continue_drop(connect_opts.required_db_url()?) {
if confirm && !ask_to_continue_drop(connect_opts.required_db_url()?.to_owned()).await {
return Ok(());
}

Expand Down Expand Up @@ -58,27 +60,44 @@ pub async fn setup(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow
migrate::run(migration_source, connect_opts, false, false, None).await
}

fn ask_to_continue_drop(db_url: &str) -> bool {
loop {
let r: Result<String, ReadlineError> =
prompt(format!("Drop database at {}? (y/n)", style(db_url).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;
async fn ask_to_continue_drop(db_url: String) -> bool {
struct RestoreCursorGuard {
disarmed: bool,
}

impl Drop for RestoreCursorGuard {
fn drop(&mut self) {
if !self.disarmed {
Term::stderr().show_cursor().unwrap()
}
}
}

let mut guard = RestoreCursorGuard { disarmed: false };

let decision_result = task::spawn_blocking(move || {
Confirm::new()
.with_prompt(format!("Drop database at {}?", style(&db_url).cyan()))
.wait_for_newline(true)
.default(false)
.show_default(true)
.interact()
})
.await
.expect("Confirm thread panicked");
match decision_result {
Ok(decision) => {
guard.disarmed = true;
decision
}
Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
// Sometimes CTRL + C causes this error to be returned
mem::drop(guard);
false
}
Err(err) => {
mem::drop(guard);
panic!("Confirm dialog failed with {err}")
}
}
}
Loading

0 comments on commit 62112e5

Please sign in to comment.