client/show-privs: allow showing single char hints in table output
All checks were successful
Build and test / check (push) Successful in 1m44s
Build and test / build (push) Successful in 3m24s
Build and test / test (push) Successful in 3m28s
Build and test / check-license (push) Successful in 4m54s
Build and test / docs (push) Successful in 6m43s

This commit is contained in:
2025-12-14 15:58:02 +09:00
parent 56596835fa
commit 7b79f7b163
3 changed files with 41 additions and 3 deletions

View File

@@ -27,6 +27,12 @@ pub struct ShowPrivsArgs {
#[arg(short, long)] #[arg(short, long)]
json: bool, json: bool,
/// Show single-character privilege names in addition to human-readable names
///
/// This flag has no effect when used with --json
#[arg(short, long)]
long: bool,
/// Return a non-zero exit code if any of the results were erroneous /// Return a non-zero exit code if any of the results were erroneous
#[arg(short, long)] #[arg(short, long)]
fail: bool, fail: bool,
@@ -67,7 +73,7 @@ pub async fn show_database_privileges(
if args.json { if args.json {
print_list_privileges_output_status_json(&privilege_data); print_list_privileges_output_status_json(&privilege_data);
} else { } else {
print_list_privileges_output_status(&privilege_data); print_list_privileges_output_status(&privilege_data, args.long);
} }
if args.fail && privilege_data.values().any(|res| res.is_err()) { if args.fail && privilege_data.values().any(|res| res.is_err()) {

View File

@@ -101,3 +101,22 @@ pub fn db_priv_field_human_readable_name(name: &str) -> String {
_ => format!("Unknown({})", name), _ => format!("Unknown({})", name),
} }
} }
/// Converts a database privilege field name to a single-character name.
/// (the characters from the cli privilege editor)
pub fn db_priv_field_single_character_name(name: &str) -> &str {
match name {
"select_priv" => "s",
"insert_priv" => "i",
"update_priv" => "u",
"delete_priv" => "d",
"create_priv" => "c",
"drop_priv" => "D",
"alter_priv" => "a",
"index_priv" => "I",
"create_tmp_table_priv" => "t",
"lock_tables_priv" => "l",
"references_priv" => "r",
_ => "?",
}
}

View File

@@ -13,6 +13,7 @@ use crate::core::{
common::yn, common::yn,
database_privileges::{ database_privileges::{
DATABASE_PRIVILEGE_FIELDS, DatabasePrivilegeRow, db_priv_field_human_readable_name, DATABASE_PRIVILEGE_FIELDS, DatabasePrivilegeRow, db_priv_field_human_readable_name,
db_priv_field_single_character_name,
}, },
protocol::request_validation::{NameValidationError, OwnerValidationError}, protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase}, types::{DbOrUser, MySQLDatabase},
@@ -23,7 +24,7 @@ pub type ListPrivilegesRequest = Option<Vec<MySQLDatabase>>;
pub type ListPrivilegesResponse = pub type ListPrivilegesResponse =
BTreeMap<MySQLDatabase, Result<Vec<DatabasePrivilegeRow>, GetDatabasesPrivilegeDataError>>; BTreeMap<MySQLDatabase, Result<Vec<DatabasePrivilegeRow>, GetDatabasesPrivilegeDataError>>;
pub fn print_list_privileges_output_status(output: &ListPrivilegesResponse) { pub fn print_list_privileges_output_status(output: &ListPrivilegesResponse, long_names: bool) {
let mut final_privs_map: BTreeMap<MySQLDatabase, Vec<DatabasePrivilegeRow>> = BTreeMap::new(); let mut final_privs_map: BTreeMap<MySQLDatabase, Vec<DatabasePrivilegeRow>> = BTreeMap::new();
for (db_name, db_result) in output { for (db_name, db_result) in output {
match db_result { match db_result {
@@ -45,7 +46,19 @@ pub fn print_list_privileges_output_status(output: &ListPrivilegesResponse) {
table.add_row(Row::new( table.add_row(Row::new(
DATABASE_PRIVILEGE_FIELDS DATABASE_PRIVILEGE_FIELDS
.into_iter() .into_iter()
.map(db_priv_field_human_readable_name) .map(|field| {
if field == "Db" || field == "User" {
db_priv_field_human_readable_name(field)
} else if long_names {
format!(
"{} ({})",
db_priv_field_human_readable_name(field),
db_priv_field_single_character_name(field),
)
} else {
db_priv_field_human_readable_name(field)
}
})
.map(|name| Cell::new(&name)) .map(|name| Cell::new(&name))
.collect(), .collect(),
)); ));