treewide: move some code around, spring cleaning
This commit is contained in:
@@ -7,12 +7,9 @@ use prettytable::{Cell, Row, Table};
|
||||
use sqlx::{Connection, MySqlConnection};
|
||||
|
||||
use crate::core::{
|
||||
self,
|
||||
common::{close_database_connection, get_current_unix_user},
|
||||
database_operations::{
|
||||
apply_permission_diffs, db_priv_field_human_readable_name, diff_permissions, yn,
|
||||
DatabasePrivileges, DATABASE_PRIVILEGE_FIELDS,
|
||||
},
|
||||
common::{close_database_connection, get_current_unix_user, yn},
|
||||
database_operations::*,
|
||||
database_privilege_operations::*,
|
||||
user_operations::user_exists,
|
||||
};
|
||||
|
||||
@@ -174,7 +171,7 @@ async fn create_databases(
|
||||
|
||||
for name in args.name {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
if let Err(e) = core::database_operations::create_database(&name, conn).await {
|
||||
if let Err(e) = create_database(&name, conn).await {
|
||||
eprintln!("Failed to create database '{}': {}", name, e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -190,7 +187,7 @@ async fn drop_databases(args: DatabaseDropArgs, conn: &mut MySqlConnection) -> a
|
||||
|
||||
for name in args.name {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
if let Err(e) = core::database_operations::drop_database(&name, conn).await {
|
||||
if let Err(e) = drop_database(&name, conn).await {
|
||||
eprintln!("Failed to drop database '{}': {}", name, e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -200,7 +197,7 @@ async fn drop_databases(args: DatabaseDropArgs, conn: &mut MySqlConnection) -> a
|
||||
}
|
||||
|
||||
async fn list_databases(args: DatabaseListArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
let databases = core::database_operations::get_database_list(conn).await?;
|
||||
let databases = get_database_list(conn).await?;
|
||||
|
||||
if databases.is_empty() {
|
||||
println!("No databases to show.");
|
||||
@@ -223,12 +220,12 @@ async fn show_databases(
|
||||
conn: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let database_users_to_show = if args.name.is_empty() {
|
||||
core::database_operations::get_all_database_privileges(conn).await?
|
||||
get_all_database_privileges(conn).await?
|
||||
} else {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
let mut result = Vec::with_capacity(args.name.len());
|
||||
for name in args.name {
|
||||
match core::database_operations::get_database_privileges(&name, conn).await {
|
||||
match get_database_privileges(&name, conn).await {
|
||||
Ok(db) => result.extend(db),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to show database '{}': {}", name, e);
|
||||
@@ -280,7 +277,7 @@ async fn show_databases(
|
||||
}
|
||||
|
||||
/// See documentation for `DatabaseCommand::EditPerm`.
|
||||
fn parse_permission_table_cli_arg(arg: &str) -> anyhow::Result<DatabasePrivileges> {
|
||||
fn parse_permission_table_cli_arg(arg: &str) -> anyhow::Result<DatabasePrivilegeRow> {
|
||||
let parts: Vec<&str> = arg.split(':').collect();
|
||||
if parts.len() != 3 {
|
||||
anyhow::bail!("Invalid argument format. See `edit-perm --help` for more information.");
|
||||
@@ -290,7 +287,7 @@ fn parse_permission_table_cli_arg(arg: &str) -> anyhow::Result<DatabasePrivilege
|
||||
let user = parts[1].to_string();
|
||||
let privs = parts[2].to_string();
|
||||
|
||||
let mut result = DatabasePrivileges {
|
||||
let mut result = DatabasePrivilegeRow {
|
||||
db,
|
||||
user,
|
||||
select_priv: false,
|
||||
@@ -347,7 +344,7 @@ fn parse_permission(yn: &str) -> anyhow::Result<bool> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<DatabasePrivileges>> {
|
||||
fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<DatabasePrivilegeRow>> {
|
||||
content
|
||||
.trim()
|
||||
.split('\n')
|
||||
@@ -360,7 +357,7 @@ fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<Data
|
||||
anyhow::bail!("")
|
||||
}
|
||||
|
||||
Ok(DatabasePrivileges {
|
||||
Ok(DatabasePrivilegeRow {
|
||||
db: (*line_parts.first().unwrap()).to_owned(),
|
||||
user: (*line_parts.get(1).unwrap()).to_owned(),
|
||||
select_priv: parse_permission(line_parts.get(2).unwrap())
|
||||
@@ -387,11 +384,11 @@ fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<Data
|
||||
.context("Could not parse REFERENCES privilege")?,
|
||||
})
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivileges>>>()
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivilegeRow>>>()
|
||||
}
|
||||
|
||||
fn format_privileges_line(
|
||||
privs: &DatabasePrivileges,
|
||||
privs: &DatabasePrivilegeRow,
|
||||
username_len: usize,
|
||||
database_name_len: usize,
|
||||
) -> String {
|
||||
@@ -420,9 +417,9 @@ pub async fn edit_permissions(
|
||||
conn: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let permission_data = if let Some(name) = &args.name {
|
||||
core::database_operations::get_database_privileges(name, conn).await?
|
||||
get_database_privileges(name, conn).await?
|
||||
} else {
|
||||
core::database_operations::get_all_database_privileges(conn).await?
|
||||
get_all_database_privileges(conn).await?
|
||||
};
|
||||
|
||||
let permissions_to_change = if !args.perm.is_empty() {
|
||||
@@ -433,7 +430,7 @@ pub async fn edit_permissions(
|
||||
parse_permission_table_cli_arg(&format!("{}:{}", name, &perm))
|
||||
.context(format!("Failed parsing database permissions: `{}`", &perm))
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivileges>>>()?
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivilegeRow>>>()?
|
||||
} else {
|
||||
args.perm
|
||||
.iter()
|
||||
@@ -441,7 +438,7 @@ pub async fn edit_permissions(
|
||||
parse_permission_table_cli_arg(perm)
|
||||
.context(format!("Failed parsing database permissions: `{}`", &perm))
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivileges>>>()?
|
||||
.collect::<anyhow::Result<Vec<DatabasePrivilegeRow>>>()?
|
||||
}
|
||||
} else {
|
||||
let comment = indoc! {r#"
|
||||
@@ -479,7 +476,7 @@ pub async fn edit_permissions(
|
||||
header[1] = format!("{:width$}", header[1], width = longest_username);
|
||||
|
||||
let example_line = format_privileges_line(
|
||||
&DatabasePrivileges {
|
||||
&DatabasePrivilegeRow {
|
||||
db: example_db,
|
||||
user: example_user,
|
||||
select_priv: true,
|
||||
|
@@ -7,8 +7,10 @@ use crate::{
|
||||
mysql_admutils_compatibility::common::{filter_db_or_user_names, DbOrUser},
|
||||
},
|
||||
core::{
|
||||
common::yn,
|
||||
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
|
||||
database_operations::{self, yn},
|
||||
database_operations::{create_database, drop_database, get_database_list},
|
||||
database_privilege_operations,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -129,20 +131,20 @@ pub async fn main() -> anyhow::Result<()> {
|
||||
Command::Create(args) => {
|
||||
let filtered_names = filter_db_or_user_names(args.name, DbOrUser::Database)?;
|
||||
for name in filtered_names {
|
||||
database_operations::create_database(&name, &mut connection).await?;
|
||||
create_database(&name, &mut connection).await?;
|
||||
println!("Database {} created.", name);
|
||||
}
|
||||
}
|
||||
Command::Drop(args) => {
|
||||
let filtered_names = filter_db_or_user_names(args.name, DbOrUser::Database)?;
|
||||
for name in filtered_names {
|
||||
database_operations::drop_database(&name, &mut connection).await?;
|
||||
drop_database(&name, &mut connection).await?;
|
||||
println!("Database {} dropped.", name);
|
||||
}
|
||||
}
|
||||
Command::Show(args) => {
|
||||
let names = if args.name.is_empty() {
|
||||
database_operations::get_database_list(&mut connection).await?
|
||||
get_database_list(&mut connection).await?
|
||||
} else {
|
||||
filter_db_or_user_names(args.name, DbOrUser::Database)?
|
||||
};
|
||||
@@ -176,7 +178,7 @@ async fn show_db(name: &str, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
// for non-existent databases will report with no users.
|
||||
// This function should *not* check for db existence, only
|
||||
// validate the names.
|
||||
let permissions = database_operations::get_database_privileges(name, conn)
|
||||
let permissions = database_privilege_operations::get_database_privileges(name, conn)
|
||||
.await
|
||||
.unwrap_or(vec![]);
|
||||
|
||||
|
@@ -9,10 +9,7 @@ use crate::{
|
||||
core::{
|
||||
common::{close_database_connection, get_current_unix_user},
|
||||
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
|
||||
user_operations::{
|
||||
create_database_user, delete_database_user, get_all_database_users_for_unix_user,
|
||||
get_database_user_for_user, set_password_for_database_user, user_exists,
|
||||
},
|
||||
user_operations::*,
|
||||
},
|
||||
};
|
||||
|
||||
|
@@ -9,9 +9,9 @@ use serde_json::json;
|
||||
use sqlx::{Connection, MySqlConnection};
|
||||
|
||||
use crate::core::{
|
||||
common::close_database_connection,
|
||||
database_operations::get_databases_where_user_has_privileges,
|
||||
user_operations::validate_user_name,
|
||||
common::{close_database_connection, get_current_unix_user},
|
||||
database_operations::*,
|
||||
user_operations::*,
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -99,7 +99,7 @@ async fn create_users(args: UserCreateArgs, conn: &mut MySqlConnection) -> anyho
|
||||
}
|
||||
|
||||
for username in args.username {
|
||||
if let Err(e) = crate::core::user_operations::create_database_user(&username, conn).await {
|
||||
if let Err(e) = create_database_user(&username, conn).await {
|
||||
eprintln!("{}", e);
|
||||
eprintln!("Skipping...\n");
|
||||
continue;
|
||||
@@ -135,7 +135,7 @@ async fn drop_users(args: UserDeleteArgs, conn: &mut MySqlConnection) -> anyhow:
|
||||
}
|
||||
|
||||
for username in args.username {
|
||||
if let Err(e) = crate::core::user_operations::delete_database_user(&username, conn).await {
|
||||
if let Err(e) = delete_database_user(&username, conn).await {
|
||||
eprintln!("{}", e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -160,7 +160,7 @@ async fn change_password_for_user(
|
||||
) -> anyhow::Result<()> {
|
||||
// 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.
|
||||
let unix_user = crate::core::common::get_current_unix_user()?;
|
||||
let unix_user = get_current_unix_user()?;
|
||||
validate_user_name(&args.username, &unix_user)?;
|
||||
|
||||
let password = if let Some(password_file) = args.password_file {
|
||||
@@ -172,17 +172,16 @@ async fn change_password_for_user(
|
||||
read_password_from_stdin_with_double_check(&args.username)?
|
||||
};
|
||||
|
||||
crate::core::user_operations::set_password_for_database_user(&args.username, &password, conn)
|
||||
.await?;
|
||||
set_password_for_database_user(&args.username, &password, conn).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
let unix_user = crate::core::common::get_current_unix_user()?;
|
||||
let unix_user = get_current_unix_user()?;
|
||||
|
||||
let users = if args.username.is_empty() {
|
||||
crate::core::user_operations::get_all_database_users_for_unix_user(&unix_user, conn).await?
|
||||
get_all_database_users_for_unix_user(&unix_user, conn).await?
|
||||
} else {
|
||||
let mut result = vec![];
|
||||
for username in args.username {
|
||||
@@ -192,8 +191,7 @@ async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::R
|
||||
continue;
|
||||
}
|
||||
|
||||
let user =
|
||||
crate::core::user_operations::get_database_user_for_user(&username, conn).await?;
|
||||
let user = get_database_user_for_user(&username, conn).await?;
|
||||
if let Some(user) = user {
|
||||
result.push(user);
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user