2024-08-07 23:00:38 +02:00
|
|
|
use anyhow::Context;
|
2024-04-21 06:03:25 +02:00
|
|
|
use clap::Parser;
|
2024-08-08 21:48:17 +02:00
|
|
|
use dialoguer::{Confirm, Editor};
|
2024-08-10 02:16:38 +02:00
|
|
|
use futures_util::{SinkExt, StreamExt};
|
|
|
|
use nix::unistd::{getuid, User};
|
2024-04-21 06:03:25 +02:00
|
|
|
use prettytable::{Cell, Row, Table};
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
use crate::{
|
|
|
|
cli::common::erroneous_server_response,
|
|
|
|
core::{
|
|
|
|
common::yn,
|
|
|
|
database_privileges::{
|
|
|
|
db_priv_field_human_readable_name, diff_privileges, display_privilege_diffs,
|
|
|
|
generate_editor_content_from_privilege_data, parse_privilege_data_from_editor_content,
|
|
|
|
parse_privilege_table_cli_arg,
|
|
|
|
},
|
|
|
|
protocol::{
|
|
|
|
print_create_databases_output_status, print_drop_databases_output_status,
|
|
|
|
print_modify_database_privileges_output_status, ClientToServerMessageStream, Request,
|
|
|
|
Response,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
server::sql::database_privilege_operations::{DatabasePrivilegeRow, DATABASE_PRIVILEGE_FIELDS},
|
2024-04-26 00:30:32 +02:00
|
|
|
};
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-07-09 19:54:27 +02:00
|
|
|
// #[command(next_help_heading = Some(DATABASE_COMMAND_HEADER))]
|
|
|
|
pub enum DatabaseCommand {
|
|
|
|
/// Create one or more databases
|
|
|
|
#[command()]
|
|
|
|
CreateDb(DatabaseCreateArgs),
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-07-09 19:54:27 +02:00
|
|
|
/// Delete one or more databases
|
|
|
|
#[command()]
|
|
|
|
DropDb(DatabaseDropArgs),
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-07-09 19:54:27 +02:00
|
|
|
/// List all databases you have access to
|
2024-04-21 06:03:25 +02:00
|
|
|
#[command()]
|
2024-07-09 19:54:27 +02:00
|
|
|
ListDb(DatabaseListArgs),
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
/// List user privileges for one or more databases
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// If no database names are provided, it will show privileges for all databases you have access to.
|
2024-07-09 19:54:27 +02:00
|
|
|
#[command()]
|
2024-08-07 21:51:03 +02:00
|
|
|
ShowDbPrivs(DatabaseShowPrivsArgs),
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
/// Change user privileges for one or more databases. See `edit-db-privs --help` for details.
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
|
|
|
/// This command has two modes of operation:
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// 1. Interactive mode: If nothing else is specified, the user will be prompted to edit the privileges using a text editor.
|
2024-04-26 00:30:32 +02:00
|
|
|
///
|
2024-07-09 19:54:27 +02:00
|
|
|
/// You can configure your preferred text editor by setting the `VISUAL` or `EDITOR` environment variables.
|
|
|
|
///
|
2024-04-26 00:30:32 +02:00
|
|
|
/// Follow the instructions inside the editor for more information.
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// 2. Non-interactive mode: If the `-p` flag is specified, the user can write privileges using arguments.
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// The privilege arguments should be formatted as `<db>:<user>:<privileges>`
|
|
|
|
/// where the privileges are a string of characters, each representing a single privilege.
|
|
|
|
/// The character `A` is an exception - it represents all privileges.
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// The character-to-privilege mapping is defined as follows:
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-04-26 00:30:32 +02:00
|
|
|
/// - `s` - SELECT
|
|
|
|
/// - `i` - INSERT
|
|
|
|
/// - `u` - UPDATE
|
|
|
|
/// - `d` - DELETE
|
|
|
|
/// - `c` - CREATE
|
|
|
|
/// - `D` - DROP
|
|
|
|
/// - `a` - ALTER
|
|
|
|
/// - `I` - INDEX
|
|
|
|
/// - `t` - CREATE TEMPORARY TABLES
|
|
|
|
/// - `l` - LOCK TABLES
|
|
|
|
/// - `r` - REFERENCES
|
|
|
|
/// - `A` - ALL PRIVILEGES
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// If you provide a database name, you can omit it from the privilege string,
|
|
|
|
/// e.g. `edit-db-privs my_db -p my_user:siu` is equivalent to `edit-db-privs -p my_db:my_user:siu`.
|
|
|
|
/// While it doesn't make much of a difference for a single edit, it can be useful for editing multiple users
|
|
|
|
/// on the same database at once.
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
|
|
|
/// Example usage of non-interactive mode:
|
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// Enable privileges `SELECT`, `INSERT`, and `UPDATE` for user `my_user` on database `my_db`:
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// `mysqladm edit-db-privs -p my_db:my_user:siu`
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// Enable all privileges for user `my_other_user` on database `my_other_db`:
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// `mysqladm edit-db-privs -p my_other_db:my_other_user:A`
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// Set miscellaneous privileges for multiple users on database `my_db`:
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
2024-08-07 21:51:03 +02:00
|
|
|
/// `mysqladm edit-db-privs my_db -p my_user:siu my_other_user:ct``
|
2024-07-09 19:54:27 +02:00
|
|
|
///
|
|
|
|
#[command(verbatim_doc_comment)]
|
2024-08-07 21:51:03 +02:00
|
|
|
EditDbPrivs(DatabaseEditPrivsArgs),
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-07-09 19:54:27 +02:00
|
|
|
pub struct DatabaseCreateArgs {
|
2024-04-21 06:03:25 +02:00
|
|
|
/// The name of the database(s) to create.
|
|
|
|
#[arg(num_args = 1..)]
|
|
|
|
name: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-07-09 19:54:27 +02:00
|
|
|
pub struct DatabaseDropArgs {
|
2024-04-21 06:03:25 +02:00
|
|
|
/// The name of the database(s) to drop.
|
|
|
|
#[arg(num_args = 1..)]
|
|
|
|
name: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-07-09 19:54:27 +02:00
|
|
|
pub struct DatabaseListArgs {
|
2024-04-21 06:03:25 +02:00
|
|
|
/// Whether to output the information in JSON format.
|
|
|
|
#[arg(short, long)]
|
|
|
|
json: bool,
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-08-07 21:51:03 +02:00
|
|
|
pub struct DatabaseShowPrivsArgs {
|
2024-04-21 06:03:25 +02:00
|
|
|
/// The name of the database(s) to show.
|
|
|
|
#[arg(num_args = 0..)]
|
|
|
|
name: Vec<String>,
|
|
|
|
|
|
|
|
/// Whether to output the information in JSON format.
|
|
|
|
#[arg(short, long)]
|
|
|
|
json: bool,
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-08-07 21:51:03 +02:00
|
|
|
pub struct DatabaseEditPrivsArgs {
|
|
|
|
/// The name of the database to edit privileges for.
|
2024-08-05 22:37:23 +02:00
|
|
|
pub name: Option<String>,
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
#[arg(short, long, value_name = "[DATABASE:]USER:PRIVILEGES", num_args = 0..)]
|
|
|
|
pub privs: Vec<String>,
|
2024-04-21 06:03:25 +02:00
|
|
|
|
|
|
|
/// Whether to output the information in JSON format.
|
|
|
|
#[arg(short, long)]
|
2024-08-05 22:37:23 +02:00
|
|
|
pub json: bool,
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
/// Specify the text editor to use for editing privileges
|
2024-04-21 06:03:25 +02:00
|
|
|
#[arg(short, long)]
|
2024-08-05 22:37:23 +02:00
|
|
|
pub editor: Option<String>,
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-07-09 19:54:27 +02:00
|
|
|
/// Disable interactive confirmation before saving changes.
|
2024-04-21 06:03:25 +02:00
|
|
|
#[arg(short, long)]
|
2024-08-05 22:37:23 +02:00
|
|
|
pub yes: bool,
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:54:27 +02:00
|
|
|
pub async fn handle_command(
|
|
|
|
command: DatabaseCommand,
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
match command {
|
|
|
|
DatabaseCommand::CreateDb(args) => create_databases(args, server_connection).await,
|
|
|
|
DatabaseCommand::DropDb(args) => drop_databases(args, server_connection).await,
|
|
|
|
DatabaseCommand::ListDb(args) => list_databases(args, server_connection).await,
|
|
|
|
DatabaseCommand::ShowDbPrivs(args) => {
|
|
|
|
show_database_privileges(args, server_connection).await
|
|
|
|
}
|
|
|
|
DatabaseCommand::EditDbPrivs(args) => {
|
|
|
|
edit_database_privileges(args, server_connection).await
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_databases(
|
|
|
|
args: DatabaseCreateArgs,
|
2024-08-10 02:16:38 +02:00
|
|
|
mut server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
2024-04-21 06:03:25 +02:00
|
|
|
if args.name.is_empty() {
|
|
|
|
anyhow::bail!("No database names provided");
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let message = Request::CreateDatabases(args.name.clone());
|
|
|
|
server_connection.send(message).await?;
|
2024-08-07 23:33:07 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let result = match server_connection.next().await {
|
|
|
|
Some(Ok(Response::CreateDatabases(result))) => result,
|
|
|
|
response => return erroneous_server_response(response),
|
|
|
|
};
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
|
|
|
|
print_create_databases_output_status(&result);
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 21:54:13 +02:00
|
|
|
async fn drop_databases(
|
|
|
|
args: DatabaseDropArgs,
|
2024-08-10 02:16:38 +02:00
|
|
|
mut server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
2024-04-21 06:03:25 +02:00
|
|
|
if args.name.is_empty() {
|
|
|
|
anyhow::bail!("No database names provided");
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let message = Request::DropDatabases(args.name.clone());
|
|
|
|
server_connection.send(message).await?;
|
2024-08-07 23:33:07 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let result = match server_connection.next().await {
|
|
|
|
Some(Ok(Response::DropDatabases(result))) => result,
|
|
|
|
response => return erroneous_server_response(response),
|
|
|
|
};
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
|
|
|
|
print_drop_databases_output_status(&result);
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 21:54:13 +02:00
|
|
|
async fn list_databases(
|
|
|
|
args: DatabaseListArgs,
|
2024-08-10 02:16:38 +02:00
|
|
|
mut server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let message = Request::ListDatabases;
|
|
|
|
server_connection.send(message).await?;
|
|
|
|
|
|
|
|
let result = match server_connection.next().await {
|
|
|
|
Some(Ok(Response::ListAllDatabases(result))) => result,
|
|
|
|
response => return erroneous_server_response(response),
|
|
|
|
};
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
|
|
|
|
let database_list = match result {
|
|
|
|
Ok(list) => list,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(anyhow::anyhow!(err.to_error_message()).context("Failed to list databases"))
|
|
|
|
}
|
|
|
|
};
|
2024-08-07 23:33:07 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
if args.json {
|
|
|
|
println!("{}", serde_json::to_string_pretty(&database_list)?);
|
|
|
|
} else if database_list.is_empty() {
|
2024-08-07 23:33:07 +02:00
|
|
|
println!("No databases to show.");
|
2024-04-21 06:03:25 +02:00
|
|
|
} else {
|
2024-08-10 02:16:38 +02:00
|
|
|
for db in database_list {
|
2024-04-26 00:30:32 +02:00
|
|
|
println!("{}", db);
|
|
|
|
}
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
Ok(())
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
async fn show_database_privileges(
|
|
|
|
args: DatabaseShowPrivsArgs,
|
2024-08-10 02:16:38 +02:00
|
|
|
mut server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let message = if args.name.is_empty() {
|
|
|
|
Request::ListPrivileges(None)
|
2024-04-21 06:03:25 +02:00
|
|
|
} else {
|
2024-08-10 02:16:38 +02:00
|
|
|
Request::ListPrivileges(Some(args.name.clone()))
|
|
|
|
};
|
|
|
|
server_connection.send(message).await?;
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
eprintln!("{}", err.to_error_message(&database_name));
|
2024-04-21 06:03:25 +02:00
|
|
|
eprintln!("Skipping...");
|
2024-08-10 02:16:38 +02:00
|
|
|
println!();
|
|
|
|
None
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
2024-08-10 02:16:38 +02:00
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
Some(Ok(Response::ListAllPrivileges(privilege_rows))) => match privilege_rows {
|
|
|
|
Ok(list) => list,
|
|
|
|
Err(err) => {
|
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
return Err(anyhow::anyhow!(err.to_error_message())
|
|
|
|
.context("Failed to list database privileges"));
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
2024-08-10 02:16:38 +02:00
|
|
|
},
|
|
|
|
response => return erroneous_server_response(response),
|
2024-04-21 06:03:25 +02:00
|
|
|
};
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection.send(Request::Exit).await?;
|
2024-08-07 23:33:07 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
if args.json {
|
|
|
|
println!("{}", serde_json::to_string_pretty(&privilege_data)?);
|
|
|
|
} else if privilege_data.is_empty() {
|
|
|
|
println!("No database privileges to show.");
|
2024-04-21 06:03:25 +02:00
|
|
|
} else {
|
|
|
|
let mut table = Table::new();
|
|
|
|
table.add_row(Row::new(
|
2024-04-26 00:30:32 +02:00
|
|
|
DATABASE_PRIVILEGE_FIELDS
|
|
|
|
.into_iter()
|
|
|
|
.map(db_priv_field_human_readable_name)
|
|
|
|
.map(|name| Cell::new(&name))
|
2024-04-21 06:03:25 +02:00
|
|
|
.collect(),
|
|
|
|
));
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
for row in privilege_data {
|
2024-04-21 06:03:25 +02:00
|
|
|
table.add_row(row![
|
|
|
|
row.db,
|
|
|
|
row.user,
|
2024-07-12 23:35:57 +02:00
|
|
|
c->yn(row.select_priv),
|
|
|
|
c->yn(row.insert_priv),
|
|
|
|
c->yn(row.update_priv),
|
|
|
|
c->yn(row.delete_priv),
|
|
|
|
c->yn(row.create_priv),
|
|
|
|
c->yn(row.drop_priv),
|
|
|
|
c->yn(row.alter_priv),
|
|
|
|
c->yn(row.index_priv),
|
|
|
|
c->yn(row.create_tmp_table_priv),
|
|
|
|
c->yn(row.lock_tables_priv),
|
|
|
|
c->yn(row.references_priv),
|
2024-04-21 06:03:25 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
table.printstd();
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
Ok(())
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
pub async fn edit_database_privileges(
|
2024-08-07 21:51:03 +02:00
|
|
|
args: DatabaseEditPrivsArgs,
|
2024-08-10 02:16:38 +02:00
|
|
|
mut server_connection: ClientToServerMessageStream,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let message = Request::ListPrivileges(args.name.clone().map(|name| vec![name]));
|
|
|
|
|
|
|
|
server_connection.send(message).await?;
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
eprintln!("{}", err.to_error_message(&database_name));
|
|
|
|
eprintln!("Skipping...");
|
|
|
|
println!();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
Some(Ok(Response::ListAllPrivileges(privilege_rows))) => match privilege_rows {
|
|
|
|
Ok(list) => list,
|
|
|
|
Err(err) => {
|
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
return Err(anyhow::anyhow!(err.to_error_message())
|
|
|
|
.context("Failed to list database privileges"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
response => return erroneous_server_response(response),
|
2024-04-21 06:03:25 +02:00
|
|
|
};
|
|
|
|
|
2024-08-07 21:51:03 +02:00
|
|
|
let privileges_to_change = if !args.privs.is_empty() {
|
2024-08-07 23:00:38 +02:00
|
|
|
parse_privilege_tables_from_args(&args)?
|
2024-04-26 00:30:32 +02:00
|
|
|
} else {
|
2024-08-17 21:24:59 +02:00
|
|
|
edit_privileges_with_editor(&privilege_data, args.name.as_deref())?
|
2024-04-26 00:30:32 +02:00
|
|
|
};
|
|
|
|
|
2024-08-08 21:02:25 +02:00
|
|
|
let diffs = diff_privileges(&privilege_data, &privileges_to_change);
|
2024-04-26 00:30:32 +02:00
|
|
|
|
|
|
|
if diffs.is_empty() {
|
|
|
|
println!("No changes to make.");
|
2024-08-10 02:16:38 +02:00
|
|
|
return Ok(());
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 21:48:17 +02:00
|
|
|
println!("The following changes will be made:\n");
|
|
|
|
println!("{}", display_privilege_diffs(&diffs));
|
2024-08-10 02:16:38 +02:00
|
|
|
|
2024-08-08 21:48:17 +02:00
|
|
|
if !args.yes
|
|
|
|
&& !Confirm::new()
|
|
|
|
.with_prompt("Do you want to apply these changes?")
|
|
|
|
.default(false)
|
|
|
|
.show_default(true)
|
|
|
|
.interact()?
|
|
|
|
{
|
2024-08-10 02:16:38 +02:00
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
return Ok(());
|
2024-08-08 21:48:17 +02:00
|
|
|
}
|
2024-04-26 00:30:32 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let message = Request::ModifyPrivileges(diffs);
|
|
|
|
server_connection.send(message).await?;
|
2024-04-21 06:03:25 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
let result = match server_connection.next().await {
|
|
|
|
Some(Ok(Response::ModifyPrivileges(result))) => result,
|
|
|
|
response => return erroneous_server_response(response),
|
|
|
|
};
|
|
|
|
|
|
|
|
print_modify_database_privileges_output_status(&result);
|
|
|
|
|
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-21 06:03:25 +02:00
|
|
|
}
|
2024-08-07 23:00:38 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
fn parse_privilege_tables_from_args(
|
2024-08-07 23:00:38 +02:00
|
|
|
args: &DatabaseEditPrivsArgs,
|
|
|
|
) -> anyhow::Result<Vec<DatabasePrivilegeRow>> {
|
|
|
|
debug_assert!(!args.privs.is_empty());
|
|
|
|
let result = if let Some(name) = &args.name {
|
|
|
|
args.privs
|
|
|
|
.iter()
|
|
|
|
.map(|p| {
|
|
|
|
parse_privilege_table_cli_arg(&format!("{}:{}", name, &p))
|
|
|
|
.context(format!("Failed parsing database privileges: `{}`", &p))
|
|
|
|
})
|
|
|
|
.collect::<anyhow::Result<Vec<DatabasePrivilegeRow>>>()?
|
|
|
|
} else {
|
|
|
|
args.privs
|
|
|
|
.iter()
|
|
|
|
.map(|p| {
|
|
|
|
parse_privilege_table_cli_arg(p)
|
|
|
|
.context(format!("Failed parsing database privileges: `{}`", &p))
|
|
|
|
})
|
|
|
|
.collect::<anyhow::Result<Vec<DatabasePrivilegeRow>>>()?
|
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
fn edit_privileges_with_editor(
|
2024-08-07 23:00:38 +02:00
|
|
|
privilege_data: &[DatabasePrivilegeRow],
|
2024-08-17 21:24:59 +02:00
|
|
|
database_name: Option<&str>,
|
2024-08-07 23:00:38 +02:00
|
|
|
) -> anyhow::Result<Vec<DatabasePrivilegeRow>> {
|
2024-08-10 02:16:38 +02:00
|
|
|
let unix_user = User::from_uid(getuid())
|
|
|
|
.context("Failed to look up your UNIX username")
|
|
|
|
.and_then(|u| u.ok_or(anyhow::anyhow!("Failed to look up your UNIX username")))?;
|
2024-08-07 23:00:38 +02:00
|
|
|
|
|
|
|
let editor_content =
|
2024-08-17 21:24:59 +02:00
|
|
|
generate_editor_content_from_privilege_data(privilege_data, &unix_user.name, database_name);
|
2024-08-07 23:00:38 +02:00
|
|
|
|
|
|
|
// TODO: handle errors better here
|
2024-08-10 02:16:38 +02:00
|
|
|
let result = Editor::new().extension("tsv").edit(&editor_content)?;
|
2024-08-07 23:00:38 +02:00
|
|
|
|
2024-08-10 02:16:38 +02:00
|
|
|
match result {
|
|
|
|
None => Ok(privilege_data.to_vec()),
|
|
|
|
Some(result) => parse_privilege_data_from_editor_content(result)
|
|
|
|
.context("Could not parse privilege data from editor"),
|
|
|
|
}
|
2024-08-07 23:00:38 +02:00
|
|
|
}
|