Report back more detailed results from commands #41

Merged
oysteikt merged 1 commits from report-status-on-modification into main 2024-08-07 23:33:58 +02:00
4 changed files with 99 additions and 37 deletions

View File

@ -5,7 +5,7 @@ use prettytable::{Cell, Row, Table};
use sqlx::{Connection, MySqlConnection}; use sqlx::{Connection, MySqlConnection};
use crate::core::{ use crate::core::{
common::{close_database_connection, get_current_unix_user, yn}, common::{close_database_connection, get_current_unix_user, yn, CommandStatus},
database_operations::*, database_operations::*,
database_privilege_operations::*, database_privilege_operations::*,
user_operations::user_exists, user_operations::user_exists,
@ -142,7 +142,7 @@ pub struct DatabaseEditPrivsArgs {
pub async fn handle_command( pub async fn handle_command(
command: DatabaseCommand, command: DatabaseCommand,
mut connection: MySqlConnection, mut connection: MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
let result = connection let result = connection
.transaction(|txn| { .transaction(|txn| {
Box::pin(async move { Box::pin(async move {
@ -165,67 +165,73 @@ pub async fn handle_command(
async fn create_databases( async fn create_databases(
args: DatabaseCreateArgs, args: DatabaseCreateArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
if args.name.is_empty() { if args.name.is_empty() {
anyhow::bail!("No database names provided"); anyhow::bail!("No database names provided");
} }
let mut result = CommandStatus::SuccessfullyModified;
for name in args.name { for name in args.name {
// TODO: This can be optimized by fetching all the database privileges in one query. // TODO: This can be optimized by fetching all the database privileges in one query.
if let Err(e) = create_database(&name, connection).await { if let Err(e) = create_database(&name, connection).await {
eprintln!("Failed to create database '{}': {}", name, e); eprintln!("Failed to create database '{}': {}", name, e);
eprintln!("Skipping..."); eprintln!("Skipping...");
result = CommandStatus::PartiallySuccessfullyModified;
} }
} }
Ok(()) Ok(result)
} }
async fn drop_databases( async fn drop_databases(
args: DatabaseDropArgs, args: DatabaseDropArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
if args.name.is_empty() { if args.name.is_empty() {
anyhow::bail!("No database names provided"); anyhow::bail!("No database names provided");
} }
let mut result = CommandStatus::SuccessfullyModified;
for name in args.name { for name in args.name {
// TODO: This can be optimized by fetching all the database privileges in one query. // TODO: This can be optimized by fetching all the database privileges in one query.
if let Err(e) = drop_database(&name, connection).await { if let Err(e) = drop_database(&name, connection).await {
eprintln!("Failed to drop database '{}': {}", name, e); eprintln!("Failed to drop database '{}': {}", name, e);
eprintln!("Skipping..."); eprintln!("Skipping...");
result = CommandStatus::PartiallySuccessfullyModified;
} }
} }
Ok(()) Ok(result)
} }
async fn list_databases( async fn list_databases(
args: DatabaseListArgs, args: DatabaseListArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
let databases = get_database_list(connection).await?; let databases = get_database_list(connection).await?;
if databases.is_empty() {
println!("No databases to show.");
return Ok(());
}
if args.json { if args.json {
println!("{}", serde_json::to_string_pretty(&databases)?); println!("{}", serde_json::to_string_pretty(&databases)?);
return Ok(CommandStatus::NoModificationsIntended);
}
if databases.is_empty() {
println!("No databases to show.");
} else { } else {
for db in databases { for db in databases {
println!("{}", db); println!("{}", db);
} }
} }
Ok(()) Ok(CommandStatus::NoModificationsIntended)
} }
async fn show_database_privileges( async fn show_database_privileges(
args: DatabaseShowPrivsArgs, args: DatabaseShowPrivsArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
let database_users_to_show = if args.name.is_empty() { let database_users_to_show = if args.name.is_empty() {
get_all_database_privileges(connection).await? get_all_database_privileges(connection).await?
} else { } else {
@ -243,13 +249,13 @@ async fn show_database_privileges(
result result
}; };
if database_users_to_show.is_empty() {
println!("No database users to show.");
return Ok(());
}
if args.json { if args.json {
println!("{}", serde_json::to_string_pretty(&database_users_to_show)?); println!("{}", serde_json::to_string_pretty(&database_users_to_show)?);
return Ok(CommandStatus::NoModificationsIntended);
}
if database_users_to_show.is_empty() {
println!("No database users to show.");
} else { } else {
let mut table = Table::new(); let mut table = Table::new();
table.add_row(Row::new( table.add_row(Row::new(
@ -280,13 +286,13 @@ async fn show_database_privileges(
table.printstd(); table.printstd();
} }
Ok(()) Ok(CommandStatus::NoModificationsIntended)
} }
pub async fn edit_privileges( pub async fn edit_privileges(
args: DatabaseEditPrivsArgs, args: DatabaseEditPrivsArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
let privilege_data = if let Some(name) = &args.name { let privilege_data = if let Some(name) = &args.name {
get_database_privileges(name, connection).await? get_database_privileges(name, connection).await?
} else { } else {
@ -317,14 +323,14 @@ pub async fn edit_privileges(
if diffs.is_empty() { if diffs.is_empty() {
println!("No changes to make."); println!("No changes to make.");
return Ok(()); return Ok(CommandStatus::NoModificationsNeeded);
} }
// TODO: Add confirmation prompt. // TODO: Add confirmation prompt.
apply_privilege_diffs(diffs, connection).await?; apply_privilege_diffs(diffs, connection).await?;
Ok(()) Ok(CommandStatus::SuccessfullyModified)
} }
pub fn parse_privilege_tables_from_args( pub fn parse_privilege_tables_from_args(

View File

@ -9,7 +9,7 @@ use serde_json::json;
use sqlx::{Connection, MySqlConnection}; use sqlx::{Connection, MySqlConnection};
use crate::core::{ use crate::core::{
common::{close_database_connection, get_current_unix_user}, common::{close_database_connection, get_current_unix_user, CommandStatus},
database_operations::*, database_operations::*,
user_operations::*, user_operations::*,
}; };
@ -78,7 +78,7 @@ pub struct UserShowArgs {
pub async fn handle_command( pub async fn handle_command(
command: UserCommand, command: UserCommand,
mut connection: MySqlConnection, mut connection: MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
let result = connection let result = connection
.transaction(|txn| { .transaction(|txn| {
Box::pin(async move { Box::pin(async move {
@ -100,15 +100,18 @@ pub async fn handle_command(
async fn create_users( async fn create_users(
args: UserCreateArgs, args: UserCreateArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
if args.username.is_empty() { if args.username.is_empty() {
anyhow::bail!("No usernames provided"); anyhow::bail!("No usernames provided");
} }
let mut result = CommandStatus::SuccessfullyModified;
for username in args.username { for username in args.username {
if let Err(e) = create_database_user(&username, connection).await { if let Err(e) = create_database_user(&username, connection).await {
eprintln!("{}", e); eprintln!("{}", e);
eprintln!("Skipping...\n"); eprintln!("Skipping...\n");
result = CommandStatus::PartiallySuccessfullyModified;
continue; continue;
} else { } else {
println!("User '{}' created.", username); println!("User '{}' created.", username);
@ -133,21 +136,28 @@ async fn create_users(
} }
println!(); println!();
} }
Ok(()) Ok(result)
} }
async fn drop_users(args: UserDeleteArgs, connection: &mut MySqlConnection) -> anyhow::Result<()> { async fn drop_users(
args: UserDeleteArgs,
connection: &mut MySqlConnection,
) -> anyhow::Result<CommandStatus> {
if args.username.is_empty() { if args.username.is_empty() {
anyhow::bail!("No usernames provided"); anyhow::bail!("No usernames provided");
} }
let mut result = CommandStatus::SuccessfullyModified;
for username in args.username { for username in args.username {
if let Err(e) = delete_database_user(&username, connection).await { if let Err(e) = delete_database_user(&username, connection).await {
eprintln!("{}", e); eprintln!("{}", e);
eprintln!("Skipping..."); eprintln!("Skipping...");
result = CommandStatus::PartiallySuccessfullyModified;
} }
} }
Ok(())
Ok(result)
} }
pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Result<String> { pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Result<String> {
@ -164,7 +174,7 @@ pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Res
async fn change_password_for_user( async fn change_password_for_user(
args: UserPasswdArgs, args: UserPasswdArgs,
connection: &mut MySqlConnection, connection: &mut MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<CommandStatus> {
// NOTE: although this also is checked in `set_password_for_database_user`, we check it here // NOTE: although this also is checked in `set_password_for_database_user`, we check it here
// to provide a more natural order of error messages. // to provide a more natural order of error messages.
let unix_user = get_current_unix_user()?; let unix_user = get_current_unix_user()?;
@ -181,10 +191,13 @@ async fn change_password_for_user(
set_password_for_database_user(&args.username, &password, connection).await?; set_password_for_database_user(&args.username, &password, connection).await?;
Ok(()) Ok(CommandStatus::SuccessfullyModified)
} }
async fn show_users(args: UserShowArgs, connection: &mut MySqlConnection) -> anyhow::Result<()> { async fn show_users(
args: UserShowArgs,
connection: &mut MySqlConnection,
) -> anyhow::Result<CommandStatus> {
let unix_user = get_current_unix_user()?; let unix_user = get_current_unix_user()?;
let users = if args.username.is_empty() { let users = if args.username.is_empty() {
@ -251,5 +264,5 @@ async fn show_users(args: UserShowArgs, connection: &mut MySqlConnection) -> any
table.printstd(); table.printstd();
} }
Ok(()) Ok(CommandStatus::NoModificationsIntended)
} }

View File

@ -7,6 +7,31 @@ use sqlx::{Connection, MySqlConnection};
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
use std::ffi::CString; use std::ffi::CString;
/// Report the result status of a command.
/// This is used to display a status message to the user.
pub enum CommandStatus {
/// The command was successful,
/// and made modification to the database.
SuccessfullyModified,
/// The command was mostly successful,
/// and modifications have been made to the database.
/// However, some of the requested modifications failed.
PartiallySuccessfullyModified,
/// The command was successful,
/// but no modifications were needed.
NoModificationsNeeded,
/// The command was successful,
/// and made no modification to the database.
NoModificationsIntended,
/// The command was cancelled, either through a dialog or a signal.
/// No modifications have been made to the database.
Cancelled,
}
pub fn get_current_unix_user() -> anyhow::Result<User> { pub fn get_current_unix_user() -> anyhow::Result<User> {
User::from_uid(getuid()) User::from_uid(getuid())
.context("Failed to look up your UNIX username") .context("Failed to look up your UNIX username")

View File

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
extern crate prettytable; extern crate prettytable;
use core::common::CommandStatus;
#[cfg(feature = "mysql-admutils-compatibility")] #[cfg(feature = "mysql-admutils-compatibility")]
use std::path::PathBuf; use std::path::PathBuf;
@ -73,9 +74,26 @@ async fn main() -> anyhow::Result<()> {
}; };
match result { match result {
Ok(_) => println!("Changes committed to database"), Ok(CommandStatus::SuccessfullyModified) => {
Err(_) => println!("Changes reverted due to error"), println!("Modifications committed successfully");
Ok(())
}
Ok(CommandStatus::PartiallySuccessfullyModified) => {
println!("Some modifications committed successfully");
Ok(())
}
Ok(CommandStatus::NoModificationsNeeded) => {
println!("No modifications made");
Ok(())
}
Ok(CommandStatus::NoModificationsIntended) => {
/* Don't report anything */
Ok(())
}
Ok(CommandStatus::Cancelled) => {
println!("Command cancelled successfully");
Ok(())
}
Err(e) => Err(e),
} }
result
} }