72 lines
1.8 KiB
Rust
72 lines
1.8 KiB
Rust
use crate::{
|
|
client::commands::erroneous_server_response,
|
|
core::{
|
|
protocol::{
|
|
ClientToServerMessageStream, Request, Response,
|
|
print_check_authorization_output_status, print_check_authorization_output_status_json,
|
|
},
|
|
types::DbOrUser,
|
|
},
|
|
};
|
|
use clap::Parser;
|
|
use futures_util::SinkExt;
|
|
use tokio_stream::StreamExt;
|
|
|
|
#[derive(Parser, Debug, Clone)]
|
|
pub struct CheckAuthArgs {
|
|
/// The `MySQL` database(s) or user(s) to check authorization for
|
|
#[arg(num_args = 1.., value_name = "NAME")]
|
|
name: Vec<String>,
|
|
|
|
/// Treat the provided names as users instead of databases
|
|
#[arg(short, long)]
|
|
users: bool,
|
|
|
|
/// Print the information as JSON
|
|
#[arg(short, long)]
|
|
json: bool,
|
|
}
|
|
|
|
pub async fn check_authorization(
|
|
args: CheckAuthArgs,
|
|
mut server_connection: ClientToServerMessageStream,
|
|
) -> anyhow::Result<()> {
|
|
if args.name.is_empty() {
|
|
anyhow::bail!("No database/user names provided");
|
|
}
|
|
|
|
let payload = args
|
|
.name
|
|
.into_iter()
|
|
.map(|name| {
|
|
if args.users {
|
|
DbOrUser::User(name.into())
|
|
} else {
|
|
DbOrUser::Database(name.into())
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
let message = Request::CheckAuthorization(payload);
|
|
server_connection.send(message).await?;
|
|
|
|
let result = match server_connection.next().await {
|
|
Some(Ok(Response::CheckAuthorization(response))) => response,
|
|
response => return erroneous_server_response(response),
|
|
};
|
|
|
|
server_connection.send(Request::Exit).await?;
|
|
|
|
if args.json {
|
|
print_check_authorization_output_status_json(&result);
|
|
} else {
|
|
print_check_authorization_output_status(&result);
|
|
}
|
|
|
|
if result.values().any(std::result::Result::is_err) {
|
|
std::process::exit(1);
|
|
}
|
|
|
|
Ok(())
|
|
}
|