core/common: make testable, fix some status messages
This commit is contained in:
@@ -1,30 +1,7 @@
|
||||
use crate::core::common::{
|
||||
get_current_unix_user, validate_name_token, validate_ownership_by_user_prefix,
|
||||
get_current_unix_user, validate_name_or_error, validate_ownership_or_error, DbOrUser,
|
||||
};
|
||||
|
||||
/// This enum is used to differentiate between database and user operations.
|
||||
/// Their output are very similar, but there are slight differences in the words used.
|
||||
pub enum DbOrUser {
|
||||
Database,
|
||||
User,
|
||||
}
|
||||
|
||||
impl DbOrUser {
|
||||
pub fn lowercased(&self) -> String {
|
||||
match self {
|
||||
DbOrUser::Database => "database".to_string(),
|
||||
DbOrUser::User => "user".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn capitalized(&self) -> String {
|
||||
match self {
|
||||
DbOrUser::Database => "Database".to_string(),
|
||||
DbOrUser::User => "User".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In contrast to the new implementation which reports errors on any invalid name
|
||||
/// for any reason, mysql-admutils would only log the error and skip that particular
|
||||
/// name. This function replicates that behavior.
|
||||
@@ -45,7 +22,7 @@ pub fn filter_db_or_user_names(
|
||||
// here.
|
||||
.map(|name| name.chars().take(32).collect::<String>())
|
||||
.filter(|name| {
|
||||
if let Err(_err) = validate_ownership_by_user_prefix(name, &unix_user) {
|
||||
if let Err(_err) = validate_ownership_or_error(name, &unix_user, db_or_user) {
|
||||
println!(
|
||||
"You are not in charge of mysql-{}: '{}'. Skipping.",
|
||||
db_or_user.lowercased(),
|
||||
@@ -60,7 +37,7 @@ pub fn filter_db_or_user_names(
|
||||
// the name is already truncated to 32 characters. So
|
||||
// if there is an error, it's guaranteed to be due to
|
||||
// invalid characters.
|
||||
if let Err(_err) = validate_name_token(name) {
|
||||
if let Err(_err) = validate_name_or_error(name, db_or_user) {
|
||||
println!(
|
||||
concat!(
|
||||
"{}: {} name '{}' contains invalid characters.\n",
|
||||
|
@@ -2,12 +2,9 @@ use clap::Parser;
|
||||
use sqlx::MySqlConnection;
|
||||
|
||||
use crate::{
|
||||
cli::{
|
||||
database_command,
|
||||
mysql_admutils_compatibility::common::{filter_db_or_user_names, DbOrUser},
|
||||
},
|
||||
cli::{database_command, mysql_admutils_compatibility::common::filter_db_or_user_names},
|
||||
core::{
|
||||
common::yn,
|
||||
common::{yn, DbOrUser},
|
||||
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
|
||||
database_operations::{create_database, drop_database, get_database_list},
|
||||
database_privilege_operations,
|
||||
|
@@ -2,12 +2,9 @@ use clap::Parser;
|
||||
use sqlx::MySqlConnection;
|
||||
|
||||
use crate::{
|
||||
cli::{
|
||||
mysql_admutils_compatibility::common::{filter_db_or_user_names, DbOrUser},
|
||||
user_command,
|
||||
},
|
||||
cli::{mysql_admutils_compatibility::common::filter_db_or_user_names, user_command},
|
||||
core::{
|
||||
common::{close_database_connection, get_current_unix_user},
|
||||
common::{close_database_connection, get_current_unix_user, DbOrUser},
|
||||
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
|
||||
user_operations::*,
|
||||
},
|
||||
|
Reference in New Issue
Block a user