From 1af9748530108cc125ba766625c22d12c9baf9ce Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 1 Dec 2025 15:19:42 +0900 Subject: [PATCH] client: add `--fail` flag for `show-*` commands --- src/client/commands/show_db.rs | 10 ++++++++++ src/client/commands/show_privs.rs | 10 ++++++++++ src/client/commands/show_user.rs | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/client/commands/show_db.rs b/src/client/commands/show_db.rs index eb7f87a..d497f83 100644 --- a/src/client/commands/show_db.rs +++ b/src/client/commands/show_db.rs @@ -20,6 +20,10 @@ pub struct ShowDbArgs { /// Print the information as JSON #[arg(short, long)] json: bool, + + /// Return a non-zero exit code if any of the results were erroneous + #[arg(short, long)] + fail: bool, } pub async fn show_databases( @@ -36,12 +40,14 @@ pub async fn show_databases( // TODO: collect errors for json output. + let mut contained_errors = false; let database_list = match server_connection.next().await { Some(Ok(Response::ListDatabases(databases))) => databases .into_iter() .filter_map(|(database_name, result)| match result { Ok(database_row) => Some(database_row), Err(err) => { + contained_errors = true; eprintln!("{}", err.to_error_message(&database_name)); eprintln!("Skipping..."); println!(); @@ -76,5 +82,9 @@ pub async fn show_databases( table.printstd(); } + if args.fail && contained_errors { + std::process::exit(1); + } + Ok(()) } diff --git a/src/client/commands/show_privs.rs b/src/client/commands/show_privs.rs index 36c748b..104c613 100644 --- a/src/client/commands/show_privs.rs +++ b/src/client/commands/show_privs.rs @@ -22,6 +22,10 @@ pub struct ShowPrivsArgs { /// Print the information as JSON #[arg(short, long)] json: bool, + + /// Return a non-zero exit code if any of the results were erroneous + #[arg(short, long)] + fail: bool, } pub async fn show_database_privileges( @@ -35,12 +39,14 @@ pub async fn show_database_privileges( }; server_connection.send(message).await?; + let mut contained_errors = false; let privilege_data = match server_connection.next().await { Some(Ok(Response::ListPrivileges(databases))) => databases .into_iter() .filter_map(|(database_name, result)| match result { Ok(privileges) => Some(privileges), Err(err) => { + contained_errors = true; eprintln!("{}", err.to_error_message(&database_name)); eprintln!("Skipping..."); println!(); @@ -96,5 +102,9 @@ pub async fn show_database_privileges( table.printstd(); } + if args.fail && contained_errors { + std::process::exit(1); + } + Ok(()) } diff --git a/src/client/commands/show_user.rs b/src/client/commands/show_user.rs index e9649af..1548ac7 100644 --- a/src/client/commands/show_user.rs +++ b/src/client/commands/show_user.rs @@ -20,6 +20,10 @@ pub struct ShowUserArgs { /// Print the information as JSON #[arg(short, long)] json: bool, + + /// Return a non-zero exit code if any of the results were erroneous + #[arg(short, long)] + fail: bool, } pub async fn show_users( @@ -37,12 +41,14 @@ pub async fn show_users( anyhow::bail!(err); } + let mut contained_errors = false; let users = match server_connection.next().await { Some(Ok(Response::ListUsers(users))) => users .into_iter() .filter_map(|(username, result)| match result { Ok(user) => Some(user), Err(err) => { + contained_errors = true; eprintln!("{}", err.to_error_message(&username)); eprintln!("Skipping..."); None @@ -89,5 +95,9 @@ pub async fn show_users( table.printstd(); } + if args.fail && contained_errors { + std::process::exit(1); + } + Ok(()) }