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 7, 2025
1 parent 6b33766 commit 8bcfc67
Show file tree
Hide file tree
Showing 4 changed files with 69 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
56 changes: 33 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;
use tokio::{select, signal, 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,26 +60,34 @@ 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 {
let ctrlc_fut = signal::ctrl_c();
let decision_fut = 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()
});

let restore_cursor = || Term::stderr().show_cursor().unwrap();

select! {
biased;
_ = ctrlc_fut => {
restore_cursor();
false
},
decision = decision_fut => {
match decision.expect("Confirm thread panicked") {
Ok(decision) => decision,
Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
// Sometimes CTRL + C causes this error to be returned
restore_cursor();
false
},
Err(err) => panic!("Confirm dialog failed with {err}"),
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use futures::{Future, TryFutureExt};

use sqlx::{AnyConnection, Connection};
use tokio::{select, signal};

use crate::opt::{Command, ConnectOpts, DatabaseCommand, MigrateCommand};

Expand All @@ -21,6 +22,21 @@ mod prepare;
pub use crate::opt::Opt;

pub async fn run(opt: Opt) -> Result<()> {
let ctrlc_fut = signal::ctrl_c();
let do_run_fut = do_run(opt);

select! {
biased;
_ = ctrlc_fut => {
Ok(())
},
do_run_outcome = do_run_fut => {
do_run_outcome
}
}
}

async fn do_run(opt: Opt) -> Result<()> {
match opt.command {
Command::Migrate(migrate) => match migrate.command {
MigrateCommand::Add {
Expand Down

0 comments on commit 8bcfc67

Please sign in to comment.